Browse Source

feat: support fetch tool entity

Yeuoly 10 months ago
parent
commit
37a5b12c86

+ 12 - 0
internal/server/controllers/model.go

@@ -1,6 +1,8 @@
 package controllers
 
 import (
+	"net/http"
+
 	"github.com/gin-gonic/gin"
 	"github.com/langgenius/dify-plugin-daemon/internal/service"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
@@ -163,3 +165,13 @@ func GetAIModelSchema(config *app.Config) gin.HandlerFunc {
 		)
 	}
 }
+
+func ListModels(c *gin.Context) {
+	BindRequest(c, func(request struct {
+		TenantID string `uri:"tenant_id" validate:"required"`
+		Page     int    `form:"page" validate:"required,min=1"`
+		PageSize int    `form:"page_size" validate:"required,min=1,max=256"`
+	}) {
+		c.JSON(http.StatusOK, service.ListModels(request.TenantID, request.Page, request.PageSize))
+	})
+}

+ 0 - 20
internal/server/controllers/plugins.go

@@ -90,23 +90,3 @@ func ListPlugins(c *gin.Context) {
 		c.JSON(http.StatusOK, service.ListPlugins(request.TenantID, request.Page, request.PageSize))
 	})
 }
-
-func ListModels(c *gin.Context) {
-	BindRequest(c, func(request struct {
-		TenantID string `uri:"tenant_id" validate:"required"`
-		Page     int    `form:"page" validate:"required,min=1"`
-		PageSize int    `form:"page_size" validate:"required,min=1,max=256"`
-	}) {
-		c.JSON(http.StatusOK, service.ListModels(request.TenantID, request.Page, request.PageSize))
-	})
-}
-
-func ListTools(c *gin.Context) {
-	BindRequest(c, func(request struct {
-		TenantID string `uri:"tenant_id" validate:"required"`
-		Page     int    `form:"page" validate:"required,min=1"`
-		PageSize int    `form:"page_size" validate:"required,min=1,max=256"`
-	}) {
-		c.JSON(http.StatusOK, service.ListTools(request.TenantID, request.Page, request.PageSize))
-	})
-}

+ 22 - 0
internal/server/controllers/tool.go

@@ -1,6 +1,8 @@
 package controllers
 
 import (
+	"net/http"
+
 	"github.com/gin-gonic/gin"
 	"github.com/langgenius/dify-plugin-daemon/internal/service"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
@@ -33,3 +35,23 @@ func ValidateToolCredentials(config *app.Config) gin.HandlerFunc {
 		)
 	}
 }
+
+func ListTools(c *gin.Context) {
+	BindRequest(c, func(request struct {
+		TenantID string `uri:"tenant_id" validate:"required"`
+		Page     int    `form:"page" validate:"required,min=1"`
+		PageSize int    `form:"page_size" validate:"required,min=1,max=256"`
+	}) {
+		c.JSON(http.StatusOK, service.ListTools(request.TenantID, request.Page, request.PageSize))
+	})
+}
+
+func GetTool(c *gin.Context) {
+	BindRequest(c, func(request struct {
+		TenantID string `uri:"tenant_id" validate:"required"`
+		PluginID string `form:"plugin_id" validate:"required"`
+		Provider string `form:"provider" validate:"required"`
+	}) {
+		c.JSON(http.StatusOK, service.GetTool(request.TenantID, request.PluginID, request.Provider))
+	})
+}

+ 1 - 0
internal/server/http_server.go

@@ -119,6 +119,7 @@ func (app *App) pluginManagementGroup(group *gin.RouterGroup, config *app.Config
 	group.GET("/list", gzip.Gzip(gzip.DefaultCompression), controllers.ListPlugins)
 	group.GET("/models", gzip.Gzip(gzip.DefaultCompression), controllers.ListModels)
 	group.GET("/tools", gzip.Gzip(gzip.DefaultCompression), controllers.ListTools)
+	group.GET("/tool", gzip.Gzip(gzip.DefaultCompression), controllers.GetTool)
 }
 
 func (app *App) pluginAssetGroup(group *gin.RouterGroup) {

+ 18 - 0
internal/service/manage_plugin.go

@@ -88,3 +88,21 @@ func ListModels(tenant_id string, page int, page_size int) *entities.Response {
 
 	return entities.NewSuccessResponse(providers)
 }
+
+func GetTool(tenant_id string, plugin_id string, provider string) *entities.Response {
+	// try get tool
+	tool, err := db.GetOne[models.ToolInstallation](
+		db.Equal("tenant_id", tenant_id),
+		db.Equal("plugin_id", plugin_id),
+	)
+
+	if err != nil {
+		return entities.NewErrorResponse(-500, err.Error())
+	}
+
+	if tool.Provider != provider {
+		return entities.NewErrorResponse(-404, "tool not found")
+	}
+
+	return entities.NewSuccessResponse(tool)
+}