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

WIP feat: update default assistant name to [Deleted Assistant] #430

Closed
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
65 changes: 34 additions & 31 deletions components/chat.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
'use client';

import React, {
useContext,
useEffect,
useState,
useRef,
useCallback,
} from 'react';
import Messages, { MessageType } from '@/components/chat/messages';
import { getGatewayUrl } from '@/actions/gateway';
import { rootTool } from '@/actions/gptscript';
import { generateThreadName, renameThread } from '@/actions/threads';
import { getWorkspaceDir } from '@/actions/workspace';
import ChatBar from '@/components/chat/chatBar';
import ToolForm from '@/components/chat/form';
import Messages, { MessageType } from '@/components/chat/messages';
import Loading from '@/components/loading';
import { Button } from '@nextui-org/react';
import { getWorkspaceDir } from '@/actions/workspace';
import { getGatewayUrl } from '@/actions/gateway';
import { ChatContext } from '@/contexts/chat';
import ScriptToolsDropdown from '@/components/scripts/tool-dropdown';
import AssistantNotFound from '@/components/assistant-not-found';
import { generateThreadName, renameThread } from '@/actions/threads';
import KnowledgeDropdown from '@/components/scripts/knowledge-dropdown';
import SaveScriptDropdown from '@/components/scripts/script-save';
import ScriptToolsDropdown from '@/components/scripts/tool-dropdown';
import { ChatContext } from '@/contexts/chat';
import { Tool } from '@gptscript-ai/gptscript';
import { rootTool } from '@/actions/gptscript';
import { Button } from '@nextui-org/react';
import React, {
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react';

interface ScriptProps {
className?: string;
Expand Down Expand Up @@ -155,11 +154,13 @@ const Chat: React.FC<ScriptProps> = ({
<h1 className="text-3xl font-medium truncate">
{scriptDisplayName ?? ''}
</h1>
<div className="flex gap-2">
<ScriptToolsDropdown />
<KnowledgeDropdown />
<SaveScriptDropdown />
</div>
{!notFound && (
<div className="flex gap-2">
<ScriptToolsDropdown />
<KnowledgeDropdown />
<SaveScriptDropdown />
</div>
)}
</div>
)}
<Messages
Expand All @@ -183,19 +184,21 @@ const Chat: React.FC<ScriptProps> = ({
{tool.chat ? 'Start chat' : 'Run script'}
</Button>
) : (
<ChatBar
disableInput={
disableInput || !running || waitingForUserResponse
}
disableCommands={disableCommands}
inputPlaceholder={inputPlaceholder}
onMessageSent={handleMessageSent}
/>
<>
{!notFound && (
<ChatBar
disableInput={
disableInput || !running || waitingForUserResponse
}
disableCommands={disableCommands}
inputPlaceholder={inputPlaceholder}
onMessageSent={handleMessageSent}
/>
)}
</>
)}
</div>
</>
) : notFound ? (
<AssistantNotFound />
) : (
<Loading>Loading your assistant...</Loading>
)}
Expand Down
33 changes: 7 additions & 26 deletions components/threads.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
import { useState, useContext } from 'react';
import New from './threads/new';
import Menu from './threads/menu';
import { Button, Divider, Tooltip } from '@nextui-org/react';
import { GoSidebarExpand, GoSidebarCollapse } from 'react-icons/go';
import { ChatContext } from '@/contexts/chat';
import { getScript } from '@/actions/me/scripts';
import { Button, Divider, Tooltip } from '@nextui-org/react';
import { useContext, useState } from 'react';
import { GoSidebarCollapse, GoSidebarExpand } from 'react-icons/go';
import Menu from './threads/menu';
import New from './threads/new';

interface ThreadsProps {
className?: string;
onOpenExplore: () => void;
}

