fix/modelos de IA
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
@@ -19,8 +19,7 @@ import {
|
|||||||
SelectValue,
|
SelectValue,
|
||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
import { useToast } from "@/hooks/use-toast";
|
import { useToast } from "@/hooks/use-toast";
|
||||||
import { createAgent } from "@/lib/api/agents";
|
import { createAgent, getAIModels } from "@/lib/api/agents";
|
||||||
import { AI_MODELS, getModelIdByValue } from "@/lib/constants/aiModels";
|
|
||||||
|
|
||||||
interface CreateAgentDialogProps {
|
interface CreateAgentDialogProps {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
@@ -32,7 +31,13 @@ export function CreateAgentDialog({ open, onOpenChange }: CreateAgentDialogProps
|
|||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [selectedModel, setSelectedModel] = useState("");
|
const [selectedModelId, setSelectedModelId] = useState("");
|
||||||
|
|
||||||
|
// Buscar modelos da API
|
||||||
|
const { data: aiModels = [], isLoading: isLoadingModels } = useQuery({
|
||||||
|
queryKey: ["aiModels"],
|
||||||
|
queryFn: getAIModels,
|
||||||
|
});
|
||||||
|
|
||||||
const createAgentMutation = useMutation({
|
const createAgentMutation = useMutation({
|
||||||
mutationFn: ({ name, modelId }: { name: string; modelId: number }) =>
|
mutationFn: ({ name, modelId }: { name: string; modelId: number }) =>
|
||||||
@@ -53,7 +58,7 @@ export function CreateAgentDialog({ open, onOpenChange }: CreateAgentDialogProps
|
|||||||
|
|
||||||
// Reset form
|
// Reset form
|
||||||
setName("");
|
setName("");
|
||||||
setSelectedModel("");
|
setSelectedModelId("");
|
||||||
},
|
},
|
||||||
onError: (error: any) => {
|
onError: (error: any) => {
|
||||||
toast({
|
toast({
|
||||||
@@ -65,17 +70,16 @@ export function CreateAgentDialog({ open, onOpenChange }: CreateAgentDialogProps
|
|||||||
});
|
});
|
||||||
|
|
||||||
const handleCreate = () => {
|
const handleCreate = () => {
|
||||||
const modelId = getModelIdByValue(selectedModel);
|
if (!selectedModelId) {
|
||||||
if (!modelId) {
|
|
||||||
toast({
|
toast({
|
||||||
title: "Erro",
|
title: "Erro",
|
||||||
description: "Modelo de IA inválido.",
|
description: "Selecione um modelo de IA.",
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
createAgentMutation.mutate({ name, modelId });
|
createAgentMutation.mutate({ name, modelId: parseInt(selectedModelId) });
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -101,13 +105,13 @@ export function CreateAgentDialog({ open, onOpenChange }: CreateAgentDialogProps
|
|||||||
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="model">Modelo de IA</Label>
|
<Label htmlFor="model">Modelo de IA</Label>
|
||||||
<Select value={selectedModel} onValueChange={setSelectedModel}>
|
<Select value={selectedModelId} onValueChange={setSelectedModelId} disabled={isLoadingModels}>
|
||||||
<SelectTrigger id="model">
|
<SelectTrigger id="model">
|
||||||
<SelectValue placeholder="Selecione um modelo" />
|
<SelectValue placeholder={isLoadingModels ? "Carregando modelos..." : "Selecione um modelo"} />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
{AI_MODELS.map((model) => (
|
{aiModels.filter(model => model.is_active === 1).map((model) => (
|
||||||
<SelectItem key={model.value} value={model.value}>
|
<SelectItem key={model.id} value={model.id.toString()}>
|
||||||
{model.name}
|
{model.name}
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
))}
|
))}
|
||||||
@@ -121,7 +125,7 @@ export function CreateAgentDialog({ open, onOpenChange }: CreateAgentDialogProps
|
|||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={handleCreate}
|
onClick={handleCreate}
|
||||||
disabled={!name || !selectedModel || createAgentMutation.isPending}
|
disabled={!name || !selectedModelId || createAgentMutation.isPending || isLoadingModels}
|
||||||
>
|
>
|
||||||
{createAgentMutation.isPending ? "Criando..." : "Criar Agente"}
|
{createAgentMutation.isPending ? "Criando..." : "Criar Agente"}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
+11
-1
@@ -1,8 +1,8 @@
|
|||||||
import api from "./axios";
|
import api from "./axios";
|
||||||
import type {
|
import type {
|
||||||
Agent,
|
|
||||||
AgentDetails,
|
AgentDetails,
|
||||||
AgentVersion,
|
AgentVersion,
|
||||||
|
AIModel,
|
||||||
CreateAgentRequest,
|
CreateAgentRequest,
|
||||||
CreateAgentResponse,
|
CreateAgentResponse,
|
||||||
EditAgentRequest,
|
EditAgentRequest,
|
||||||
@@ -149,3 +149,13 @@ export const getAgentVersions = async (
|
|||||||
);
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtém todos os modelos de IA disponíveis
|
||||||
|
*/
|
||||||
|
export const getAIModels = async (): Promise<AIModel[]> => {
|
||||||
|
const response = await api.get<AIModel[]>(
|
||||||
|
"/webhook/codex/get_models_ia"
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,33 +1,30 @@
|
|||||||
import { AIModel } from "../types/agent";
|
import { AIModel } from "../types/agent";
|
||||||
|
|
||||||
// Mapeamento de modelos de IA conforme a API
|
/**
|
||||||
// Nota: Os IDs precisam ser confirmados com a API real
|
* Este arquivo contém funções auxiliares para trabalhar com modelos de IA.
|
||||||
export const AI_MODELS: AIModel[] = [
|
* Os modelos agora são buscados dinamicamente da API através da função getAIModels() em @/lib/api/agents
|
||||||
{ id: 1, name: "Google Gemini 2.5 Pro", value: "google/gemini-2.5-pro" },
|
*
|
||||||
{ id: 2, name: "Google Gemini 2.5 Flash", value: "google/gemini-2.5-flash" },
|
* @deprecated AI_MODELS - Use getAIModels() da API ao invés disso
|
||||||
{ id: 3, name: "Google Gemini 2.5 Flash Lite", value: "google/gemini-2.5-flash-lite" },
|
*/
|
||||||
{ id: 4, name: "OpenAI GPT-5", value: "openai/gpt-5" },
|
|
||||||
{ id: 5, name: "OpenAI GPT-5 Mini", value: "openai/gpt-5-mini" },
|
|
||||||
{ id: 6, name: "OpenAI GPT-5 Nano", value: "openai/gpt-5-nano" },
|
|
||||||
{ id: 7, name: "Anthropic Claude Sonnet 4.5", value: "anthropic/claude-sonnet-4.5" },
|
|
||||||
{ id: 8, name: "Anthropic Claude Haiku 4.5", value: "anthropic/claude-haiku-4.5" },
|
|
||||||
{ id: 9, name: "Anthropic Claude Opus 4.5", value: "anthropic/claude-opus-4.5" },
|
|
||||||
{ id: 10, name: "Claude Haiku 4.5", value: "claude/haiku-4.5" },
|
|
||||||
];
|
|
||||||
|
|
||||||
// Função auxiliar para obter o nome do modelo pelo ID
|
// Função auxiliar para obter o nome do modelo pelo ID
|
||||||
export const getModelNameById = (id: number): string => {
|
export const getModelNameById = (models: AIModel[], id: number): string => {
|
||||||
const model = AI_MODELS.find((m) => m.id === id);
|
const model = models.find((m) => m.id === id);
|
||||||
return model?.name || "Modelo Desconhecido";
|
return model?.name || "Modelo Desconhecido";
|
||||||
};
|
};
|
||||||
|
|
||||||
// Função auxiliar para obter o modelo pelo value
|
// Função auxiliar para obter o modelo pelo identifier
|
||||||
export const getModelByValue = (value: string): AIModel | undefined => {
|
export const getModelByIdentifier = (models: AIModel[], identifier: string): AIModel | undefined => {
|
||||||
return AI_MODELS.find((m) => m.value === value);
|
return models.find((m) => m.model_identifier === identifier);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Função auxiliar para obter o ID pelo value
|
// Função auxiliar para obter o ID pelo identifier
|
||||||
export const getModelIdByValue = (value: string): number | undefined => {
|
export const getModelIdByIdentifier = (models: AIModel[], identifier: string): number | undefined => {
|
||||||
const model = getModelByValue(value);
|
const model = getModelByIdentifier(models, identifier);
|
||||||
return model?.id;
|
return model?.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Função auxiliar para filtrar apenas modelos ativos
|
||||||
|
export const getActiveModels = (models: AIModel[]): AIModel[] => {
|
||||||
|
return models.filter((m) => m.is_active === 1);
|
||||||
|
};
|
||||||
|
|||||||
@@ -115,6 +115,12 @@ export interface ListAgentsResponse {
|
|||||||
|
|
||||||
export interface AIModel {
|
export interface AIModel {
|
||||||
id: number;
|
id: number;
|
||||||
|
provider_id: number;
|
||||||
name: string;
|
name: string;
|
||||||
value: string;
|
model_identifier: string;
|
||||||
|
cost_input_per_million: string;
|
||||||
|
cost_output_per_million: string;
|
||||||
|
is_active: number;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-13
@@ -27,8 +27,8 @@ import {
|
|||||||
editAgentName,
|
editAgentName,
|
||||||
toggleAgentStatus,
|
toggleAgentStatus,
|
||||||
getAgentVersions,
|
getAgentVersions,
|
||||||
|
getAIModels,
|
||||||
} from "@/lib/api/agents";
|
} from "@/lib/api/agents";
|
||||||
import { AI_MODELS, getModelIdByValue } from "@/lib/constants/aiModels";
|
|
||||||
|
|
||||||
export default function AgentDetails() {
|
export default function AgentDetails() {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
@@ -40,9 +40,15 @@ export default function AgentDetails() {
|
|||||||
const [isDeactivateDialogOpen, setIsDeactivateDialogOpen] = useState(false);
|
const [isDeactivateDialogOpen, setIsDeactivateDialogOpen] = useState(false);
|
||||||
const [isEditingName, setIsEditingName] = useState(false);
|
const [isEditingName, setIsEditingName] = useState(false);
|
||||||
const [tempName, setTempName] = useState("");
|
const [tempName, setTempName] = useState("");
|
||||||
const [selectedModel, setSelectedModel] = useState("");
|
const [selectedModelId, setSelectedModelId] = useState<number | undefined>();
|
||||||
const [systemPrompt, setSystemPrompt] = useState("");
|
const [systemPrompt, setSystemPrompt] = useState("");
|
||||||
|
|
||||||
|
// Fetch AI models
|
||||||
|
const { data: aiModels = [], isLoading: isLoadingModels } = useQuery({
|
||||||
|
queryKey: ["aiModels"],
|
||||||
|
queryFn: getAIModels,
|
||||||
|
});
|
||||||
|
|
||||||
// Fetch agent details
|
// Fetch agent details
|
||||||
const {
|
const {
|
||||||
data: agentDetails,
|
data: agentDetails,
|
||||||
@@ -68,8 +74,7 @@ export default function AgentDetails() {
|
|||||||
// Update local state when agent details are loaded
|
// Update local state when agent details are loaded
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (agentDetails) {
|
if (agentDetails) {
|
||||||
const model = AI_MODELS.find((m) => m.id === agentDetails.model_id);
|
setSelectedModelId(agentDetails.model_id);
|
||||||
setSelectedModel(model?.value || "");
|
|
||||||
setSystemPrompt(agentDetails.system_prompt);
|
setSystemPrompt(agentDetails.system_prompt);
|
||||||
setTempName(agentDetails.name);
|
setTempName(agentDetails.name);
|
||||||
}
|
}
|
||||||
@@ -148,18 +153,17 @@ export default function AgentDetails() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const handleSave = (versionType: "major" | "minor" | "patch", notes: string) => {
|
const handleSave = (versionType: "major" | "minor" | "patch", notes: string) => {
|
||||||
const modelId = getModelIdByValue(selectedModel);
|
if (!selectedModelId) {
|
||||||
if (!modelId) {
|
|
||||||
toast({
|
toast({
|
||||||
title: "Erro",
|
title: "Erro",
|
||||||
description: "Modelo de IA inválido.",
|
description: "Selecione um modelo de IA.",
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
editAgentMutation.mutate({
|
editAgentMutation.mutate({
|
||||||
model_id: modelId,
|
model_id: selectedModelId,
|
||||||
system_prompt: systemPrompt,
|
system_prompt: systemPrompt,
|
||||||
version_type: versionType,
|
version_type: versionType,
|
||||||
notes,
|
notes,
|
||||||
@@ -341,15 +345,16 @@ export default function AgentDetails() {
|
|||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<Select
|
<Select
|
||||||
value={selectedModel}
|
value={selectedModelId?.toString()}
|
||||||
onValueChange={setSelectedModel}
|
onValueChange={(value) => setSelectedModelId(parseInt(value))}
|
||||||
|
disabled={isLoadingModels}
|
||||||
>
|
>
|
||||||
<SelectTrigger>
|
<SelectTrigger>
|
||||||
<SelectValue />
|
<SelectValue placeholder={isLoadingModels ? "Carregando modelos..." : "Selecione um modelo"} />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
{AI_MODELS.map((model) => (
|
{aiModels.filter(model => model.is_active === 1).map((model) => (
|
||||||
<SelectItem key={model.value} value={model.value}>
|
<SelectItem key={model.id} value={model.id.toString()}>
|
||||||
{model.name}
|
{model.name}
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user