From 68189d0fc108a5583ea6b74c1a72cd17d7f6c176 Mon Sep 17 00:00:00 2001 From: Jonas Lindberg Date: Mon, 22 Apr 2024 05:47:37 +0000 Subject: [PATCH] work on agent creation and updating --- .../ui/library/library-create-button.svelte | 10 +- apps/web/src/lib/schema.ts | 10 +- .../web/src/routes/app/agents/+page.server.ts | 186 +++++++----------- apps/web/src/routes/app/agents/+page.svelte | 74 ++++--- .../src/routes/app/agents/CreateForm.svelte | 49 ++++- apps/web/src/routes/app/agents/Edit.svelte | 87 -------- .../src/routes/app/agents/UpdateForm.svelte | 112 +++++++++++ 7 files changed, 280 insertions(+), 248 deletions(-) delete mode 100644 apps/web/src/routes/app/agents/Edit.svelte create mode 100644 apps/web/src/routes/app/agents/UpdateForm.svelte diff --git a/apps/web/src/lib/components/ui/library/library-create-button.svelte b/apps/web/src/lib/components/ui/library/library-create-button.svelte index 8fb7372a..db873b36 100644 --- a/apps/web/src/lib/components/ui/library/library-create-button.svelte +++ b/apps/web/src/lib/components/ui/library/library-create-button.svelte @@ -1,19 +1,13 @@ - dispatch('close')}> + (open = true)} class="transition-hover group relative flex aspect-[3/4] flex-col items-center justify-center overflow-hidden rounded-lg border border-primary bg-background from-primary-950 to-primary-800 shadow-lg duration-300 hover:scale-105 hover:bg-gradient-to-br hover:shadow-xl" > diff --git a/apps/web/src/lib/schema.ts b/apps/web/src/lib/schema.ts index 21ea5ca6..fd3a223f 100644 --- a/apps/web/src/lib/schema.ts +++ b/apps/web/src/lib/schema.ts @@ -21,17 +21,17 @@ export const createCrewSchema = z.object({ }); export type CreateCrewSchema = typeof createCrewSchema; -export const createAgentSchema = z.object({ +export const agentSchema = z.object({ + id: z.string(), title: z.string().min(1).max(50), description: z.string().max(500).default(''), published: z.boolean().default(false), role: z.string().min(1).max(50), - tools: z.string(), + tools: z.array(z.record(z.string(), z.never())), system_message: z.string().min(20), - model: z.string() + model: z.union([z.literal('gpt-4-turbo'), z.literal('gpt-3.5-turbo')]) }); - -export type CreateAgentSchema = typeof createAgentSchema; +export type AgentSchema = typeof agentSchema; // TODO: rename to createUserSchema export const formSchema = z.object({ diff --git a/apps/web/src/routes/app/agents/+page.server.ts b/apps/web/src/routes/app/agents/+page.server.ts index 16d44835..0f00640a 100644 --- a/apps/web/src/routes/app/agents/+page.server.ts +++ b/apps/web/src/routes/app/agents/+page.server.ts @@ -2,8 +2,8 @@ import { supabase } from '$lib/supabase'; import { fail, error } from '@sveltejs/kit'; import { zod } from 'sveltekit-superforms/adapters'; -import { createAgentSchema, editAgentSchema } from '$lib/schema'; -import { superValidate } from 'sveltekit-superforms/server'; +import { agentSchema } from '$lib/schema'; +import { setError, superValidate } from 'sveltekit-superforms/server'; import { pickRandomAvatar } from '$lib/utils'; import api from '$lib/api'; @@ -34,7 +34,7 @@ export const load = async ({ locals }) => { }); const form = { - create: await superValidate(zod(createAgentSchema)) + agent: await superValidate(zod(agentSchema)) }; return { @@ -45,132 +45,96 @@ export const load = async ({ locals }) => { export const actions = { create: async ({ request, locals }) => { - const session = await locals.getSession(); + console.log('create agent'); + const userSession = await locals.getSession(); - const form = await superValidate(request, zod(createAgentSchema)); + const form = await superValidate(request, zod(agentSchema)); if (!form.valid) { return fail(400, { form, message: 'unable to create a new agent' }); } - console.log(form); - const randomAvatar = pickRandomAvatar(); - let data, error; + const randomAvatar = pickRandomAvatar(); - try { - ({ data, error } = await supabase - .from('agents') - .insert([ - { - profile_id: session?.user.id, - title: form.data.title, - description: form.data.description, - model: form.data.model === 'undefined' ? 'gpt-3.5-turbo' : form.data.model, - role: form.data.role, - published: form.data.published === 'on' ? true : false, - system_message: form.data.system_message, - tools: [ - { - id: form.data.id, - parameter: {} - } - ], - avatar: randomAvatar.avatarUrl, - version: '1.0' - } - ]) - .select()); - } catch (error) { - console.error(error); - return fail(500, { - message: 'Something went wrong , please try again' + const agent = await api + .POST('/agents/', { + body: { + profile_id: userSession.user.id, + avatar: randomAvatar.avatarUrl, + title: form.data.title, + description: form.data.description, + // published: form.data.published, + role: form.data.role, + tools: form.data.tools, + system_message: form.data.system_message, + model: form.data.model === 'gpt-4-turbo' ? 'gpt-4-turbo-preview' : 'gpt-3.5-turbo' + } + }) + .then(({ data: d, error: e }) => { + if (e) { + console.error(`Error creating crew: ${e.detail}`); + return fail(500, { + message: + 'Agent create failed. Please try again. If the problem persists, contact support.' + }); + } + if (!d) { + console.error(`No data returned from crew creation`); + return fail(500, { + message: + 'Agent create failed. Please try again. If the problem persists, contact support.' + }); + } + return d; }); - } - - if (error) { - console.error('Error creating agent:', error); - return { error, message: error.message }; - } - return { - message: 'Agent created successfully please reload the page to view your new agent' - }; + return { form }; }, - editAgent: async ({ request, url }) => { - const id = url.searchParams.get('id'); + update: async ({ request, locals }) => { + console.log('update agent'); + const userSession = await locals.getSession(); - const form = await superValidate(request, zod(createAgentSchema)); - console.log(form, 'form'); - - const currentAgent = await supabase - .from('agents') - .select('*') - .eq('id', id?.split('$')[1]) - .single(); - - const prev_tools = currentAgent.data.tools; - - console.log(prev_tools); + const form = await superValidate(request, zod(agentSchema)); if (!form.valid) { - return fail(400, { form, message: 'Could not edit agent' }); + return fail(400, { form, message: 'unable to create a new agent' }); } - console.log(form); - - let data, error; - - try { - ({ data, error } = await supabase - .from('agents') - .update({ + const agent = await api + .PATCH('/agents/{id}', { + params: { + path: { + id: form.data.id + } + }, + body: { title: form.data.title, - role: form.data.role, description: form.data.description, - tools: [ - ...currentAgent.data.tools, - { - id: form.data.id, - parameter: {} - } - ], + published: form.data.published, + role: form.data.role, + tools: form.data.tools, system_message: form.data.system_message, - model: form.data.model, - published: form.data.published === 'on' ? true : false - }) - .eq('id', id?.split('$')[1])); - } catch (error) { - console.error('something when wron when editing agent:', error); - return fail(500, { message: 'Something went wrong, please try again' }); - } - - if (error) { - console.error('Error editing agent:', error); - return { error }; - } - - console.log('Agent edited successfully:', data); - return { - message: 'Agent edited successfully please reload to see the changes you made' - }; - }, - removeTools: async ({ request, url }) => { - const id = url.searchParams.get('id'); - const toolId = url.searchParams.get('toolId'); - const form = await request.formData(); - - console.log(id, toolId, 'id, toolId'); - - const currentAgent = await supabase.from('agents').select('*').eq('id', id).single(); - - console.log(currentAgent, 'currentAgent'); - - const deleteTool = currentAgent.data.tools.filter((tool) => tool.id !== toolId); - const { data, error } = await supabase - .from('agents') - .update({ - tools: deleteTool + model: form.data.model === 'gpt-4-turbo' ? 'gpt-4-turbo-preview' : 'gpt-3.5-turbo' + } }) - .eq('id', id); + .then(({ data: d, error: e }) => { + if (e) { + console.error(`Error creating crew: ${e.detail}`); + throw fail(500, { + message: + 'Agent update failed. Please try again. If the problem persists, contact support.' + }); + } + if (!d) { + console.error(`No data returned from crew creation`); + throw fail(500, { + message: + 'Agent update failed. Please try again. If the problem persists, contact support.' + }); + } + return d; + }); + + return { form }; } }; diff --git a/apps/web/src/routes/app/agents/+page.svelte b/apps/web/src/routes/app/agents/+page.svelte index e5d3d2e7..b1f5734c 100644 --- a/apps/web/src/routes/app/agents/+page.svelte +++ b/apps/web/src/routes/app/agents/+page.svelte @@ -1,6 +1,8 @@ - + {#each data.agents as agent} - -
- - {agent.title} - - - {agent.role} - -
-
- - Are you sure absolutely sure? - - This action cannot be undone. This will permanently delete this agent from our services. - Make sure to delete the agent from all of your own crews before you perform this action. - - - - Cancel - - -
-
+ + + (open = true)} avatar={agent.avatar}> +
+ + {agent.title} + + + {agent.role} + +
+
+ + Are you sure absolutely sure? + + This action cannot be undone. This will permanently delete this agent from our + services. Make sure to delete the agent from all of your own crews before you + perform this action. + + + + Cancel + + +
+
+
+ + + +
{/each}
diff --git a/apps/web/src/routes/app/agents/CreateForm.svelte b/apps/web/src/routes/app/agents/CreateForm.svelte index 93ead01a..a36a4741 100644 --- a/apps/web/src/routes/app/agents/CreateForm.svelte +++ b/apps/web/src/routes/app/agents/CreateForm.svelte @@ -1,23 +1,37 @@
-

Create a new Agent

+ + Create a new Agent + + You are about to create a new Agent. Please fill out the form below. + +

Basic Information

@@ -43,6 +57,29 @@

Functional Options

+ + + Model* + { + v && ($formData.model = v.value); + }} + > + + + + + + Models + gpt-4-turbo + gpt-3.5-turbo + + + + + Role* @@ -59,5 +96,5 @@

* required fields

- Create + Update
diff --git a/apps/web/src/routes/app/agents/Edit.svelte b/apps/web/src/routes/app/agents/Edit.svelte deleted file mode 100644 index d2ba382f..00000000 --- a/apps/web/src/routes/app/agents/Edit.svelte +++ /dev/null @@ -1,87 +0,0 @@ - - - - -
{ - return async ({ result }) => { - invalidateAll(); - state = 'idle'; - applyAction(result); - }; - }} - > - - - -
-
diff --git a/apps/web/src/routes/app/agents/UpdateForm.svelte b/apps/web/src/routes/app/agents/UpdateForm.svelte new file mode 100644 index 00000000..c083fc27 --- /dev/null +++ b/apps/web/src/routes/app/agents/UpdateForm.svelte @@ -0,0 +1,112 @@ + + +
+ + Update a new Agent + + You are about to update an Agent. Please fill out the form below. + + +

Basic Information

+ + + Title* + + + + + + + Description +