Skip to content

Commit

Permalink
feat: show threads with deleted scripts in a read-only state
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhopperlowe committed Sep 9, 2024
1 parent 74be4af commit ce07bed
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 89 deletions.
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 @@ -153,11 +152,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 @@ -181,19 +182,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
97 changes: 65 additions & 32 deletions contexts/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ interface ChatContextState {
interrupt: () => void;
fetchThreads: () => void;
restartScript: () => void;
switchToThread: (
script: string,
id: string,
scriptId: string
) => Promise<void>;
}

const defaultScriptName = `[Deleted Assistant]`;
const defaultScriptName = `Tildy`;
const notFoundScriptName = `[Deleted Assistant]`;

const ChatContext = createContext<ChatContextState>({} as ChatContextState);
const ChatContextProvider: React.FC<ChatContextProps> = ({
Expand Down Expand Up @@ -124,6 +130,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);
setForceRun(true);
setShouldRestart(true);
}
};

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

Expand All @@ -149,28 +170,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 @@ -218,12 +243,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 befgore triggering another event
await restartScript();
setShouldRestart(false);
});
}
Expand Down Expand Up @@ -305,25 +331,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 @@ -391,6 +423,7 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
tools,
setTools,
handleCreateThread,
switchToThread,
}}
>
{children}
Expand Down

0 comments on commit ce07bed

Please sign in to comment.