forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathagent-creator.tsx
118 lines (110 loc) · 3.5 KB
/
agent-creator.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import CharacterForm from '@/components/character-form';
import { useToast } from '@/hooks/use-toast';
import { apiClient } from '@/lib/api';
import type { Agent } from '@elizaos/core';
import { useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import AvatarPanel from './avatar-panel';
import PluginsPanel from './plugins-panel';
import { SecretPanel } from './secret-panel';
import { useAgentUpdate } from '@/hooks/use-agent-update';
// Define a partial agent for initialization
const defaultCharacter: Partial<Agent> = {
name: '',
username: '',
system: '',
bio: [] as string[],
topics: [] as string[],
adjectives: [] as string[],
plugins: ['@elizaos/plugin-sql', '@elizaos/plugin-local-ai'],
settings: { secrets: {} },
};
export default function AgentCreator() {
const navigate = useNavigate();
const { toast } = useToast();
const queryClient = useQueryClient();
const [initialCharacter] = useState<Partial<Agent>>({
...defaultCharacter,
});
// Use agent update hook for proper handling of nested fields
const agentState = useAgentUpdate(initialCharacter as Agent);
const ensureRequiredFields = (character: Agent): Agent => {
return {
...character,
bio: character.bio ?? [],
messageExamples: character.messageExamples ?? [],
postExamples: character.postExamples ?? [],
topics: character.topics ?? [],
adjectives: character.adjectives ?? [],
plugins: character.plugins ?? [],
style: {
all: character.style?.all ?? [],
chat: character.style?.chat ?? [],
post: character.style?.post ?? [],
},
settings: character.settings ?? { secrets: {} },
};
};
const handleSubmit = async (character: Agent) => {
try {
const completeCharacter = ensureRequiredFields(character);
await apiClient.createAgent({
characterJson: completeCharacter,
});
// Invalidate the characters query to refresh the characters list
queryClient.invalidateQueries({ queryKey: ['characters'] });
toast({
title: 'Success',
description: 'Character created successfully!',
});
queryClient.invalidateQueries({ queryKey: ['agents'] });
navigate('/');
} catch (error) {
toast({
title: 'Error',
description: error instanceof Error ? error.message : 'Failed to create character',
variant: 'destructive',
});
}
};
return (
<CharacterForm
characterValue={agentState.agent}
setCharacterValue={agentState}
title="Character Settings"
description="Configure your AI character's behavior and capabilities"
onSubmit={handleSubmit}
onReset={() => agentState.reset()}
onDelete={() => {
navigate('/');
}}
isAgent={true}
customComponents={[
{
name: 'Plugins',
component: (
<PluginsPanel characterValue={agentState.agent} setCharacterValue={agentState} />
),
},
{
name: 'Secret',
component: (
<SecretPanel
characterValue={agentState.agent}
onChange={(updatedAgent) => {
agentState.updateSettings(updatedAgent.settings);
}}
/>
),
},
{
name: 'Avatar',
component: (
<AvatarPanel characterValue={agentState.agent} setCharacterValue={agentState} />
),
},
]}
/>
);
}