Go: add context, part9 (#17412)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-07-27 15:06:48 +08:00
committed by GitHub
parent a6a67c5ece
commit 3065a29935
37 changed files with 426 additions and 411 deletions

View File

@@ -178,6 +178,7 @@ func (h *ChatHandler) MindMap(c *gin.Context) {
return
}
ctx := c.Request.Context()
searchConfig := map[string]interface{}{}
modelTenantID := user.ID
if req.SearchID != "" {
@@ -185,7 +186,7 @@ func (h *ChatHandler) MindMap(c *gin.Context) {
jsonInternalError(c, fmt.Errorf("search service not configured"))
return
}
detail, err := h.searchSvc.GetDetail(req.SearchID)
detail, err := h.searchSvc.GetDetail(ctx, req.SearchID)
if err != nil {
jsonInternalError(c, err)
return
@@ -202,7 +203,6 @@ func (h *ChatHandler) MindMap(c *gin.Context) {
return
}
ctx := c.Request.Context()
mindMap, err := runMindMap(ctx, mindMapRunConfig{
Question: req.Question,
KbIDs: kbIDs,

View File

@@ -72,7 +72,7 @@ func (h *ChatHandler) ChatAudioSpeech(c *gin.Context) {
return
}
driver, modelName, apiConfig, _, err := h.llm.GetTenantDefaultModelByType(user.ID, entity.ModelTypeTTS)
driver, modelName, apiConfig, _, err := h.llm.GetTenantDefaultModelByType(ctx, user.ID, entity.ModelTypeTTS)
if err != nil {
common.ErrorWithCode(c, common.CodeDataError, err.Error())
return
@@ -201,7 +201,7 @@ func (h *ChatHandler) ChatAudioTranscription(c *gin.Context) {
return
}
driver, modelName, apiConfig, _, err := h.llm.GetTenantDefaultModelByType(user.ID, entity.ModelTypeSpeech2Text)
driver, modelName, apiConfig, _, err := h.llm.GetTenantDefaultModelByType(ctx, user.ID, entity.ModelTypeSpeech2Text)
if err != nil {
common.ErrorWithCode(c, common.CodeDataError, err.Error())
return

View File

@@ -128,7 +128,8 @@ func (h *SearchHandler) ListSearches(c *gin.Context) {
}
// List search apps with filtering
result, err := h.searchService.ListSearches(userID, keywords, page, pageSize, orderby, desc, ownerIDs)
ctx := c.Request.Context()
result, err := h.searchService.ListSearches(ctx, userID, keywords, page, pageSize, orderby, desc, ownerIDs)
if err != nil {
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 500, nil, err.Error())
return
@@ -174,7 +175,8 @@ func (h *SearchHandler) CreateSearch(c *gin.Context) {
}
// Create search (same as Python SearchService.save within DB.atomic())
result, err := h.searchService.CreateSearch(userID, req.Name, req.Description)
ctx := c.Request.Context()
result, err := h.searchService.CreateSearch(ctx, userID, req.Name, req.Description)
if err != nil {
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, common.CodeBadRequest, nil, err.Error())
return
@@ -210,7 +212,8 @@ func (h *SearchHandler) GetSearch(c *gin.Context) {
}
// Get search detail with permission check
search, err := h.searchService.GetSearchDetail(userID, searchID)
ctx := c.Request.Context()
search, err := h.searchService.GetSearchDetail(ctx, userID, searchID)
if err != nil {
// Check if it's a permission error
if err.Error() == "has no permission for this operation" {
@@ -268,7 +271,8 @@ func (h *SearchHandler) DeleteSearch(c *gin.Context) {
}
// Delete search with permission check
err := h.searchService.DeleteSearch(userID, searchID)
ctx := c.Request.Context()
err := h.searchService.DeleteSearch(ctx, userID, searchID)
if err != nil {
// Check if it's an authorization error
if err.Error() == "no authorization" {
@@ -324,7 +328,8 @@ func (h *SearchHandler) UpdateSearch(c *gin.Context) {
}
// Update search
updatedSearch, err := h.searchService.UpdateSearch(userID, searchID, &req)
ctx := c.Request.Context()
updatedSearch, err := h.searchService.UpdateSearch(ctx, userID, searchID, &req)
if err != nil {
errMsg := err.Error()
switch errMsg {

View File

@@ -250,7 +250,8 @@ func (h *SearchBotHandler) Ask(c *gin.Context) {
// Resolve chat model ID.
modelID := ""
if req.SearchID != "" && h.searchSvc != nil {
if detail, err := h.searchSvc.GetDetail(req.SearchID); err == nil {
ctx := c.Request.Context()
if detail, err := h.searchSvc.GetDetail(ctx, req.SearchID); err == nil {
if sc, ok := detail["search_config"].(map[string]interface{}); ok {
if cid, ok := sc["chat_id"].(string); ok && cid != "" {
modelID = cid
@@ -350,7 +351,8 @@ func (h *SearchBotHandler) MindMap(c *gin.Context) {
jsonInternalError(c, fmt.Errorf("search service not configured"))
return
}
detail, err := h.searchSvc.GetDetail(req.SearchID)
ctx := c.Request.Context()
detail, err := h.searchSvc.GetDetail(ctx, req.SearchID)
if err != nil {
jsonInternalError(c, err)
return
@@ -395,8 +397,7 @@ func (h *SearchBotHandler) SearchBotDetail(c *gin.Context) {
common.ResponseWithCodeData(c, code, nil, "Authentication error: API key is invalid!")
return
}
detail, err := h.searchSvc.GetSearchShareDetail(user.ID, searchID)
detail, err := h.searchSvc.GetSearchShareDetail(ctx, user.ID, searchID)
if err != nil {
switch err.Error() {
case "has no permission for this operation":

View File

@@ -51,8 +51,6 @@ func NewSkillSearchHandler(docEngine engine.DocEngine, spaceRemover file.DocRemo
// @Summary Get Skill Search Config
// @Description Get the search configuration for skills
// @Tags skill-search
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param embd_id query string true "Embedding Model ID"
// @Param space_id query string false "Skill Space ID"
@@ -68,7 +66,8 @@ func (h *SkillSearchHandler) GetConfig(c *gin.Context) {
embdID := c.Query("embd_id")
spaceID := c.Query("space_id")
result, code, err := h.searchService.GetConfig(user.ID, spaceID, embdID)
ctx := c.Request.Context()
result, code, err := h.searchService.GetConfig(ctx, user.ID, spaceID, embdID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -81,8 +80,6 @@ func (h *SkillSearchHandler) GetConfig(c *gin.Context) {
// @Summary Update Skill Search Config
// @Description Update the search configuration for skills
// @Tags skill-search
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param request body service.UpdateConfigRequest true "config info"
// @Success 200 {object} map[string]interface{}
@@ -102,7 +99,8 @@ func (h *SkillSearchHandler) UpdateConfig(c *gin.Context) {
req.TenantID = user.ID
result, code, err := h.searchService.UpdateConfig(&req)
ctx := c.Request.Context()
result, code, err := h.searchService.UpdateConfig(ctx, &req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -115,8 +113,6 @@ func (h *SkillSearchHandler) UpdateConfig(c *gin.Context) {
// @Summary Search Skills
// @Description Search skills using configured search strategy
// @Tags skill-search
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param request body service.SearchRequest true "search query"
// @Success 200 {object} map[string]interface{}
@@ -156,8 +152,6 @@ type IndexSkillsRequest struct {
// @Summary Index Skills
// @Description Index skills for search. If embd_id is not provided, will use the one from skill search config.
// @Tags skill-search
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param request body IndexSkillsRequest true "skills to index"
// @Success 200 {object} map[string]interface{}
@@ -178,7 +172,8 @@ func (h *SkillSearchHandler) IndexSkills(c *gin.Context) {
// If embd_id not provided, get from skill search config
embdID := req.EmbdID
if embdID == "" {
config, code, err := h.searchService.GetConfig(user.ID, req.SpaceID, "")
ctx := c.Request.Context()
config, code, err := h.searchService.GetConfig(ctx, user.ID, req.SpaceID, "")
if err != nil {
common.ResponseWithCodeData(c, code, nil, "failed to get skill search config: "+err.Error())
return
@@ -231,8 +226,6 @@ type ReindexRequest struct {
// @Summary Reindex All Skills
// @Description Reindex all skills for a tenant. If embd_id is not provided, will use the one from skill search config.
// @Tags skill-search
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param request body ReindexRequest true "skills to reindex"
// @Success 200 {object} map[string]interface{}
@@ -253,7 +246,8 @@ func (h *SkillSearchHandler) Reindex(c *gin.Context) {
// If embd_id not provided, get from skill search config
embdID := req.EmbdID
if embdID == "" {
config, code, err := h.searchService.GetConfig(user.ID, req.SpaceID, "")
ctx := c.Request.Context()
config, code, err := h.searchService.GetConfig(ctx, user.ID, req.SpaceID, "")
if err != nil {
common.ResponseWithCodeData(c, code, nil, "failed to get skill search config: "+err.Error())
return
@@ -348,8 +342,6 @@ func (h *SkillSearchHandler) InitializeIndex(c *gin.Context) {
// @Summary List Skill Spaces
// @Description List all skill spaces for the current tenant
// @Tags skill-space
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/skills/spaces [get]
@@ -360,7 +352,8 @@ func (h *SkillSearchHandler) ListSpaces(c *gin.Context) {
return
}
result, code, err := h.spaceService.ListSpaces(user.ID)
ctx := c.Request.Context()
result, code, err := h.spaceService.ListSpaces(ctx, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -420,8 +413,6 @@ func (h *SkillSearchHandler) CreateSpace(c *gin.Context) {
// @Summary Get Skill Space
// @Description Get a skill space by ID
// @Tags skill-space
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param space_id path string true "Space ID"
// @Success 200 {object} map[string]interface{}
@@ -439,7 +430,8 @@ func (h *SkillSearchHandler) GetSpace(c *gin.Context) {
return
}
result, code, err := h.spaceService.GetSpace(spaceID, user.ID)
ctx := c.Request.Context()
result, code, err := h.spaceService.GetSpace(ctx, spaceID, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -526,7 +518,7 @@ func (h *SkillSearchHandler) DeleteSpace(c *gin.Context) {
return
}
code, err := h.spaceService.DeleteSpace(spaceID, user.ID, h.docEngine, c.Request.Context())
code, err := h.spaceService.DeleteSpace(c.Request.Context(), spaceID, user.ID, h.docEngine)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -563,7 +555,8 @@ func (h *SkillSearchHandler) GetSpaceByFolder(c *gin.Context) {
return
}
result, code, err := h.spaceService.GetSpaceByFolderID(folderID, user.ID)
ctx := c.Request.Context()
result, code, err := h.spaceService.GetSpaceByFolderID(ctx, folderID, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return