From ed676052d2fe577bbaed931b07a52851f19ff3d8 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Sun, 11 Aug 2024 22:15:27 -0400 Subject: [PATCH] fix: report when a script is not found Signed-off-by: Donnie Adams --- actions/gptscript.tsx | 1 + actions/me/scripts.tsx | 29 +++++++++++++++++++---------- components/assistant-not-found.tsx | 12 ++++++++++++ components/edit/configure.tsx | 4 ++++ components/script.tsx | 5 +++-- components/scripts/create.tsx | 2 +- contexts/edit.tsx | 8 ++++++++ contexts/script.tsx | 22 +++++++++++++++++++++- 8 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 components/assistant-not-found.tsx diff --git a/actions/gptscript.tsx b/actions/gptscript.tsx index 4405118b..7a3bc27a 100644 --- a/actions/gptscript.tsx +++ b/actions/gptscript.tsx @@ -4,6 +4,7 @@ import { Tool, Block, Text } from '@gptscript-ai/gptscript'; import { gpt } from '@/config/env'; export const rootTool = async (toolContent: string): Promise => { + if (!toolContent) return {} as Tool; const parsedTool = await gpt().parseTool(toolContent); for (let block of parsedTool) { if (block.type === 'tool') return block; diff --git a/actions/me/scripts.tsx b/actions/me/scripts.tsx index da5366a6..378d399b 100644 --- a/actions/me/scripts.tsx +++ b/actions/me/scripts.tsx @@ -78,13 +78,18 @@ export async function getScripts(query?: ScriptsQuery): Promise { - const scripts = await get("scripts", id.replace(`${GATEWAY_URL()}/`, '')) as Script - const parsedScript = await gpt().parseTool(scripts.content || '') - return { ...scripts, - script: parsedScript, - description: getDescription(parsedScript), - agentName: getName(parsedScript) +export async function getScript(id: string): Promise { + try { + const scripts = await get("scripts", id.replace(`${GATEWAY_URL()}/`, '')) as Script + const parsedScript = await gpt().parseTool(scripts.content || '') + return { + ...scripts, + script: parsedScript, + description: getDescription(parsedScript), + agentName: getName(parsedScript) + } + } catch (e) { + return undefined } } @@ -100,9 +105,13 @@ export async function deleteScript(script: Script) { return await del(`${script.id}`, "scripts") } -export async function getScriptContent(scriptURL: string) { - const script = await gpt().parse(scriptURL, true); - return gpt().stringify(script); +export async function getScriptContent(scriptURL: string): Promise { + try { + const script = await gpt().parse(scriptURL, true); + return gpt().stringify(script); + } catch (e) { + return undefined; + } } export async function getNewScriptName() { diff --git a/components/assistant-not-found.tsx b/components/assistant-not-found.tsx new file mode 100644 index 00000000..9690238f --- /dev/null +++ b/components/assistant-not-found.tsx @@ -0,0 +1,12 @@ +type NotFoundProps = { + textSize?: string; + spaceY?: string; +}; + +export default function AssistantNotFound({textSize, spaceY}: NotFoundProps) { + return ( +
+

Assistant not found...

+
+ ); +} \ No newline at end of file diff --git a/components/edit/configure.tsx b/components/edit/configure.tsx index 96babecb..2d27d047 100644 --- a/components/edit/configure.tsx +++ b/components/edit/configure.tsx @@ -17,6 +17,7 @@ import { AccordionItem, } from "@nextui-org/react"; import { PiToolboxBold } from "react-icons/pi"; +import AssistantNotFound from "@/components/assistant-not-found" interface ConfigureProps { collapsed?: boolean; @@ -29,6 +30,7 @@ const Configure: React.FC = ({className, collapsed}) => { setRoot, models, loading, + notFound, visibility, setVisibility, dynamicInstructions, @@ -49,6 +51,8 @@ const Configure: React.FC = ({className, collapsed}) => { if (loading) return Loading your assistant's details...; + if (notFound) return ; + return ( <>
diff --git a/components/script.tsx b/components/script.tsx index da76f8c3..33354263 100644 --- a/components/script.tsx +++ b/components/script.tsx @@ -8,9 +8,9 @@ import Loading from "@/components/loading"; import {Button} from "@nextui-org/react"; import {getWorkspaceDir} from "@/actions/workspace"; import {createThread, getThreads, generateThreadName, renameThread} from "@/actions/threads"; -import {type Script} from "@/actions/me/scripts"; import {getGatewayUrl} from "@/actions/gateway"; import {ScriptContext} from "@/contexts/script"; +import AssistantNotFound from "@/components/assistant-not-found" interface ScriptProps { className?: string @@ -38,6 +38,7 @@ const Script: React.FC = ({ className, messagesHeight = 'h-full', e socket, connected, running, + notFound, restartScript, fetchThreads, } = useContext(ScriptContext); @@ -126,7 +127,7 @@ const Script: React.FC = ({ className, messagesHeight = 'h-full', e )}
- ) : ( + ) : notFound ? : ( Loading your assistant... )} diff --git a/components/scripts/create.tsx b/components/scripts/create.tsx index 105a2f84..b219cda4 100644 --- a/components/scripts/create.tsx +++ b/components/scripts/create.tsx @@ -26,7 +26,7 @@ export default function Create() { }).then((script) => { getScript(`${script.id}`) .then((script) => - window.location.href = `/edit?file=${script.publicURL}&id=${script.id}` + window.location.href = `/edit?file=${script?.publicURL}&id=${script?.id}` ); }) }) diff --git a/contexts/edit.tsx b/contexts/edit.tsx index dc9040b0..3baae848 100644 --- a/contexts/edit.tsx +++ b/contexts/edit.tsx @@ -22,6 +22,8 @@ interface EditContextProps { interface EditContextState { loading: boolean; setLoading: (loading: boolean) => void; + notFound: boolean; + setNotFound: (notFound: boolean) => void; root: Tool; dependencies: DependencyBlock[]; setDependencies: React.Dispatch>; models: string[], setModels: React.Dispatch>; @@ -48,6 +50,7 @@ interface EditContextState { const EditContext = createContext({} as EditContextState); const EditContextProvider: React.FC = ({scriptPath, children}) => { const [loading, setLoading] = useState(true); + const [notFound, setNotFound] = useState(false); const [root, setRoot] = useState({} as Tool); const [tools, setTools] = useState([]); const [script, setScript] = useState([]); @@ -72,6 +75,10 @@ const EditContextProvider: React.FC = ({scriptPath, children}) getScript(scriptPath) .then(async (script) => { + if (script === undefined) { + setNotFound(true); + return; + } const parsedScript = await parse(script.content || '') const texts = await getTexts(script.content || ''); setScript(parsedScript); @@ -223,6 +230,7 @@ const EditContextProvider: React.FC = ({scriptPath, children}) dynamicInstructions, setDynamicInstructions, models, setModels, loading, setLoading, + notFound, setNotFound, root, setRoot, tools, setTools, script, setScript, diff --git a/contexts/script.tsx b/contexts/script.tsx index 6c6ff306..a5596c74 100644 --- a/contexts/script.tsx +++ b/contexts/script.tsx @@ -96,17 +96,27 @@ const ScriptContextProvider: React.FC = ({children, initialS useEffect(() => { if(scriptId) { getScript(scriptId).then(async (script) => { + if (script === undefined) { + setNotFound(true); + return; + } + setNotFound(false); setTool(await rootTool(script.content || '')); setScriptContent(script.script as Block[]); setInitialFetch(true); }); } else { getScriptContent(script).then(async (content) => { + if (content === undefined) { + setNotFound(true); + return; + } + setNotFound(false); setTool(await rootTool(content)) setInitialFetch(true); }); } - }, [script]); + }, [script, scriptId]); useEffect(() => { setHasParams(tool.arguments?.properties != undefined && Object.keys(tool.arguments?.properties).length > 0); @@ -165,12 +175,22 @@ const ScriptContextProvider: React.FC = ({children, initialS if(scriptId) { getScript(scriptId).then(async (script) => { + if (script === undefined) { + setNotFound(true); + return; + } + setNotFound(false); setTool(await rootTool(script.content || '')); setScriptContent(script.script as Block[]); setInitialFetch(true); }); } else { getScriptContent(script).then(async (content) => { + if (content === undefined) { + setNotFound(true); + return; + } + setNotFound(false); setTool(await rootTool(content)) setInitialFetch(true); });