Skip to content

Commit

Permalink
Merge pull request #61 from thedadams/gh-736
Browse files Browse the repository at this point in the history
  • Loading branch information
thedadams authored Aug 12, 2024
2 parents a80d481 + ed67605 commit 0e2539a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 13 deletions.
1 change: 1 addition & 0 deletions actions/gptscript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Tool, Block, Text } from '@gptscript-ai/gptscript';
import { gpt } from '@/config/env';

export const rootTool = async (toolContent: string): Promise<Tool> => {
if (!toolContent) return {} as Tool;
const parsedTool = await gpt().parseTool(toolContent);
for (let block of parsedTool) {
if (block.type === 'tool') return block;
Expand Down
29 changes: 19 additions & 10 deletions actions/me/scripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ export async function getScripts(query?: ScriptsQuery): Promise<ParsedScriptsQue

}

export async function getScript(id: string): Promise<ParsedScript> {
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<ParsedScript | undefined> {
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
}
}

Expand All @@ -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<string | undefined> {
try {
const script = await gpt().parse(scriptURL, true);
return gpt().stringify(script);
} catch (e) {
return undefined;
}
}

export async function getNewScriptName() {
Expand Down
12 changes: 12 additions & 0 deletions components/assistant-not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type NotFoundProps = {
textSize?: string;
spaceY?: string;
};

export default function AssistantNotFound({textSize, spaceY}: NotFoundProps) {
return (
<div className={`flex flex-col justify-center items-center h-full ${spaceY ? spaceY : 'space-y-10'}`}>
<h1 className={`${textSize ? textSize : "text-3xl"}`}>Assistant not found...</h1>
</div>
);
}
4 changes: 4 additions & 0 deletions components/edit/configure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ const Configure: React.FC<ConfigureProps> = ({className, collapsed}) => {
setRoot,
models,
loading,
notFound,
visibility,
setVisibility,
dynamicInstructions,
Expand All @@ -49,6 +51,8 @@ const Configure: React.FC<ConfigureProps> = ({className, collapsed}) => {

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

if (notFound) return <AssistantNotFound />;

return (
<>
<div className="flex flex-col w-full justify-center items-center space-y-2 mb-6 mt-10">
Expand Down
5 changes: 3 additions & 2 deletions components/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,6 +38,7 @@ const Script: React.FC<ScriptProps> = ({ className, messagesHeight = 'h-full', e
socket,
connected,
running,
notFound,
restartScript,
scriptId,
fetchThreads,
Expand Down Expand Up @@ -127,7 +128,7 @@ const Script: React.FC<ScriptProps> = ({ className, messagesHeight = 'h-full', e
<ChatBar onMessageSent={handleMessageSent} />
)}
</div>
</>) : (
</>) : notFound ? <AssistantNotFound /> : (
<Loading>Loading your assistant...</Loading>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/scripts/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
})
})
Expand Down
8 changes: 8 additions & 0 deletions contexts/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.SetStateAction<DependencyBlock[]>>;
models: string[], setModels: React.Dispatch<React.SetStateAction<string[]>>;
Expand All @@ -48,6 +50,7 @@ interface EditContextState {
const EditContext = createContext<EditContextState>({} as EditContextState);
const EditContextProvider: React.FC<EditContextProps> = ({scriptPath, children}) => {
const [loading, setLoading] = useState(true);
const [notFound, setNotFound] = useState(false);
const [root, setRoot] = useState<Tool>({} as Tool);
const [tools, setTools] = useState<Tool[]>([]);
const [script, setScript] = useState<Block[]>([]);
Expand All @@ -72,6 +75,10 @@ const EditContextProvider: React.FC<EditContextProps> = ({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);
Expand Down Expand Up @@ -223,6 +230,7 @@ const EditContextProvider: React.FC<EditContextProps> = ({scriptPath, children})
dynamicInstructions, setDynamicInstructions,
models, setModels,
loading, setLoading,
notFound, setNotFound,
root, setRoot,
tools, setTools,
script, setScript,
Expand Down
20 changes: 20 additions & 0 deletions contexts/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,22 @@ const ScriptContextProvider: React.FC<ScriptContextProps> = ({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);
});
Expand Down Expand Up @@ -167,12 +177,22 @@ const ScriptContextProvider: React.FC<ScriptContextProps> = ({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);
});
Expand Down

0 comments on commit 0e2539a

Please sign in to comment.