|
@@ -1,7 +1,10 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "errors"
|
|
|
|
+
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
+ "github.com/langgenius/dify-plugin-daemon/internal/core/persistence"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
|
|
@@ -18,7 +21,12 @@ func createSession[T any](
|
|
access_type access_types.PluginAccessType,
|
|
access_type access_types.PluginAccessType,
|
|
access_action access_types.PluginAccessAction,
|
|
access_action access_types.PluginAccessAction,
|
|
cluster_id string,
|
|
cluster_id string,
|
|
-) *session_manager.Session {
|
|
|
|
|
|
+) (*session_manager.Session, error) {
|
|
|
|
+ persistence := persistence.GetPersistence()
|
|
|
|
+ if persistence == nil {
|
|
|
|
+ return nil, errors.New("persistence not found")
|
|
|
|
+ }
|
|
|
|
+
|
|
plugin_identity := parser.MarshalPluginIdentity(r.PluginName, r.PluginVersion)
|
|
plugin_identity := parser.MarshalPluginIdentity(r.PluginName, r.PluginVersion)
|
|
runtime := plugin_manager.GetGlobalPluginManager().Get(plugin_identity)
|
|
runtime := plugin_manager.GetGlobalPluginManager().Get(plugin_identity)
|
|
|
|
|
|
@@ -30,10 +38,11 @@ func createSession[T any](
|
|
access_type,
|
|
access_type,
|
|
access_action,
|
|
access_action,
|
|
runtime.Configuration(),
|
|
runtime.Configuration(),
|
|
|
|
+ persistence,
|
|
)
|
|
)
|
|
|
|
|
|
session.BindRuntime(runtime)
|
|
session.BindRuntime(runtime)
|
|
- return session
|
|
|
|
|
|
+ return session, nil
|
|
}
|
|
}
|
|
|
|
|
|
func InvokeLLM(
|
|
func InvokeLLM(
|
|
@@ -42,12 +51,16 @@ func InvokeLLM(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_LLM,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_LLM,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -65,11 +78,15 @@ func InvokeTextEmbedding(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_TEXT_EMBEDDING,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_TEXT_EMBEDDING,
|
|
ctx.GetString("cluster_id"))
|
|
ctx.GetString("cluster_id"))
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -87,12 +104,16 @@ func InvokeRerank(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_RERANK,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_RERANK,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -110,12 +131,16 @@ func InvokeTTS(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_TTS,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_TTS,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -133,12 +158,16 @@ func InvokeSpeech2Text(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_SPEECH2TEXT,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_SPEECH2TEXT,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -156,12 +185,16 @@ func InvokeModeration(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_MODERATION,
|
|
access_types.PLUGIN_ACCESS_ACTION_INVOKE_MODERATION,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -179,12 +212,16 @@ func ValidateProviderCredentials(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_VALIDATE_PROVIDER_CREDENTIALS,
|
|
access_types.PLUGIN_ACCESS_ACTION_VALIDATE_PROVIDER_CREDENTIALS,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|
|
@@ -202,12 +239,16 @@ func ValidateModelCredentials(
|
|
max_timeout_seconds int,
|
|
max_timeout_seconds int,
|
|
) {
|
|
) {
|
|
// create session
|
|
// create session
|
|
- session := createSession(
|
|
|
|
|
|
+ session, err := createSession(
|
|
r,
|
|
r,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_TYPE_MODEL,
|
|
access_types.PLUGIN_ACCESS_ACTION_VALIDATE_MODEL_CREDENTIALS,
|
|
access_types.PLUGIN_ACCESS_ACTION_VALIDATE_MODEL_CREDENTIALS,
|
|
ctx.GetString("cluster_id"),
|
|
ctx.GetString("cluster_id"),
|
|
)
|
|
)
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.JSON(500, gin.H{"error": err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
defer session.Close()
|
|
defer session.Close()
|
|
|
|
|
|
baseSSEService(
|
|
baseSSEService(
|