Skip to content

Commit

Permalink
Messages WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bhackett1024 committed Jan 20, 2025
1 parent 74f115a commit f3fa7ac
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions app/components/chat/Chat.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export const ChatImpl = memo(
const [uploadedFiles, setUploadedFiles] = useState<File[]>([]); // Move here
const [imageDataList, setImageDataList] = useState<string[]>([]); // Move here
const [searchParams, setSearchParams] = useSearchParams();
const [trailingMessages, setTrailingMessages] = useState<Message[]>([]);
const [simulationLoading, setSimulationLoading] = useState(false);
const files = useStore(workbenchStore.files);
const { promptId } = useSettings();

Expand All @@ -140,20 +142,10 @@ export const ChatImpl = memo(
'There was an error processing your request: ' + (error.message ? error.message : 'No details were returned'),
);
},
onFinish: (message, response) => {
const usage = response.usage;

if (usage) {
console.log('Token usage:', usage);

// You can now use the usage data as needed
}

logger.debug('Finished streaming');
},
initialMessages,
initialInput: Cookies.get(PROMPT_COOKIE_KEY) || '',
});

useEffect(() => {
const prompt = searchParams.get('prompt');

Expand Down Expand Up @@ -204,6 +196,7 @@ export const ChatImpl = memo(
gNumAborts++;
chatStore.setKey('aborted', true);
workbenchStore.abortAllActions();
setSimulationLoading(false);
};

useEffect(() => {
Expand Down Expand Up @@ -244,14 +237,10 @@ export const ChatImpl = memo(
message = "Error creating recording.";
}

const recordingMessage: CreateMessage = {
const recordingMessage: Message = {
id: `create-recording-${messages.length}`,
role: 'assistant',
content: [
{
type: 'text',
text: message,
},
] as any, // Type assertion to bypass compiler check
content: message,
};

return { recordingId, recordingMessage };
Expand All @@ -267,14 +256,10 @@ export const ChatImpl = memo(
message = "Error enhancing prompt.";
}

const enhancedPromptMessage: CreateMessage = {
const enhancedPromptMessage: Message = {
id: `enhanced-prompt-${messages.length}`,
role: 'assistant',
content: [
{
type: 'text',
text: message,
},
] as any, // Type assertion to bypass compiler check
content: message,
};

return { enhancedPrompt, enhancedPromptMessage };
Expand All @@ -288,6 +273,8 @@ export const ChatImpl = memo(
return;
}

setSimulationLoading(true);

/**
* @note (delm) Usually saving files shouldn't take long but it may take longer if there
* many unsaved files. In that case we need to block user input and show an indicator
Expand All @@ -309,7 +296,8 @@ export const ChatImpl = memo(
return;
}

append(recordingMessage);
console.log("RecordingMessage", recordingMessage);
setTrailingMessages([...trailingMessages, recordingMessage]);

if (recordingId) {
const info = await getEnhancedPrompt(recordingId, contentBase64);
Expand All @@ -319,7 +307,9 @@ export const ChatImpl = memo(
}

simulationEnhancedPrompt = info.enhancedPrompt;
append(info.enhancedPromptMessage);

console.log("EnhancedPromptMessage", info.enhancedPromptMessage);
setTrailingMessages([...trailingMessages, info.enhancedPromptMessage]);
}
}

Expand All @@ -329,6 +319,8 @@ export const ChatImpl = memo(

runAnimation();

setSimulationLoading(false);

append({
role: 'user',
content: [
Expand Down Expand Up @@ -397,7 +389,7 @@ export const ChatImpl = memo(
input={input}
showChat={showChat}
chatStarted={chatStarted}
isStreaming={isLoading}
isStreaming={isLoading || simulationLoading}
enhancingPrompt={enhancingPrompt}
promptEnhanced={promptEnhanced}
sendMessage={sendMessage}
Expand All @@ -411,7 +403,7 @@ export const ChatImpl = memo(
description={description}
importChat={importChat}
exportChat={exportChat}
messages={messages.map((message, i) => {
messages={[...messages, ...trailingMessages].map((message, i) => {
if (message.role === 'user') {
return message;
}
Expand Down

0 comments on commit f3fa7ac

Please sign in to comment.