Source-only publication of reviewed commit 64e96a7f423673dc29ad56485be9fc79bce8af1c.
Passed in isolation: focused frontend/backend tests, race checks, TypeScript typecheck, lint, Go compilation, Flowgram build, and deterministic SDK build/tests.
Not accepted as a production release: Next.js export/native assembly remains blocked by environment memory limits; browser interaction and MySQL staging acceptance remain unverified. No production server, database/schema, runtime configuration, deployment, release tag, or workflow execution was performed.
Expected source tree: 0a4a7528e9.
125 lines
3.7 KiB
Go
125 lines
3.7 KiB
Go
// Modified in the independent AgentDesk fork: localized knowledge workflows and safe display errors.
|
|
package dashboard
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"agent-desk/internal/ai/rag"
|
|
"agent-desk/internal/pkg/constants"
|
|
"agent-desk/internal/pkg/dto/request"
|
|
"agent-desk/internal/pkg/errorsx"
|
|
"agent-desk/internal/pkg/httpx"
|
|
"agent-desk/internal/pkg/httpx/params"
|
|
"agent-desk/internal/pkg/i18nx"
|
|
"agent-desk/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func KnowledgeRetrievePostDebugSearch(ctx *gin.Context) {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.KnowledgeSearchRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
resp, err := rag.Answer.DebugSearch(knowledgeRequestContext(ctx), req)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, safeKnowledgeOperationError(err, "error.knowledge.retrieveFailed"))
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, resp)
|
|
}
|
|
|
|
func KnowledgeRetrievePostDebugAnswer(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.KnowledgeAnswerRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
resp, err := rag.Answer.DebugAnswer(knowledgeRequestContext(ctx), req, operator)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, safeKnowledgeOperationError(err, "error.knowledge.retrieveFailed"))
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, resp)
|
|
}
|
|
|
|
func KnowledgeRetrievePostBuild(ctx *gin.Context) {
|
|
req := struct {
|
|
DocumentID int64 `json:"documentId"`
|
|
FAQID int64 `json:"faqId"`
|
|
}{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
if req.DocumentID > 0 {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentUpdate); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := rag.Answer.BuildDocumentIndex(knowledgeRequestContext(ctx), req.DocumentID); err != nil {
|
|
httpx.WriteJSON(ctx, safeKnowledgeOperationError(err, "error.knowledge.indexFailed"))
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|
|
|
|
if req.FAQID > 0 {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQUpdate); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := rag.Index.IndexFAQByID(knowledgeRequestContext(ctx), req.FAQID); err != nil {
|
|
httpx.WriteJSON(ctx, safeKnowledgeOperationError(err, "error.knowledge.indexFailed"))
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|
|
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0066"))
|
|
}
|
|
|
|
func knowledgeRequestContext(ctx *gin.Context) context.Context {
|
|
parent := context.Background()
|
|
if ctx != nil && ctx.Request != nil {
|
|
parent = ctx.Request.Context()
|
|
}
|
|
return i18nx.WithLocale(parent, i18nx.Locale(ctx))
|
|
}
|
|
|
|
// Only catalog-owned errors without arbitrary diagnostic arguments are safe to
|
|
// return from index/embedding/vector providers. Unwrap the application error so
|
|
// a surrounding SQL/provider message never reaches the response.
|
|
func safeKnowledgeOperationError(err error, fallbackKey string) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var appErr *errorsx.I18nError
|
|
if errors.As(err, &appErr) && appErr != nil && len(appErr.Args) == 0 && i18nx.Get(appErr.Key) != appErr.Key {
|
|
return appErr
|
|
}
|
|
var localizedErr *i18nx.Error
|
|
if errors.As(err, &localizedErr) && localizedErr != nil && len(localizedErr.Args) == 0 && i18nx.Get(localizedErr.Key) != localizedErr.Key {
|
|
return i18nx.Errorf(localizedErr.Key)
|
|
}
|
|
return errorsx.BusinessErrorI18n(0, fallbackKey)
|
|
}
|