const Threads: React.FC<ThreadsProps> = ({ onOpenExplore }: ThreadsProps) => {
const {
setScript,
setScriptContent,
thread,
setThread,
threads,
setScriptId,
setShouldRestart,
} = useContext(ChatContext);
const { thread, threads, switchToThread } = useContext(ChatContext);

const [isCollapsed, setIsCollapsed] = useState(false);

const handleRun = async (script: string, id: string, scriptId: string) => {
if (id !== thread) {
setScriptContent((await getScript(scriptId))?.script || []);
setScript(script);
setThread(id);
setScriptId(scriptId);
setShouldRestart(true);
}
};

const isSelected = (id: string) => id === thread;

return (
Expand Down Expand Up @@ -90,7 +71,7 @@ const Threads: React.FC<ThreadsProps> = ({ onOpenExplore }: ThreadsProps) => {
key={key}
className={`border-1 border-gray-300 px-4 rounded-xl transition duration-150 ease-in-out ${isSelected(thread.meta.id) ? 'bg-primary border-primary dark:border-primary-50 dark:bg-primary-50 text-white' : 'hover:bg-gray-100 dark:hover:bg-zinc-700 cursor-pointer dark:bg-zinc-800 dark:border-zinc-800'} `}
onClick={() =>
handleRun(
switchToThread(
thread.meta.script,
thread.meta.id,
thread.meta.scriptId || ''
Expand Down
95 changes: 64 additions & 31 deletions contexts/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ interface ChatContextState {
interrupt: () => void;
fetchThreads: () => void;
restartScript: () => void;
switchToThread: (
script: string,
id: string,
scriptId: string
) => Promise<void>;
}

const defaultScriptName = `Tildy`;
const notFoundScriptName = `[Assistant Not Found]`;

const ChatContext = createContext<ChatContextState>({} as ChatContextState);
const ChatContextProvider: React.FC<ChatContextProps> = ({
Expand Down Expand Up @@ -121,6 +127,21 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
const threadInitialized = useRef(false);
const [shouldRestart, setShouldRestart] = useState(false);

const switchToThread = async (
script: string,
id: string,
scriptId: string
) => {
if (id !== thread) {
setScript(script);
setThread(id);
setScriptContent((await getScript(scriptId))?.script || []);
setScriptId(scriptId);
// use `setForceRun` instead of `setShouldRestart` because it triggers the `run` WS event which will reset the messages as well
setForceRun(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you shouldn't need to setsetForceRun, setting Thread and ShouldRestart should be sufficient. I see a double refreshing issue when switch to thread, like the thread reloads twices on screen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I thought the same thing and removed it in a previous commit, but actually had to revert that change, because it caused the issue where it just reused the chat from the previous thread

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can try removing the shouldRestart, but I'm not sure if that will work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I fixed this by removing setShouldRestart and keeping setForceRun the reason I opted for this is because it doesn't look like setShouldRestart consistently triggers the run event (which updates the messages). It would probably be more ideal to get shouldRestart to update the messages, but in the interest of time spent on this issue I opted to use the tool already available

}
};

useEffect(() => {
if (!thread || scriptContent.length === 0) return;

Expand All @@ -146,28 +167,32 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
getScript(scriptId).then(async (script) => {
if (script === undefined) {
setNotFound(true);
return;
setScriptContent([]);
setScriptDisplayName(notFoundScriptName);
} else {
setNotFound(false);
setScriptContent(script.script as Block[]);
setScriptDisplayName(script.displayName || '');
}
setNotFound(false);
setScriptContent(script.script as Block[]);
setScriptDisplayName(script.displayName || '');
setInitialFetch(true);
});
} else if (script) {
getScriptContent(script).then((content) => {
if (content === undefined) {
setNotFound(true);
return;
setScriptContent([]);
setScriptDisplayName(notFoundScriptName);
} else {
parseContent(content).then((parsedContent) => {
setScriptContent(parsedContent);
});
setNotFound(false);
setScriptDisplayName(defaultScriptName);
}
setScriptDisplayName(defaultScriptName);
parseContent(content).then((parsedContent) => {
setScriptContent(parsedContent);
});
setNotFound(false);
setInitialFetch(true);
});
}
}, [script, scriptId, thread]);
}, [script, scriptId, setScriptContent, thread]);

useEffect(() => {
if (!enableThread || thread || threadInitialized.current) {
Expand Down Expand Up @@ -215,12 +240,13 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({

useEffect(() => {
if (thread && shouldRestart) {
getThread(thread).then((thread) => {
getThread(thread).then(async (thread) => {
if (thread) {
setInitialFetch(false);
setWorkspace(thread.meta.workspace);
}
restartScript();
// need to wait for the WS Server to restart before triggering another event
await restartScript();
setShouldRestart(false);
});
}
Expand Down Expand Up @@ -301,25 +327,31 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
setInitialFetch(false);

if (scriptId) {
getScript(scriptId).then(async (script) => {
if (script === undefined) {
setNotFound(true);
return;
}
setNotFound(false);
setScriptContent(script.script as Block[]);
setInitialFetch(true);
});
const foundScript = await getScript(scriptId);

if (!foundScript) {
setNotFound(true);
setScriptContent([]);
setScriptDisplayName(notFoundScriptName);
return;
}

setNotFound(false);
setScriptContent(foundScript.script as Block[]);
setInitialFetch(true);
} else {
getScriptContent(script).then(async (content) => {
if (content === undefined) {
setNotFound(true);
return;
}
setScriptContent(await parseContent(content));
setNotFound(false);
setInitialFetch(true);
});
const content = await getScriptContent(script);

if (!content) {
setNotFound(true);
setScriptContent([]);
setScriptDisplayName(notFoundScriptName);
return;
}

setScriptContent(await parseContent(content));
setNotFound(false);
setInitialFetch(true);
}
restart();
}, 200),
Expand Down Expand Up @@ -385,6 +417,7 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
tools,
setTools,
handleCreateThread,
switchToThread,
}}
>
{children}
Expand Down
Loading