@greatapps/greatagents-ui 0.3.21 → 0.3.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greatapps/greatagents-ui",
3
- "version": "0.3.21",
3
+ "version": "0.3.23",
4
4
  "description": "Shared agents UI components for Great platform",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -89,8 +89,8 @@ export function createGagentsClient(config: GagentsClientConfig) {
89
89
  idAccount: number,
90
90
  id: number,
91
91
  body: Partial<
92
- Pick<Agent, "title" | "photo" | "delay_typing" | "waiting_time" | "active">
93
- > & { prompt?: string; change_notes?: string },
92
+ Pick<Agent, "title" | "photo" | "delay_typing" | "waiting_time" | "active" | "identity" | "mission" | "tone_style_format" | "rules" | "conversation_flow" | "context">
93
+ > & { change_notes?: string },
94
94
  ) => request<Agent>("PUT", idAccount, `agents/${id}`, body),
95
95
 
96
96
  deleteAgent: (idAccount: number, id: number) =>
@@ -154,14 +154,14 @@ export function createGagentsClient(config: GagentsClientConfig) {
154
154
  idAccount: number,
155
155
  idAgent: number,
156
156
  body: Pick<Objective, "title"> &
157
- Partial<Pick<Objective, "slug" | "prompt" | "order" | "active">>,
157
+ Partial<Pick<Objective, "slug" | "instruction" | "description" | "conversation_flow" | "rules" | "order" | "active">>,
158
158
  ) => request<Objective>("POST", idAccount, `agents/${idAgent}/objectives`, body),
159
159
 
160
160
  updateObjective: (
161
161
  idAccount: number,
162
162
  idAgent: number,
163
163
  id: number,
164
- body: Partial<Pick<Objective, "title" | "slug" | "prompt" | "order" | "active">>,
164
+ body: Partial<Pick<Objective, "title" | "slug" | "instruction" | "description" | "conversation_flow" | "rules" | "order" | "active">>,
165
165
  ) => request<Objective>("PUT", idAccount, `agents/${idAgent}/objectives/${id}`, body),
166
166
 
167
167
  deleteObjective: (idAccount: number, idAgent: number, id: number) =>
