Skip to content

Commit

Permalink
Merge pull request #418 from tylerslaton/issue-307
Browse files Browse the repository at this point in the history
fix: address issue with assistant menu in editor
  • Loading branch information
tylerslaton authored Sep 5, 2024
2 parents 9d3d776 + 23187c8 commit f8cb3bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
24 changes: 24 additions & 0 deletions actions/me/scripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,27 @@ export async function getScriptContent(
return undefined;
}
}

const newDefaultAssistant = (name: string): string => {
return `
Name: ${name}
Chat: true
You are a helpful assistant named ${name}. When you first start, just introduce yourself and wait for the user's next message.
`;
};

export async function createDefaultAssistant(): Promise<Script> {
const defaultName = 'New Assistant';
const slug =
defaultName.toLowerCase().replaceAll(' ', '-') +
'-' +
Math.random().toString(36).substring(2, 7);

return await createScript({
displayName: defaultName,
slug,
visibility: 'private',
content: newDefaultAssistant(defaultName),
});
}
13 changes: 11 additions & 2 deletions components/edit/scriptNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ParsedScript, getScripts } from '@/actions/me/scripts';
import { EditContext } from '@/contexts/edit';
import { AuthContext } from '@/contexts/auth';
import { stringify } from '@/actions/gptscript';
import { createDefaultAssistant } from '@/actions/me/scripts';

interface ScriptNavProps {
className?: string;
Expand All @@ -27,6 +28,12 @@ const ScriptNav: React.FC<ScriptNavProps> = ({ collapsed, setCollapsed }) => {
const { script, loading } = useContext(EditContext);
const { me } = useContext(AuthContext);

const handleNew = async () => {
createDefaultAssistant().then((script) => {
window.location.href = `/edit?id=${script?.id}`;
});
};

useEffect(() => {
if (!me) return;
getScripts({ owner: me?.username })
Expand All @@ -39,7 +46,7 @@ const ScriptNav: React.FC<ScriptNavProps> = ({ collapsed, setCollapsed }) => {
scripts.map((script, i) => (
<DropdownItem
startContent={<GoPerson className="mb-1" />}
key={script.publicURL}
key={script.id}
>
{script.agentName || `Untitled Assistant ${i}`}
</DropdownItem>
Expand Down Expand Up @@ -74,8 +81,10 @@ const ScriptNav: React.FC<ScriptNavProps> = ({ collapsed, setCollapsed }) => {
stringify(script).then((gptscript) => {
navigator.clipboard.writeText(gptscript);
});
} else if (key === 'new') {
handleNew();
} else {
window.location.href = `/edit?file=${key}`;
window.location.href = `/edit?id=${key}`;
}
}}
disabledKeys={['no-files']}
Expand Down
30 changes: 5 additions & 25 deletions components/scripts/create.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
import React from 'react';
import { Button } from '@nextui-org/react';
import { GoPersonAdd } from 'react-icons/go';
import { createScript, getScript } from '@/actions/me/scripts';

const newDefaultAssistant = (name: string): string => {
return `
Name: ${name}
Chat: true
You are a helpful assistant named ${name}. When you first start, just introduce yourself and wait for the user's next message.
`;
};
import { createDefaultAssistant } from '@/actions/me/scripts';
import { useRouter } from 'next/navigation';

export default function Create() {
const [loading, setLoading] = React.useState(false);
const router = useRouter();

const handleSubmit = async () => {
const defaultName = 'New Assistant';
const slug =
defaultName.toLowerCase().replaceAll(' ', '-') +
'-' +
Math.random().toString(36).substring(2, 7);
createScript({
displayName: defaultName,
slug,
visibility: 'private',
content: newDefaultAssistant(defaultName),
}).then((script) => {
getScript(`${script.id}`).then(
(script) =>
(window.location.href = `/edit?file=${script?.publicURL}&id=${script?.id}`)
);
createDefaultAssistant().then((script) => {
router.push(`/edit?id=${script?.id}`);
});
};

Expand Down

0 comments on commit f8cb3bd

Please sign in to comment.