Skip to content
Merged
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
2 changes: 1 addition & 1 deletion actions/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function create<T>(obj: T, path: string): Promise<T> {
return request(obj, path, 'POST');
}

export async function del<T>(id: string, path: string): Promise<any> {
export async function del(id: string, path: string): Promise<any> {
return request(undefined, `${path}/${id}`, 'DELETE');
}

Expand Down
3 changes: 1 addition & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import Chat from '@/components/chat';
import Threads from '@/components/threads';
import { ChatContextProvider } from '@/contexts/chat';
import { NavContext } from '@/contexts/nav';
import { tildy } from '@/config/assistant';
import ExploreModal from '@/components/explore/ExploreModal';
import { useDisclosure } from '@nextui-org/react';

function RunFile() {
const [script, _setScript] = useState<string>(
useSearchParams().get('file') ?? tildy
useSearchParams().get('file') ?? ''
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this is really all I wanted to do. However, this caused a bunch of errors to start popping up on launch, staring with the getMe stuff.

Most of the other changes are resolving the errors that popped up as a result.

);
const [scriptId, _scriptId] = useState<string>(
useSearchParams().get('id') ?? ''
Expand Down
3 changes: 2 additions & 1 deletion components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const Chat: React.FC<ScriptProps> = ({
} = useContext(ChatContext);

useEffect(() => {
rootTool(scriptContent).then((tool) => setTool(tool));
if (scriptContent.length)
rootTool(scriptContent).then((tool) => setTool(tool));
}, [scriptContent]);

useEffect(() => {
Expand Down
5 changes: 3 additions & 2 deletions components/threads/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const NewThread = ({ className, onOpenExplore }: NewThreadProps) => {

const fetchScripts = async () => {
setScripts([]);
const resp = await getScripts({ owner: me?.username });
if (!me) return;
const resp = await getScripts({ owner: me.username });
setScripts(
(resp.scripts || [])
.sort((a, b) => {
Expand Down Expand Up @@ -67,7 +68,7 @@ const NewThread = ({ className, onOpenExplore }: NewThreadProps) => {

useEffect(() => {
fetchScripts();
}, [isOpen]);
}, [isOpen, me]);

return (
<Dropdown placement="right-start">
Expand Down
24 changes: 7 additions & 17 deletions contexts/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,22 @@ const AuthContextProvider: React.FC<AuthContextProps> = ({ children }) => {

useEffect(() => {
setAuthenticated(document && document?.cookie?.includes('gateway_token'));
if (document && document.cookie?.includes('gateway_token')) {
getMe()
.then((me) => {
setMe(me);
setAuthenticated(true);
})
.finally(() => setLoading(false));
if (!document || !document.cookie?.includes('gateway_token')) {
loginThroughTool().then(() => {
setLoading(false);
});
} else {
loginThroughTool()
.then(() => {
getMe().then((me) => {
setMe(me);
setAuthenticated(true);
});
})
.finally(() => setLoading(false));
setLoading(false);
}
}, []);

useEffect(() => {
if (!authenticated) setMe(null);
if (loading) setMe(null);
else if (!me)
getMe().then((me) => {
setMe(me);
});
}, [authenticated]);
}, [me, loading]);

return (
<AuthContext.Provider
Expand Down
28 changes: 13 additions & 15 deletions contexts/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { getThreads, getThread, Thread, createThread } from '@/actions/threads';
import { getScript, getScriptContent } from '@/actions/me/scripts';
import { loadTools, parseContent, rootTool } from '@/actions/gptscript';
import debounce from 'lodash/debounce';
import { getWorkspaceDir, setWorkspaceDir } from '@/actions/workspace';
import { setWorkspaceDir } from '@/actions/workspace';
import { gatewayTool } from '@/actions/knowledge/util';
import { tildy } from '@/config/assistant';

interface ChatContextProps {
children: React.ReactNode;
Expand Down Expand Up @@ -83,7 +84,7 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
enableThread,
}) => {
const [script, setScript] = useState<string>(initialScript);
const [workspace, setWorkspace] = useState('');
const [workspace, setWorkspace] = useState<string>('');
const [tool, setTool] = useState<Tool>({} as Tool);
const [program, setProgram] = useState<Program>({} as Program);
const [showForm, setShowForm] = useState(true);
Expand Down Expand Up @@ -141,14 +142,6 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
});
}, [scriptContent]);

// need to initialize the workspace from the env variable with serves
// as the default.
useEffect(() => {
getWorkspaceDir().then((workspace) => {
setWorkspace(workspace);
});
}, []);

useEffect(() => {
if (scriptId) {
getScript(scriptId).then(async (script) => {
Expand All @@ -161,14 +154,16 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
setScriptDisplayName(script.displayName || '');
setInitialFetch(true);
});
} else {
getScriptContent(script).then(async (content) => {
} else if (script) {
getScriptContent(script).then((content) => {
if (content === undefined) {
setNotFound(true);
return;
}
setScriptDisplayName(defaultScriptName);
setScriptContent(await parseContent(content));
parseContent(content).then((parsedContent) => {
setScriptContent(parsedContent);
});
setNotFound(false);
setInitialFetch(true);
});
Expand All @@ -186,12 +181,14 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
if ((initialScript && initialScriptId) || threads.length === 0) {
// if both threads and scriptId are set, then always create a new thread
const newThread = await createThread(
script,
scriptDisplayName ?? defaultScriptName,
script ? script : tildy,
scriptDisplayName !== '' ? scriptDisplayName : defaultScriptName,
scriptId
);
const threadId = newThread?.meta?.id;
setThread(threadId);
setScript(newThread.meta.script);
setScriptId(newThread.meta.scriptId);
setThreads(await getThreads());
setSelectedThreadId(threadId);
setWorkspace(newThread.meta.workspace);
Expand All @@ -201,6 +198,7 @@ const ChatContextProvider: React.FC<ChatContextProps> = ({
setThread(latestThread.meta.id);
setSelectedThreadId(latestThread.meta.id);
setScriptId(latestThread.meta.scriptId);
setScript(latestThread.meta.script);
}
} catch (e) {
threadInitialized.current = false;
Expand Down
4 changes: 1 addition & 3 deletions contexts/nav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { createContext, useState, useEffect } from 'react';
import { Me, getMe } from '@/actions/me/me';
import { loginThroughTool } from '@/actions/auth/auth';
import { createContext, useState } from 'react';

interface NavContextProps {
children: React.ReactNode;
Expand Down