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

OV-416: Generate Script with AI dont returns json to view #434

Merged
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
1 change: 1 addition & 0 deletions frontend/src/bundles/chat/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { sanitizeJsonString } from './sanitize-json-string.js';
6 changes: 6 additions & 0 deletions frontend/src/bundles/chat/helpers/sanitize-json-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const sanitizeJsonString = (input: string): string => {
const match = input.match(/\[.*]/s);
return match ? match[0].trim() : input.trim();
};

export { sanitizeJsonString };
44 changes: 42 additions & 2 deletions frontend/src/bundles/chat/store/slice.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
import { createSlice } from '@reduxjs/toolkit';

import { MessageSender } from '~/bundles/chat/enums/enums.js';
import { sanitizeJsonString } from '~/bundles/chat/helpers/helpers.js';
import {
type GenerateTextRequestDto,
type Message,
} from '~/bundles/chat/types/types.js';
import {
EMPTY_VALUE,
LAST_ELEMENT_INDEX,
} from '~/bundles/common/constants/constants.js';
import { DataStatus } from '~/bundles/common/enums/enums.js';
import { type ValueOf } from '~/bundles/common/types/types.js';
import {
type ValueOf,
type VideoScript,
} from '~/bundles/common/types/types.js';

import { deleteChat, sendMessage } from './actions.js';

type State = {
messages: Message[];
videoScripts: VideoScript[];
videoScriptErrorMessage: string;
dataStatus: ValueOf<typeof DataStatus>;
};

const initialState: State = {
messages: [],
videoScripts: [],
videoScriptErrorMessage: '',
dataStatus: DataStatus.IDLE,
};

