Es la base del cobro por uso: hasta ahora no había ninguna medición de consumo en todo el repo. - UmindUso registra cada evento facturable con el costo YA calculado al precio vigente del plan. Congelarlo evita que subir un precio revalúe consumo pasado, que haría indefendible una factura ante un reclamo. - callAI devuelve los tokens que reportó el proveedor (campo usage, igual en todos los OpenAI-compatibles; input+output en Anthropic). Se mide cada ronda de tool-calling, no solo la última: todas gastan tokens. - ExtraerTextoOCR y TranscribirAudioSelfHosted reciben agenteID; 0 = no medir, que es lo que pasan los botones "Probar" del panel de staff. - Aviso al superar el tope del plan, una vez por mes y sin cortar el servicio. El flag de "ya avisé" es en memoria a propósito. - GET /app/umind/uso con filtros de fecha: resumen por tipo + detalle. - Test del cálculo de costo por tipo, incluida fracción de 1k tokens y tenant sin plan. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package controllers
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/services"
|
|
)
|
|
|
|
func OcrConfigPage(c *fiber.Ctx) error {
|
|
cfg, _ := models.GetOcrConfig()
|
|
return c.Render("ocr_config", fiber.Map{
|
|
"user": c.Locals("user"),
|
|
"modules": c.Locals("userModules"),
|
|
"cfg": cfg,
|
|
}, "layouts/main")
|
|
}
|
|
|
|
func GetOcrConfigHandler(c *fiber.Ctx) error {
|
|
cfg, err := models.GetOcrConfig()
|
|
if err != nil {
|
|
return c.JSON(fiber.Map{"data": nil})
|
|
}
|
|
return c.JSON(fiber.Map{"data": cfg})
|
|
}
|
|
|
|
func SaveOcrConfigHandler(c *fiber.Ctx) error {
|
|
type body struct {
|
|
ID uint `json:"id"`
|
|
BaseURL string `json:"base_url"`
|
|
Token string `json:"token"`
|
|
Notas string `json:"notas"`
|
|
}
|
|
var b body
|
|
if err := c.BodyParser(&b); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "body inválido"})
|
|
}
|
|
if b.BaseURL == "" {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "base_url es requerida"})
|
|
}
|
|
if b.Token == "" {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "token es requerido"})
|
|
}
|
|
|
|
cfg := models.OcrConfig{BaseURL: b.BaseURL, Token: b.Token, Notas: b.Notas}
|
|
cfg.ID = b.ID
|
|
if err := models.SaveOcrConfig(cfg); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"message": "Configuración guardada"})
|
|
}
|
|
|
|
// TestOcrConfigHandler recibe una imagen de prueba y devuelve el texto que
|
|
// extrae el servicio configurado — confirma que la URL/token funcionan de
|
|
// verdad, no solo que se guardaron.
|
|
func TestOcrConfigHandler(c *fiber.Ctx) error {
|
|
fh, err := c.FormFile("imagen")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "imagen requerida"})
|
|
}
|
|
if fh.Size > 8*1024*1024 {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "máximo 8MB"})
|
|
}
|
|
f, err := fh.Open()
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "no se pudo leer el archivo"})
|
|
}
|
|
defer f.Close()
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "no se pudo leer el archivo"})
|
|
}
|
|
|
|
mimeType := fh.Header.Get("Content-Type")
|
|
if mimeType == "" {
|
|
mimeType = "image/png"
|
|
}
|
|
texto, err := services.ExtraerTextoOCR(0, data, mimeType)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"text": texto})
|
|
}
|