@@ -0,0 +1,310 @@
1
+ import { useState, useRef, useCallback, useEffect, useMemo } from "react";
2
+ import type { Agent, ConversationFlowStep } from "../../types";
3
+ import type { GagentsHookConfig } from "../../hooks/types";
4
+ import { useUpdateAgent } from "../../hooks";
5
+ import { Button, Label } from "@greatapps/greatauth-ui/ui";
6
+ import { Loader2 } from "lucide-react";
7
+ import { toast } from "sonner";
8
+ import { ConversationFlowEditor } from "./conversation-flow-editor";
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Section config
12
+ // ---------------------------------------------------------------------------
13
+
14
+ interface SectionDef {
15
+ key: "identity" | "mission" | "tone_style_format" | "rules" | "conversation_flow" | "context";
16
+ label: string;
17
+ helper: string;
18
+ placeholder: string;
19
+ required?: boolean;
20
+ field: keyof Agent;
21
+ type: "textarea" | "conversation_flow";
22
+ }
23
+
24
+ const SECTIONS: SectionDef[] = [
25
+ {
26
+ key: "identity",
27
+ label: "Identidade",
28
+ helper: "Descreva quem é o agente, o seu nome e personalidade",
29
+ required: true,
30
+ field: "identity",
31
+ type: "textarea",
32
+ placeholder: "Você é a Ana, assistente virtual da Clínica Saúde & Bem-Estar. Você é simpática, profissional e sempre disposta a ajudar os pacientes...",
33
+ },
34
+ {
35
+ key: "mission",
36
+ label: "Missão",
37
+ helper: "Qual é a missão principal deste agente",
38
+ required: true,
39
+ field: "mission",
40
+ type: "textarea",
41
+ placeholder: "Sua missão é ajudar pacientes a agendar consultas, tirar dúvidas sobre horários e especialidades, e fornecer informações sobre a clínica...",
42
+ },
43
+ {
44
+ key: "tone_style_format",
45
+ label: "Tom, Estilo & Formato",
46
+ helper: "Defina como o agente comunica e formata as respostas",
47
+ required: false,
48
+ field: "tone_style_format",
49
+ type: "textarea",
50
+ placeholder: "Use tom empático e acolhedor. Responda de forma clara e objetiva, em no máximo 3 parágrafos. Use listas quando houver múltiplas opções...",
51
+ },
52
+ {
53
+ key: "rules",
54
+ label: "Regras",
55
+ helper: "Limites, restrições e comportamentos obrigatórios",
56
+ required: false,
57
+ field: "rules",
58
+ type: "textarea",
59
+ placeholder: "Nunca forneça diagnósticos médicos. Sempre recomende consulta presencial para urgências. Não agende consultas fora do horário de funcionamento (08h-18h)...",
60
+ },
61
+ {
62
+ key: "conversation_flow",
63
+ label: "Fluxo de Conversa",
64
+ helper: "Etapas que o agente segue no início de cada conversa",
65
+ required: false,
66
+ field: "conversation_flow",
67
+ type: "conversation_flow",
68
+ placeholder: "",
69
+ },
70
+ {
71
+ key: "context",
72
+ label: "Contexto",
73
+ helper: "Informações adicionais que o agente deve saber",
74
+ required: false,
75
+ field: "context",
76
+ type: "textarea",
77
+ placeholder: "A clínica funciona de segunda a sexta, das 08h às 18h. Especialidades disponíveis: Cardiologia, Dermatologia, Ortopedia, Pediatria...",
78
+ },
79
+ ];
80
+
81
+ // ---------------------------------------------------------------------------
82
+ // Helpers
83
+ // ---------------------------------------------------------------------------
84
+
85
+ function parseConversationFlow(raw: string | null | undefined): ConversationFlowStep[] {
86
+ if (!raw) return [];
87
+ try {
88
+ const parsed = JSON.parse(raw);
89
+ if (Array.isArray(parsed)) return parsed;
90
+ return [];
91
+ } catch {
92
+ return [];
93
+ }
94
+ }
95
+
96
+ function computeTotals(fields: Record<string, string>, steps: ConversationFlowStep[]) {
97
+ const textChars = Object.values(fields).reduce((sum, v) => sum + v.length, 0);
98
+ const flowChars = JSON.stringify(steps).length;
99
+ const total = textChars + flowChars;
100
+ return { chars: total, tokens: Math.ceil(total / 4) };
101
+ }
102
+
103
+ // ---------------------------------------------------------------------------
104
+ // Auto-resize textarea component
105
+ // ---------------------------------------------------------------------------
106
+
107
+ function AutoTextarea({
108
+ value,
109
+ onChange,
110
+ disabled,
111
+ placeholder,
112
+ ariaLabel,
113
+ }: {
114
+ value: string;
115
+ onChange: (v: string) => void;
116
+ disabled?: boolean;
117
+ placeholder?: string;
118
+ ariaLabel?: string;
119
+ }) {
120
+ const ref = useRef<HTMLTextAreaElement>(null);
121
+
122
+ const autoResize = useCallback(() => {
123
+ const el = ref.current;
124
+ if (!el) return;
125
+ el.style.height = "auto";
126
+ el.style.height = `${Math.max(120, el.scrollHeight)}px`;
127
+ }, []);
128
+
129
+ useEffect(() => {
130
+ autoResize();
131
+ }, [value, autoResize]);
132
+
133
+ function handleKeyDown(e: React.KeyboardEvent<HTMLTextAreaElement>) {
134
+ if (e.key === "Tab") {
135
+ e.preventDefault();
136
+ const el = e.currentTarget;
137
+ const start = el.selectionStart;
138
+ const end = el.selectionEnd;
139
+ const newValue = el.value.substring(0, start) + " " + el.value.substring(end);
140
+ onChange(newValue);
141
+ requestAnimationFrame(() => {
142
+ el.selectionStart = el.selectionEnd = start + 2;
143
+ });
144
+ }
145
+ }
146
+
147
+ return (
148
+ <textarea
149
+ ref={ref}
150
+ aria-label={ariaLabel}
151
+ value={value}
152
+ onChange={(e) => onChange(e.target.value)}
153
+ onKeyDown={handleKeyDown}
154
+ placeholder={placeholder}
155
+ disabled={disabled}
156
+ className="w-full resize-none rounded-lg border bg-background p-3 text-sm leading-relaxed focus:outline-none focus:ring-2 focus:ring-ring disabled:opacity-50"
157
+ style={{ minHeight: "120px" }}
158
+ />
159
+ );
160
+ }
161
+
162
+ // ---------------------------------------------------------------------------
163
+ // AgentDefinitionEditor
164
+ // ---------------------------------------------------------------------------
165
+
166
+ interface AgentDefinitionEditorProps {
167
+ agent: Agent;
168
+ config: GagentsHookConfig;
169
+ }
170
+
171
+ export function AgentDefinitionEditor({ agent, config }: AgentDefinitionEditorProps) {
172
+ const updateAgent = useUpdateAgent(config);
173
+
174
+ // Track agent ID to reset state on agent switch
175
+ const [trackedAgentId, setTrackedAgentId] = useState(agent.id);
176
+
177
+ // Text field state
178
+ const [fields, setFields] = useState<Record<string, string>>(() => ({
179
+ identity: agent.identity ?? "",
180
+ mission: agent.mission ?? "",
181
+ tone_style_format: agent.tone_style_format ?? "",
182
+ rules: agent.rules ?? "",
183
+ context: agent.context ?? "",
184
+ }));
185
+
186
+ // Conversation flow state
187
+ const [conversationFlowSteps, setConversationFlowSteps] = useState<ConversationFlowStep[]>(
188
+ () => parseConversationFlow(agent.conversation_flow),
189
+ );
190
+
191
+ // Reset state when agent changes
192
+ if (trackedAgentId !== agent.id) {
193
+ setTrackedAgentId(agent.id);
194
+ setFields({
195
+ identity: agent.identity ?? "",
196
+ mission: agent.mission ?? "",
197
+ tone_style_format: agent.tone_style_format ?? "",
198
+ rules: agent.rules ?? "",
199
+ context: agent.context ?? "",
200
+ });
201
+ setConversationFlowSteps(parseConversationFlow(agent.conversation_flow));
202
+ }
203
+
204
+ function updateField(key: string, value: string) {
205
+ setFields((prev) => ({ ...prev, [key]: value }));
206
+ }
207
+
208
+ const { chars, tokens } = computeTotals(fields, conversationFlowSteps);
209
+
210
+ const hasChanges = useMemo(() => {
211
+ if (fields.identity !== (agent.identity ?? "")) return true;
212
+ if (fields.mission !== (agent.mission ?? "")) return true;
213
+ if (fields.tone_style_format !== (agent.tone_style_format ?? "")) return true;
214
+ if (fields.rules !== (agent.rules ?? "")) return true;
215
+ if (fields.context !== (agent.context ?? "")) return true;
216
+ const originalSteps = parseConversationFlow(agent.conversation_flow);
217
+ if (JSON.stringify(conversationFlowSteps) !== JSON.stringify(originalSteps)) return true;
218
+ return false;
219
+ }, [fields, conversationFlowSteps, agent]);
220
+
221
+ function discard() {
222
+ setFields({
223
+ identity: agent.identity ?? "",
224
+ mission: agent.mission ?? "",
225
+ tone_style_format: agent.tone_style_format ?? "",
226
+ rules: agent.rules ?? "",
227
+ context: agent.context ?? "",
228
+ });
229
+ setConversationFlowSteps(parseConversationFlow(agent.conversation_flow));
230
+ }
231
+
232
+ async function handleSave() {
233
+ if (!fields.identity.trim() || !fields.mission.trim()) {
234
+ toast.error("Identidade e Missão são campos obrigatórios");
235
+ return;
236
+ }
237
+
238
+ const body: Record<string, unknown> = {
239
+ identity: fields.identity.trim(),
240
+ mission: fields.mission.trim(),
241
+ tone_style_format: fields.tone_style_format.trim() || null,
242
+ rules: fields.rules.trim() || null,
243
+ conversation_flow: conversationFlowSteps.length > 0
244
+ ? JSON.stringify(conversationFlowSteps)
245
+ : null,
246
+ context: fields.context.trim() || null,
247
+ };
248
+
249
+ try {
250
+ await updateAgent.mutateAsync({ id: agent.id, body });
251
+ toast.success("Definição do agente salva com sucesso");
252
+ } catch {
253
+ toast.error("Erro ao salvar definição do agente");
254
+ }
255
+ }
256
+
257
+ return (
258
+ <div className="space-y-6 p-4">
259
+ {/* Sections */}
260
+ {SECTIONS.map((section) => (
261
+ <div key={section.key} className="space-y-2">
262
+ <Label className="text-sm font-medium">
263
+ {section.label}
264
+ {section.required && <span className="ml-0.5 text-destructive">*</span>}
265
+ </Label>
266
+ <p className="text-xs text-muted-foreground">{section.helper}</p>
267
+
268
+ {section.type === "textarea" ? (
269
+ <AutoTextarea
270
+ value={fields[section.key] ?? ""}
271
+ onChange={(v) => updateField(section.key, v)}
272
+ disabled={updateAgent.isPending}
273
+ placeholder={section.placeholder}
274
+ ariaLabel={section.label}
275
+ />
276
+ ) : (
277
+ <ConversationFlowEditor
278
+ steps={conversationFlowSteps}
279
+ onChange={setConversationFlowSteps}
280
+ disabled={updateAgent.isPending}
281
+ />
282
+ )}
283
+ </div>
284
+ ))}
285
+
286
+ {/* Character count + token estimate */}
287
+ <div className="flex items-center gap-3 text-xs text-muted-foreground">
288
+ <span className="tabular-nums">{chars.toLocaleString("pt-BR")} caracteres</span>
289
+ <span>·</span>
290
+ <span className="tabular-nums">~{tokens.toLocaleString("pt-BR")} tokens</span>
291
+ </div>
292
+
293
+ {/* Sticky save/discard bar */}
294
+ {hasChanges && (
295
+ <div className="sticky bottom-0 z-10 flex items-center justify-between gap-2 rounded-lg border bg-background p-3 shadow-sm">
296
+ <p className="text-sm text-muted-foreground">Você tem alterações não salvas.</p>
297
+ <div className="flex gap-2">
298
+ <Button variant="ghost" size="sm" onClick={discard} disabled={updateAgent.isPending}>
299
+ Descartar
300
+ </Button>
301
+ <Button size="sm" onClick={handleSave} disabled={updateAgent.isPending}>
302
+ {updateAgent.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
303
+ Salvar
304
+ </Button>
305
+ </div>
306
+ </div>
307
+ )}
308
+ </div>
309
+ );
310
+ }
@@ -7,11 +7,11 @@ import {
7
7
  Input,
8
8
  Label,
9
9
  Switch,
10
- Dialog,
11
- DialogContent,
12
- DialogHeader,
13
- DialogTitle,
14
- DialogFooter,
10
+ Sheet,
11
+ SheetContent,
12
+ SheetHeader,
13
+ SheetTitle,
14
+ SheetFooter,
15
15
  } from "@greatapps/greatauth-ui/ui";
16
16
  import { Loader2 } from "lucide-react";
17
17
  import { toast } from "sonner";
@@ -185,7 +185,7 @@ export function AgentEditForm({ config, agent, idAccount, open, onOpenChange }:
185
185
  </div>
186
186
  </div>
187
187
 
188
- <DialogFooter>
188
+ <SheetFooter>
189
189
  <Button
190
190
  type="button"
191
191
  variant="outline"
@@ -200,20 +200,20 @@ export function AgentEditForm({ config, agent, idAccount, open, onOpenChange }:
200
200
  )}
201
201
  Salvar
202
202
  </Button>
203
- </DialogFooter>
203
+ </SheetFooter>
204
204
  </form>
205
205
  );