const { reducer, actions, name } = createSlice({
initialState,
name: 'chat',
reducers: {},
reducers: {
generateVideoScript(state) {
const messages = state.messages.filter(
(message) => message.sender === MessageSender.AI,
);

if (!messages || messages.length === EMPTY_VALUE) {
return;
}

const lastMessage = messages.at(LAST_ELEMENT_INDEX);
if (!lastMessage) {
return;
}

try {
const sanitizedJson = sanitizeJsonString(lastMessage.text);
const videoScripts: VideoScript[] = JSON.parse(sanitizedJson);
state.videoScripts = videoScripts;
state.videoScriptErrorMessage = '';
} catch {
state.videoScripts = [];
state.videoScriptErrorMessage =
'There was an error Generating the Script, please Re-Generate';
}
},
},
extraReducers(builder) {
builder.addCase(sendMessage.pending, (state) => {
state.dataStatus = DataStatus.PENDING;
Expand Down Expand Up @@ -60,6 +98,8 @@ const { reducer, actions, name } = createSlice({
});
builder.addCase(deleteChat.fulfilled, (state) => {
state.messages = [];
state.videoScripts = [];
state.videoScriptErrorMessage = '';
state.dataStatus = DataStatus.FULFILLED;
});
builder.addCase(deleteChat.rejected, (state) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import { EMPTY_VALUE } from '~/bundles/common/constants/constants.js';
import { DataStatus } from '~/bundles/common/enums/data-status.enum.js';
import { useAppSelector } from '~/bundles/common/hooks/hooks.js';
import { IconName } from '~/bundles/common/icons/icons.js';
import { type VideoScript } from '~/bundles/common/types/types.js';

import { GenerateScriptPlaceholderContent } from '../generate-script-placeholder-content/generate-script-placeholder-content.js';
import { GenerateScriptScene } from '../generate-script-scene/generate-script-scene.js';
import styles from './styles.module.css';

type Properties = {
videoScripts: VideoScript[];
};

const GenerateScriptPlaceholder: React.FC<Properties> = ({ videoScripts }) => {
const { dataStatus } = useAppSelector(({ chat }) => ({
dataStatus: chat.dataStatus,
}));
const GenerateScriptPlaceholder: React.FC = () => {
const { dataStatus, videoScripts, videoScriptErrorMessage } =
useAppSelector(({ chat }) => ({
dataStatus: chat.dataStatus,
videoScripts: chat.videoScripts,
videoScriptErrorMessage: chat.videoScriptErrorMessage,
}));

const renderLoadingState = (): React.ReactNode => (
<Box mt="100px">
Expand All @@ -31,6 +29,13 @@ const GenerateScriptPlaceholder: React.FC<Properties> = ({ videoScripts }) => {
/>
);

const renderErrorMessage = (): React.ReactNode => (
<GenerateScriptPlaceholderContent
message={videoScriptErrorMessage}
icon={IconName.WARNING}
/>
);

const renderScripts = (): React.ReactNode => (
<>
{videoScripts.map((videoScript, index) => (
Expand All @@ -43,6 +48,9 @@ const GenerateScriptPlaceholder: React.FC<Properties> = ({ videoScripts }) => {
if (dataStatus === DataStatus.PENDING) {
return renderLoadingState();
}
if (videoScriptErrorMessage) {
return renderErrorMessage();
}
if (videoScripts.length === EMPTY_VALUE) {
return renderEmptyState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ import {
TabPanels,
Tabs,
} from '~/bundles/common/components/components.js';
import {
getVideoScriptMessageFromPayload,
sanitizeJsonString,
} from '~/bundles/common/components/video-modal/components/video-modal-content/helpers/helpers.js';
import { EMPTY_VALUE } from '~/bundles/common/constants/constants.js';
import { getVideoScriptMessageFromPayload } from '~/bundles/common/components/video-modal/components/video-modal-content/helpers/helpers.js';
import {
useAppDispatch,
useAppSelector,
useCallback,
useMemo,
} from '~/bundles/common/hooks/hooks.js';
import { type VideoScript } from '~/bundles/common/types/video-script.type.js';
import { type GenerateVideoScriptRequestDto } from '~/bundles/video-scripts/video-scripts.js';

import { GenerateScriptForm } from '../generate-script-form/generate-script-form.js';
Expand All @@ -41,35 +35,15 @@ const GenerateScriptView: React.FC = () => {
const sendMessageRequest: GenerateTextRequestDto = {
message: getVideoScriptMessageFromPayload(payload, messages),
};
void dispatch(chatActions.sendMessage(sendMessageRequest));
void dispatch(chatActions.sendMessage(sendMessageRequest)).then(
() => {
dispatch(chatActions.generateVideoScript());
},
);
},
[messages, dispatch],
);

const lastGeneratedScript: VideoScript[] = useMemo(() => {
if (!messages || messages.length === EMPTY_VALUE) {
return [];
}

const lastMessage = messages.at(-1);
if (!lastMessage) {
return [];
}

try {
const sanitizedJson = sanitizeJsonString(lastMessage.text);
const videoScripts: VideoScript[] = JSON.parse(sanitizedJson);
return videoScripts;
} catch {
return [
{
title: 'Scene',
description: lastMessage.text,
},
];
}
}, [messages]);

return (
<>
<Heading className={styles['script-view-heading']} variant="H3">
Expand All @@ -94,9 +68,7 @@ const GenerateScriptView: React.FC = () => {
<GenerateScriptForm
onSubmit={handleGenerateVideoScriptSubmit}
/>
<GenerateScriptPlaceholder
videoScripts={lastGeneratedScript}
/>
<GenerateScriptPlaceholder />
</HStack>
</TabPanel>
</TabPanels>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getVideoScriptMessageFromPayload } from './generate-script-message-template/generate-script-message-template.js';
export { sanitizeJsonString } from './sanitize-json-string/sanitize-json-string.js';
export { getVideoScriptMessageFromPayload } from './generate-script-message-template.js';
export { sanitizeJsonString } from './sanitize-json-string.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const sanitizeJsonString = (input: string): string => {
const match = input.match(/\[.*]/s);
return match ? match[0].trim() : input.trim();
};

export { sanitizeJsonString };

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/src/bundles/common/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { FPS } from './fps.constant.js';
export { SOCKET_TRANSPORT_WEBSOCKETS } from './socket-trasnport.constants.js';
export { EMPTY_VALUE } from 'shared';
export { EMPTY_VALUE, LAST_ELEMENT_INDEX } from 'shared';
1 change: 1 addition & 0 deletions shared/src/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { EMPTY_VALUE } from './empty-length.constant.js';
export { LAST_ELEMENT_INDEX } from './last-element-index.constant.js';
3 changes: 3 additions & 0 deletions shared/src/constants/last-element-index.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const LAST_ELEMENT_INDEX = -1;

export { LAST_ELEMENT_INDEX };
2 changes: 1 addition & 1 deletion shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export {
VideosApiPath,
VideoValidationMessage,
} from './bundles/videos/videos.js';
export { EMPTY_VALUE } from './constants/constants.js';
export { EMPTY_VALUE, LAST_ELEMENT_INDEX } from './constants/constants.js';
export {
ApiPath,
AppEnvironment,
Expand Down
Loading