Skip to content

Commit 0e2539a

Browse files
authored
Merge pull request #61 from thedadams/gh-736
2 parents a80d481 + ed67605 commit 0e2539a

File tree

8 files changed

+68
-13
lines changed

8 files changed

+68
-13
lines changed

actions/gptscript.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Tool, Block, Text } from '@gptscript-ai/gptscript';
44
import { gpt } from '@/config/env';
55

66
export const rootTool = async (toolContent: string): Promise<Tool> => {
7+
if (!toolContent) return {} as Tool;
78
const parsedTool = await gpt().parseTool(toolContent);
89
for (let block of parsedTool) {
910
if (block.type === 'tool') return block;

actions/me/scripts.tsx

+19-10
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ export async function getScripts(query?: ScriptsQuery): Promise<ParsedScriptsQue
7878

7979
}
8080

81-
export async function getScript(id: string): Promise<ParsedScript> {
82-
const scripts = await get("scripts", id.replace(`${GATEWAY_URL()}/`, '')) as Script
83-
const parsedScript = await gpt().parseTool(scripts.content || '')
84-
return { ...scripts,
85-
script: parsedScript,
86-
description: getDescription(parsedScript),
87-
agentName: getName(parsedScript)
81+
export async function getScript(id: string): Promise<ParsedScript | undefined> {
82+
try {
83+
const scripts = await get("scripts", id.replace(`${GATEWAY_URL()}/`, '')) as Script
84+
const parsedScript = await gpt().parseTool(scripts.content || '')
85+
return {
86+
...scripts,
87+
script: parsedScript,
88+
description: getDescription(parsedScript),
89+
agentName: getName(parsedScript)
90+
}
91+
} catch (e) {
92+
return undefined
8893
}
8994
}
9095

@@ -100,9 +105,13 @@ export async function deleteScript(script: Script) {
100105
return await del(`${script.id}`, "scripts")
101106
}
102107

103-
export async function getScriptContent(scriptURL: string) {
104-
const script = await gpt().parse(scriptURL, true);
105-
return gpt().stringify(script);
108+
export async function getScriptContent(scriptURL: string): Promise<string | undefined> {
109+
try {
110+
const script = await gpt().parse(scriptURL, true);
111+
return gpt().stringify(script);
112+
} catch (e) {
113+
return undefined;
114+
}
106115
}
107116

108117
export async function getNewScriptName() {

components/assistant-not-found.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type NotFoundProps = {
2+
textSize?: string;
3+
spaceY?: string;
4+
};
5+
6+
export default function AssistantNotFound({textSize, spaceY}: NotFoundProps) {
7+
return (
8+
<div className={`flex flex-col justify-center items-center h-full ${spaceY ? spaceY : 'space-y-10'}`}>
9+
<h1 className={`${textSize ? textSize : "text-3xl"}`}>Assistant not found...</h1>
10+
</div>
11+
);
12+
}

components/edit/configure.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
AccordionItem,
1818
} from "@nextui-org/react";
1919
import { PiToolboxBold } from "react-icons/pi";
20+
import AssistantNotFound from "@/components/assistant-not-found"
2021

2122
interface ConfigureProps {
2223
collapsed?: boolean;
@@ -29,6 +30,7 @@ const Configure: React.FC<ConfigureProps> = ({className, collapsed}) => {
2930
setRoot,
3031
models,
3132
loading,
33+
notFound,
3234
visibility,
3335
setVisibility,
3436
dynamicInstructions,
@@ -49,6 +51,8 @@ const Configure: React.FC<ConfigureProps> = ({className, collapsed}) => {
4951

5052
if (loading) return <Loading>Loading your assistant's details...</Loading>;
5153

54+
if (notFound) return <AssistantNotFound />;
55+
5256
return (
5357
<>
5458
<div className="flex flex-col w-full justify-center items-center space-y-2 mb-6 mt-10">

components/script.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import Loading from "@/components/loading";
88
import {Button} from "@nextui-org/react";
99
import {getWorkspaceDir} from "@/actions/workspace";
1010
import {createThread, getThreads, generateThreadName, renameThread} from "@/actions/threads";
11-
import {type Script} from "@/actions/me/scripts";
1211
import {getGatewayUrl} from "@/actions/gateway";
1312
import {ScriptContext} from "@/contexts/script";
13+
import AssistantNotFound from "@/components/assistant-not-found"
1414

1515
interface ScriptProps {
1616
className?: string
@@ -38,6 +38,7 @@ const Script: React.FC<ScriptProps> = ({ className, messagesHeight = 'h-full', e
3838
socket,
3939
connected,
4040
running,
41+
notFound,
4142
restartScript,
4243
scriptId,
4344
fetchThreads,
@@ -127,7 +128,7 @@ const Script: React.FC<ScriptProps> = ({ className, messagesHeight = 'h-full', e
127128
<ChatBar onMessageSent={handleMessageSent} />
128129
)}
129130
</div>
130-
</>) : (
131+
</>) : notFound ? <AssistantNotFound /> : (
131132
<Loading>Loading your assistant...</Loading>
132133
)}
133134
</div>

components/scripts/create.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function Create() {
2626
}).then((script) => {
2727
getScript(`${script.id}`)
2828
.then((script) =>
29-
window.location.href = `/edit?file=${script.publicURL}&id=${script.id}`
29+
window.location.href = `/edit?file=${script?.publicURL}&id=${script?.id}`
3030
);
3131
})
3232
})

contexts/edit.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ interface EditContextProps {
2222
interface EditContextState {
2323
loading: boolean;
2424
setLoading: (loading: boolean) => void;
25+
notFound: boolean;
26+
setNotFound: (notFound: boolean) => void;
2527
root: Tool;
2628
dependencies: DependencyBlock[]; setDependencies: React.Dispatch<React.SetStateAction<DependencyBlock[]>>;
2729
models: string[], setModels: React.Dispatch<React.SetStateAction<string[]>>;
@@ -48,6 +50,7 @@ interface EditContextState {
4850
const EditContext = createContext<EditContextState>({} as EditContextState);
4951
const EditContextProvider: React.FC<EditContextProps> = ({scriptPath, children}) => {
5052
const [loading, setLoading] = useState(true);
53+
const [notFound, setNotFound] = useState(false);
5154
const [root, setRoot] = useState<Tool>({} as Tool);
5255
const [tools, setTools] = useState<Tool[]>([]);
5356
const [script, setScript] = useState<Block[]>([]);
@@ -72,6 +75,10 @@ const EditContextProvider: React.FC<EditContextProps> = ({scriptPath, children})
7275

7376
getScript(scriptPath)
7477
.then(async (script) => {
78+
if (script === undefined) {
79+
setNotFound(true);
80+
return;
81+
}
7582
const parsedScript = await parse(script.content || '')
7683
const texts = await getTexts(script.content || '');
7784
setScript(parsedScript);
@@ -223,6 +230,7 @@ const EditContextProvider: React.FC<EditContextProps> = ({scriptPath, children})
223230
dynamicInstructions, setDynamicInstructions,
224231
models, setModels,
225232
loading, setLoading,
233+
notFound, setNotFound,
226234
root, setRoot,
227235
tools, setTools,
228236
script, setScript,

contexts/script.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,22 @@ const ScriptContextProvider: React.FC<ScriptContextProps> = ({children, initialS
9797
useEffect(() => {
9898
if(scriptId) {
9999
getScript(scriptId).then(async (script) => {
100+
if (script === undefined) {
101+
setNotFound(true);
102+
return;
103+
}
104+
setNotFound(false);
100105
setTool(await rootTool(script.content || ''));
101106
setScriptContent(script.script as Block[]);
102107
setInitialFetch(true);
103108
});
104109
} else {
105110
getScriptContent(script).then(async (content) => {
111+
if (content === undefined) {
112+
setNotFound(true);
113+
return;
114+
}
115+
setNotFound(false);
106116
setTool(await rootTool(content))
107117
setInitialFetch(true);
108118
});
@@ -167,12 +177,22 @@ const ScriptContextProvider: React.FC<ScriptContextProps> = ({children, initialS
167177

168178
if(scriptId) {
169179
getScript(scriptId).then(async (script) => {
180+
if (script === undefined) {
181+
setNotFound(true);
182+
return;
183+
}
184+
setNotFound(false);
170185
setTool(await rootTool(script.content || ''));
171186
setScriptContent(script.script as Block[]);
172187
setInitialFetch(true);
173188
});
174189
} else {
175190
getScriptContent(script).then(async (content) => {
191+
if (content === undefined) {
192+
setNotFound(true);
193+
return;
194+
}
195+
setNotFound(false);
176196
setTool(await rootTool(content))
177197
setInitialFetch(true);
178198
});

0 commit comments

Comments
 (0)