Skip to content

Commit

Permalink
refactor: confused code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Jan 6, 2025
1 parent 15fdd2c commit 8796eda
Show file tree
Hide file tree
Showing 59 changed files with 141 additions and 149 deletions.
4 changes: 2 additions & 2 deletions internal/cluster/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"time"

"github.com/google/uuid"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_runtime"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/manifest_entities"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
)

type fakePlugin struct {
plugin_entities.PluginRuntime
positive_manager.PositivePluginRuntime
basic_runtime.BasicChecksum
}

func (r *fakePlugin) InitEnvironment() error {
Expand Down
1 change: 0 additions & 1 deletion internal/core/plugin_manager/aws_manager/init.go

This file was deleted.

13 changes: 0 additions & 13 deletions internal/core/plugin_manager/basic_manager/type.go

This file was deleted.

42 changes: 42 additions & 0 deletions internal/core/plugin_manager/basic_runtime/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package basic_runtime

import (
"os"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
)

type BasicChecksum struct {
MediaTransport

WorkingPath string
// plugin decoder used to manage the plugin
Decoder decoder.PluginDecoder

InnerChecksum string
}

func (r *BasicChecksum) calculateChecksum() (string, error) {
checksum, err := r.Decoder.Checksum()
if err != nil {
return "", err
}

return checksum, nil
}

func (r *BasicChecksum) Checksum() (string, error) {
if r.InnerChecksum == "" {
checksum, err := r.calculateChecksum()
if err != nil {
return "", err
}
r.InnerChecksum = checksum
}

return r.InnerChecksum, nil
}

func (r *BasicChecksum) Cleanup() {
os.RemoveAll(r.WorkingPath)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package basic_manager
package basic_runtime

import (
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_transport"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
)

type MediaTransport struct {
mediaManager *media_transport.MediaBucket

assetsIds []string
}

// RemapAssets will take the assets and remap them to a media id
func (r *BasicPluginRuntime) RemapAssets(
func (r *MediaTransport) RemapAssets(
declaration *plugin_entities.PluginDeclaration,
assets map[string][]byte,
) error {
Expand All @@ -17,3 +24,7 @@ func (r *BasicPluginRuntime) RemapAssets(
r.assetsIds = assetsIds
return nil
}

func NewMediaTransport(mediaManager *media_transport.MediaBucket) MediaTransport {
return MediaTransport{mediaManager: mediaManager}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"bytes"
Expand All @@ -8,8 +8,8 @@ import (
"sync/atomic"
"time"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_runtime"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_transport"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
Expand All @@ -29,7 +29,7 @@ type DifyServer struct {

engine gnet.Engine

mediaManager *media_manager.MediaBucket
mediaManager *media_transport.MediaBucket

// listening address
addr string
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *DifyServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
// new plugin connected
c.SetContext(&codec{})
runtime := &RemotePluginRuntime{
BasicPluginRuntime: basic_manager.NewBasicPluginRuntime(
MediaTransport: basic_runtime.NewMediaTransport(
s.mediaManager,
),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"encoding/json"
Expand All @@ -17,7 +17,7 @@ func (r *RemotePluginRuntime) Listen(session_id string) *entities.Broadcast[plug
listener.OnClose(func() {
// execute in new goroutine to avoid deadlock
routine.Submit(map[string]string{
"module": "remote_manager",
"module": "debugging_runtime",
"method": "removeMessageCallbackHandler",
}, func() {
r.removeMessageCallbackHandler(session_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import "github.com/langgenius/dify-plugin-daemon/internal/service/install_service"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"time"
Expand Down Expand Up @@ -39,7 +39,7 @@ func (r *RemotePluginRuntime) StartPlugin() error {

// handle heartbeat
routine.Submit(map[string]string{
"module": "remote_manager",
"module": "debugging_runtime",
"function": "StartPlugin",
"plugin_id": identity.String(),
}, func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"context"
Expand All @@ -11,7 +11,7 @@ import (
"syscall"
"time"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_transport"
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *RemotePluginServer) collectShutdownSignal() {
}

// NewRemotePluginServer creates a new RemotePluginServer
func NewRemotePluginServer(config *app.Config, media_manager *media_manager.MediaBucket) *RemotePluginServer {
func NewRemotePluginServer(config *app.Config, media_transport *media_transport.MediaBucket) *RemotePluginServer {
addr := fmt.Sprintf(
"tcp://%s:%d",
config.PluginRemoteInstallingHost,
Expand All @@ -115,7 +115,7 @@ func NewRemotePluginServer(config *app.Config, media_manager *media_manager.Medi

multicore := true
s := &DifyServer{
mediaManager: media_manager,
mediaManager: media_transport,
addr: addr,
port: config.PluginRemoteInstallingPort,
multicore: multicore,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote_manager
package debugging_runtime

import (
"errors"
Expand All @@ -9,7 +9,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_transport"
"github.com/langgenius/dify-plugin-daemon/internal/db"
"github.com/langgenius/dify-plugin-daemon/internal/oss/local"
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
Expand Down Expand Up @@ -49,7 +49,7 @@ func preparePluginServer(t *testing.T) (*RemotePluginServer, uint16) {
PluginRemoteInstallingPort: port,
PluginRemoteInstallingMaxConn: 1,
PluginRemoteInstallServerEventLoopNums: 8,
}, media_manager.NewAssetsBucket(oss, "assets", 10)), port
}, media_transport.NewAssetsBucket(oss, "assets", 10)), port
}

// TestLaunchAndClosePluginServer tests the launch and close of the plugin server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package remote_manager
package debugging_runtime

import (
"bytes"
"sync"
"sync/atomic"
"time"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_runtime"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
"github.com/panjf2000/gnet/v2"
Expand All @@ -17,7 +17,7 @@ type pluginRuntimeMode string
const _PLUGIN_RUNTIME_MODE_CI pluginRuntimeMode = "ci"

type RemotePluginRuntime struct {
basic_manager.BasicPluginRuntime
basic_runtime.MediaTransport
plugin_entities.PluginRuntime

// connection
Expand Down
2 changes: 1 addition & 1 deletion internal/core/plugin_manager/install_to_serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package plugin_manager
import (
"fmt"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/serverless"
serverless "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/serverless_connector"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
"github.com/langgenius/dify-plugin-daemon/internal/db"
"github.com/langgenius/dify-plugin-daemon/internal/types/models"
Expand Down
15 changes: 7 additions & 8 deletions internal/core/plugin_manager/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"path"
"strings"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/local_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_runtime"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/local_runtime"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
Expand Down Expand Up @@ -130,12 +129,12 @@ func (p *PluginManager) launchLocal(pluginUniqueIdentifier plugin_entities.Plugi
return nil, nil, nil, failed(err.Error())
}

localPluginRuntime := local_manager.NewLocalPluginRuntime(p.pythonInterpreterPath)
localPluginRuntime := local_runtime.NewLocalPluginRuntime(p.pythonInterpreterPath)
localPluginRuntime.PluginRuntime = plugin.runtime
localPluginRuntime.PositivePluginRuntime = positive_manager.PositivePluginRuntime{
BasicPluginRuntime: basic_manager.NewBasicPluginRuntime(p.mediaBucket),
WorkingPath: plugin.runtime.State.WorkingPath,
Decoder: plugin.decoder,
localPluginRuntime.BasicChecksum = basic_runtime.BasicChecksum{
MediaTransport: basic_runtime.NewMediaTransport(p.mediaBucket),
WorkingPath: plugin.runtime.State.WorkingPath,
Decoder: plugin.decoder,
}

if err := localPluginRuntime.RemapAssets(
Expand Down
1 change: 0 additions & 1 deletion internal/core/plugin_manager/local_manager/tester.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package local_manager
package local_runtime

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package local_manager
package local_runtime

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package local_manager
package local_runtime

import (
"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package local_manager handles the local plugin runtime management
package local_manager
// Package local_runtime handles the local plugin runtime management
package local_runtime

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package local_manager
package local_runtime

import (
"bufio"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package local_manager
package local_runtime

import (
"io"
Expand Down
1 change: 1 addition & 0 deletions internal/core/plugin_manager/local_runtime/tester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package local_runtime
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package local_manager
package local_runtime

import (
"sync"

"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_runtime"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
)

type LocalPluginRuntime struct {
positive_manager.PositivePluginRuntime
basic_runtime.BasicChecksum
plugin_entities.PluginRuntime

waitChan chan bool
Expand Down
Loading

0 comments on commit 8796eda

Please sign in to comment.