aimodel_settings.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "net/http"
  4. prout "imuslab.com/arozos/mod/prouter"
  5. "imuslab.com/arozos/mod/utils"
  6. )
  7. /*
  8. AI Model Settings Manager
  9. Registers the "AI Model" tab in System Settings > AI Integration and
  10. exposes the admin-only endpoints used to configure the AI endpoint
  11. (OpenAI- or Anthropic-compatible), the global API key, per-model pricing,
  12. a usage quota and to view / reset the aggregated token & cost metrics.
  13. The AGI "aimodel" library (mod/agi/agi.aimodel.go) consumes the same
  14. configuration and writes the usage metrics that this page displays.
  15. GET /system/aimodel/config – masked connection config
  16. POST /system/aimodel/config – save connection config
  17. GET /system/aimodel/pricing – per-model pricing map
  18. POST /system/aimodel/pricing – save per-model pricing map
  19. GET /system/aimodel/quota – usage quota + current window usage
  20. POST /system/aimodel/quota – save usage quota
  21. GET /system/aimodel/metrics – aggregated usage metrics
  22. POST /system/aimodel/metrics/reset – reset usage metrics
  23. POST /system/aimodel/test – connectivity test (lists models)
  24. All endpoints require administrator privileges.
  25. */
  26. func AIModelSettingInit() {
  27. //Register the settings tab in the "AI Integration" group.
  28. registerSetting(settingModule{
  29. Name: "AI Model",
  30. Desc: "Configure an OpenAI / Anthropic compatible endpoint, pricing, quota and view token usage",
  31. IconPath: "SystemAO/system_setting/img/ai.svg",
  32. Group: "AInteg",
  33. StartDir: "SystemAO/advance/aimodel.html",
  34. RequireAdmin: true,
  35. })
  36. //Admin-only router. The AI configuration contains a sensitive API key, so
  37. //every endpoint here is restricted to administrators.
  38. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  39. ModuleName: "System Settings",
  40. AdminOnly: true,
  41. UserHandler: userHandler,
  42. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  43. utils.SendErrorResponse(w, "Permission Denied")
  44. },
  45. })
  46. adminRouter.HandleFunc("/system/aimodel/config", AGIGateway.HandleAIModelConfig)
  47. adminRouter.HandleFunc("/system/aimodel/pricing", AGIGateway.HandleAIModelPricing)
  48. adminRouter.HandleFunc("/system/aimodel/quota", AGIGateway.HandleAIModelQuota)
  49. adminRouter.HandleFunc("/system/aimodel/metrics", AGIGateway.HandleAIModelMetrics)
  50. adminRouter.HandleFunc("/system/aimodel/metrics/reset", AGIGateway.HandleAIModelMetricsReset)
  51. adminRouter.HandleFunc("/system/aimodel/test", AGIGateway.HandleAIModelTest)
  52. }