206
206
 
207
207
  if (open !== undefined && onOpenChange) {
208
208
  return (
209
- <Dialog open={open} onOpenChange={onOpenChange}>
210
- <DialogContent className="sm:max-w-md">
211
- <DialogHeader>
212
- <DialogTitle>Editar Agente</DialogTitle>
213
- </DialogHeader>
209
+ <Sheet open={open} onOpenChange={onOpenChange}>
210
+ <SheetContent className="sm:max-w-md overflow-y-auto">
211
+ <SheetHeader>
212
+ <SheetTitle>Editar Agente</SheetTitle>
213
+ </SheetHeader>
214
214
  {formContent}
215
- </DialogContent>
216
- </Dialog>
215
+ </SheetContent>
216
+ </Sheet>
217
217
  );
218
218
  }
219
219
 
@@ -3,11 +3,11 @@ import { useCreateAgent, useUpdateAgent } from "../../hooks";
3
3
  import type { Agent } from "../../types";
4
4
  import type { GagentsHookConfig } from "../../hooks/types";
5
5
  import {
6
- Dialog,
7
- DialogContent,
8
- DialogHeader,
9
- DialogTitle,
10
- DialogFooter,
6
+ Sheet,
7
+ SheetContent,
8
+ SheetHeader,
9
+ SheetTitle,
10
+ SheetFooter,
11
11
  Button,
12
12
  Input,
13
13
  Label,
@@ -134,13 +134,13 @@ export function AgentFormDialog({
134
134
  }
135
135
 
136
136
  return (
137
- <Dialog open={open} onOpenChange={onOpenChange}>
138
- <DialogContent className="sm:max-w-lg">
139
- <DialogHeader>
140
- <DialogTitle>
137
+ <Sheet open={open} onOpenChange={onOpenChange}>
138
+ <SheetContent className="sm:max-w-lg overflow-y-auto">
139
+ <SheetHeader>
140
+ <SheetTitle>
141
141
  {isEditing ? "Editar Agente" : "Novo Agente"}
142
- </DialogTitle>
143
- </DialogHeader>
142
+ </SheetTitle>
143
+ </SheetHeader>
144
144
  <form onSubmit={handleSubmit} className="space-y-4">
145
145
  <div className="flex justify-center">
146
146
  <ImageCropUpload
@@ -224,7 +224,7 @@ export function AgentFormDialog({
224
224
  </p>
225
225
  </div>
226
226
  </div>
227
- <DialogFooter>
227
+ <SheetFooter>
228
228
  <Button
229
229
  type="button"
230
230
  variant="outline"
@@ -239,9 +239,9 @@ export function AgentFormDialog({
239
239
  ) : null}
240
240
  {isEditing ? "Salvar" : "Criar"}
241
241
  </Button>
242
- </DialogFooter>
242
+ </SheetFooter>
243
243
  </form>
244
- </DialogContent>
245
- </Dialog>
244
+ </SheetContent>
245
+ </Sheet>
246
246
  );
247
247
  }