1import{a as D,u as M,b as U,j as e}from"./vendor-query-CmFN4-M1.js";import{r as m}from"./vendor-react-7PyNDetJ.js";import{aK as q,av as x,aG as h,aD as C,aE as _,I as S,p as E,ab as l,b as y,c as R}from"./index-BZSaBQD0.js";import{T as $,a as k,b as F,c as n,d as L,e as c}from"./table-BEgIk-tI.js";import{u as I}from"./useAICosts-DVQ2xyMS.js";import{aJ as P,i as z}from"./vendor-ui-DjI1iuvA.js";import"./vendor-supabase-BfoV2C50.js";const g=.39,w=5;function H(o){return o>=.7?e.jsxs(l,{className:"bg-emerald-600 hover:bg-emerald-600",children:[(o*100).toFixed(0),"%"]}):o>=.5?e.jsxs(l,{className:"bg-amber-500 hover:bg-amber-500",children:[(o*100).toFixed(0),"%"]}):o>=.3?e.jsxs(l,{className:"bg-orange-600 hover:bg-orange-600",children:[(o*100).toFixed(0),"%"]}):e.jsxs(l,{variant:"destructive",children:[(o*100).toFixed(0),"%"]})}function X(){const o=D(),[T,A]=m.useState({}),{data:j,isLoading:B}=M({staleTime:6e4,refetchOnWindowFocus:!1,queryKey:["ai-prompt-configs-credit"],queryFn:async()=>{const{data:s,error:a}=await y.from("ai_prompt_configs").select("id, function_name, display_name, credit_cost, is_active, model_id, updated_at").eq("is_active",!0).order("function_name");if(a)throw a;return s??[]}}),{data:p}=I("30d","user"),f=m.useMemo(()=>{const s=new Map;for(const t of p??[]){const r=s.get(t.feature)??{execs:0,usd:0};r.execs+=Number(t.executions||0),r.usd+=Number(t.sum_cost_usd||0),s.set(t.feature,r)}const a=new Map;for(const[t,r]of s)a.set(t,{execs:r.execs,avgUsd:r.execs>0?r.usd/r.execs:0});return a},[p]),b=U({mutationFn:async({id:s,credit_cost:a})=>{const{error:t}=await y.from("ai_prompt_configs").update({credit_cost:a}).eq("id",s);if(t)throw t},onSuccess:()=>{R.success("Custo atualizado"),o.invalidateQueries({queryKey:["ai-prompt-configs-credit"]})},onError:s=>R.error((s==null?void 0:s.message)??"Falha ao atualizar")}),v=m.useMemo(()=>(j??[]).map(s=>{const a=f.get(s.function_name),t=(a==null?void 0:a.avgUsd)??0,r=(a==null?void 0:a.execs)??0,d=t*w,i=(s.credit_cost??0)*g,u=i>0?(i-d)/i:null;return{...s,execs:r,costBrl:d,revenueBrl:i,margin:u}}),[j,f]),N=v.filter(s=>s.margin!==null&&s.margin<.5).length;return e.jsx(q,{children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-3xl font-bold flex items-center gap-2",children:[e.jsx(P,{className:"h-7 w-7 text-primary"}),"Custo de IA por Função (RADC)"]}),e.jsxs("p",{className:"text-muted-foreground mt-1",children:["Edite quantos RADC cada função cobra do usuário. Mudanças são instantâneas (a próxima chamada já usa o novo valor). Margem comparada a R$ ",g.toFixed(2),"/RADC (top-up avulso) e custo real médio dos últimos 30 dias."]})]}),N>0&&e.jsx(x,{className:"border-orange-500/40 bg-orange-500/5",children:e.jsxs(h,{className:"py-4 flex items-center gap-3",children:[e.jsx(z,{className:"h-5 w-5 text-orange-500"}),e.jsxs("span",{className:"text-sm",children:[e.jsx("strong",{children:N})," função(ões) com margem abaixo de 50% â revisar custo ou modelo."]})]})}),e.jsxs(x,{children:[e.jsx(C,{children:e.jsx(_,{children:"Funções ativas"})}),e.jsx(h,{children:B?e.jsx("p",{className:"text-muted-foreground",children:"Carregandoâ¦"}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs($,{children:[e.jsx(k,{children:e.jsxs(F,{children:[e.jsx(n,{children:"Função"}),e.jsx(n,{className:"text-right",children:"Chamadas 30d"}),e.jsx(n,{className:"text-right",children:"Custo real / chamada"}),e.jsx(n,{className:"text-right",children:"Receita / chamada"}),e.jsx(n,{className:"text-right",children:"Margem"}),e.jsx(n,{className:"text-right w-40",children:"RADC cobrado"}),e.jsx(n,{})]})}),e.jsx(L,{children:v.map(s=>{const a=T[s.id],t=s.credit_cost??0,r=a??t,d=a!==void 0&&a!==t;return e.jsxs(F,{children:[e.jsxs(c,{children:[e.jsx("div",{className:"font-medium",children:s.display_name||s.function_name}),e.jsx("div",{className:"text-xs text-muted-foreground font-mono",children:s.function_name})]}),e.jsx(c,{className:"text-right tabular-nums",children:s.execs.toLocaleString("pt-BR")}),e.jsx(c,{className:"text-right tabular-nums",children:s.execs>0?`R$ ${s.costBrl.toFixed(4)}`:"â"}),e.jsxs(c,{className:"text-right tabular-nums",children:["R$ ",s.revenueBrl.toFixed(2)]}),e.jsx(c,{className:"text-right",children:s.margin===null||s.execs===0?"â":H(s.margin)}),e.jsx(c,{className:"text-right",children:e.jsx(S,{type:"number",min:0,step:1,value:r,onChange:i=>A(u=>({...u,[s.id]:Math.max(0,Number(i.target.value)||0)})),className:"w-24 text-right ml-auto"})}),e.jsx(c,{children:e.jsx(E,{size:"sm",disabled:!d||b.isPending,onClick:()=>b.mutate({id:s.id,credit_cost:r}),children:"Salvar"})})]},s.id)})})]})})})]}),e.jsxs(x,{children:[e.jsx(C,{children:e.jsx(_,{children:"Como ler esta tabela"})}),e.jsxs(h,{className:"text-sm text-muted-foreground space-y-2",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Receita / chamada"})," = ",e.jsxs("code",{children:["RADC cobrado à R$ ",g.toFixed(2)]})," (preço do top-up avulso)."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Custo real / chamada"})," = média USD em ",e.jsx("code",{children:"ai_llm_call_log"})," dos últimos 30 dias, apenas ",e.jsx("code",{children:"caller_kind = user"}),", convertido a R$ ",w.toFixed(2),"/USD."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Margem ⥠70%"})," = saudável ⢠",e.jsx("strong",{children:"50â70%"})," = ok ⢠",e.jsx("strong",{children:"30â50%"})," = revisar ⢠",e.jsx("strong",{children:"< 30%"})," = aumentar RADC ou trocar modelo."]}),e.jsxs("p",{children:["Tomo cobra ",e.jsx("strong",{children:"1 RADC por turno"})," (função ",e.jsx("code",{children:"radreport-agent"}),"). Tools internas cobram delta adicional. Use ",e.jsx("code",{children:"credit_cost = 0"})," para funções operacionais (memory-extractor, sub-agentes Tomo)."]})]})]})]})})}export{X as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.