From de49788d52d2a005192f5023e2b9ead7144f5245 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 29 Dec 2024 11:27:40 +0800 Subject: [PATCH] Add utility endpoint for listing connectors in Neo API - Introduced a new GET endpoint at `/utility/connectors` to list available connectors, enhancing the API's utility features. - Implemented `handleConnectors` function to filter and format connector data, providing a structured response with labels and values for each connector. - Updated documentation comments to include usage examples for the new endpoint, improving developer experience and usability. --- neo/api.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/neo/api.go b/neo/api.go index c4a9ca5c..0db39ee8 100644 --- a/neo/api.go +++ b/neo/api.go @@ -12,6 +12,7 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/yaoapp/gou/api" + "github.com/yaoapp/gou/connector" "github.com/yaoapp/gou/process" "github.com/yaoapp/yao/helper" "github.com/yaoapp/yao/neo/conversation" @@ -141,6 +142,12 @@ func (neo *DSL) API(router *gin.Engine, path string) error { router.GET(path+"/generate/prompts", append(middlewares, neo.handleGeneratePrompts)...) router.POST(path+"/generate/prompts", append(middlewares, neo.handleGeneratePrompts)...) + // Utility endpoints + // List connectors example: + // curl -X GET 'http://localhost:5099/api/__yao/neo/utility/connectors?token=xxx' + router.GET(path+"/utility/connectors", append(middlewares, neo.handleConnectors)...) + + // Dangerous operations // Dangerous operations // Clear all chats example: // curl -X DELETE 'http://localhost:5099/api/__yao/neo/dangerous/clear_chats?token=xxx' @@ -950,3 +957,29 @@ func (neo *DSL) handleAssistantDelete(c *gin.Context) { c.JSON(200, gin.H{"message": "ok"}) c.Done() } + +// handleConnectors handles listing connectors +func (neo *DSL) handleConnectors(c *gin.Context) { + options := []map[string]interface{}{} + + // Filter and format connectors + for id, conn := range connector.Connectors { + if conn.Is(connector.OPENAI) || conn.Is(connector.MOAPI) { + setting := conn.Setting() + label := setting["label"] + if label == nil || label == "" { + label = setting["name"] + } + if label == nil || label == "" { + label = id + } + options = append(options, map[string]interface{}{ + "label": label, + "value": id, + }) + } + } + + c.JSON(200, gin.H{"data": options}) + c.Done() +}