diff --git a/cmd/commandline/plugin.go b/cmd/commandline/plugin.go index 845f6598c..b25865046 100644 --- a/cmd/commandline/plugin.go +++ b/cmd/commandline/plugin.go @@ -172,50 +172,6 @@ If no parameters are provided, an interactive mode will be started.`, }, } - // NOTE: tester is deprecated, maybe, in several months, we will support this again - // pluginTestCommand = &cobra.Command{ - // Use: "test [-i inputs] [-t timeout] package_path invoke_type invoke_action", - // Short: "", - // Long: "Test runs the given plugin package locally, and you can specify the inputs using json format, if not specified, will use default inputs\n" + - // "type: invoke type, available values: \n" + - // "[\n" + - // " tool, model, endpoint\n" + - // "]\n" + - // "action: invoke action, available values: \n" + - // "[\n" + - // " invoke_tool, validate_tool_credentials, \n" + - // " invoke_endpoint\n" + - // " invoke_llm, invoke_text_embedding, invoke_rerank, invoke_tts, invoke_speech2text, invoke_moderation, \n" + - // " validate_provider_credentials, validate_model_credentials, get_tts_model_voices, \n" + - // " get_text_embedding_num_tokens, get_ai_model_schemas, get_llm_num_tokens\n" + - // "]\n", - // Run: func(cmd *cobra.Command, args []string) { - // if len(args) < 3 { - // log.Error("invalid args, please specify package_path, invoke_type, invoke_action") - // return - // } - // // get package path - // package_path_str := args[0] - // // get invoke type - // invoke_type_str := args[1] - // // get invoke action - // invoke_action_str := args[2] - // // get inputs if specified - // inputs := map[string]any{} - // if cmd.Flag("inputs") != nil { - // inputs_str := cmd.Flag("inputs").Value.String() - // err := json.Unmarshal([]byte(inputs_str), &inputs) - // if err != nil { - // log.Error("failed to unmarshal inputs, inputs: %s, error: %v", inputs_str, err) - // return - // } - // } - // // parse flag - // timeout := "" - // if cmd.Flag("timeout") != nil { - // timeout = cmd.Flag("timeout").Value.String() - // } - ) func init() { diff --git a/cmd/commandline/signature/signature_test.go b/cmd/commandline/signature/signature_test.go index ddf74d0c8..702060717 100644 --- a/cmd/commandline/signature/signature_test.go +++ b/cmd/commandline/signature/signature_test.go @@ -285,7 +285,7 @@ func TestVerifyPluginWithoutVerificationField(t *testing.T) { t.Fatalf("Failed to create dummy plugin file: %v", err) } - pluginPackageWithoutVerificationField, err := signer.TraditionalSignPlugin(dummyPlugin) + pluginPackageWithoutVerificationField, err := signer.SignPlugin(dummyPlugin, nil) if err != nil { t.Fatalf("Failed to sign plugin: %v", err) } diff --git a/internal/core/plugin_manager/local_runtime/tester.go b/internal/core/plugin_manager/local_runtime/tester.go deleted file mode 100644 index f5e7662d2..000000000 --- a/internal/core/plugin_manager/local_runtime/tester.go +++ /dev/null @@ -1 +0,0 @@ -package local_runtime diff --git a/internal/core/plugin_manager/serverless_runtime/dockerfile/build.go b/internal/core/plugin_manager/serverless_runtime/dockerfile/build.go deleted file mode 100644 index 9d60bfa33..000000000 --- a/internal/core/plugin_manager/serverless_runtime/dockerfile/build.go +++ /dev/null @@ -1,30 +0,0 @@ -package dockerfile - -import ( - "fmt" - - "github.com/langgenius/dify-plugin-daemon/internal/utils/strings" - "github.com/langgenius/dify-plugin-daemon/pkg/entities/constants" - "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities" -) - -func handleTemplate(configuration *plugin_entities.PluginDeclaration, templateFunc func(configuration *plugin_entities.PluginDeclaration) (string, error)) (string, error) { - if templateFunc == nil { - return "", fmt.Errorf("template function is nil, language: %s, version: %s", configuration.Meta.Runner.Language, configuration.Meta.Runner.Version) - } - return templateFunc(configuration) -} - -// GenerateDockerfile generates a Dockerfile for the plugin -func GenerateDockerfile(configuration *plugin_entities.PluginDeclaration) (string, error) { - if !strings.Find(configuration.Meta.Arch, constants.AMD64) { - return "", fmt.Errorf("unsupported architecture: %s", configuration.Meta.Arch) - } - - switch configuration.Meta.Runner.Language { - case constants.Python: - return handleTemplate(configuration, pythonTemplates[configuration.Meta.Runner.Version]) - } - - return "", fmt.Errorf("unsupported language: %s", configuration.Meta.Runner.Language) -} diff --git a/internal/core/plugin_manager/serverless_runtime/dockerfile/build_test.go b/internal/core/plugin_manager/serverless_runtime/dockerfile/build_test.go deleted file mode 100644 index 7d6ba6dcc..000000000 --- a/internal/core/plugin_manager/serverless_runtime/dockerfile/build_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package dockerfile - -import ( - "strings" - "testing" - - "github.com/langgenius/dify-plugin-daemon/pkg/entities/constants" - "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities" -) - -func preparePluginDeclaration() *plugin_entities.PluginDeclaration { - return &plugin_entities.PluginDeclaration{ - PluginDeclarationWithoutAdvancedFields: plugin_entities.PluginDeclarationWithoutAdvancedFields{ - Meta: plugin_entities.PluginMeta{ - Arch: []constants.Arch{ - constants.AMD64, - }, - Runner: plugin_entities.PluginRunner{ - Language: constants.Python, - Version: "3.12", - Entrypoint: "main", - }, - }, - }, - } -} - -func TestGenerateDockerfile(t *testing.T) { - pluginDeclaration := preparePluginDeclaration() - dockerfile, err := GenerateDockerfile(pluginDeclaration) - if err != nil { - t.Fatalf("Error generating Dockerfile: %v", err) - } - - if !strings.Contains(dockerfile, "main") || !strings.Contains(dockerfile, "3.12") { - t.Logf("Generated Dockerfile: %s", dockerfile) - } -} - -func TestGenerateDockerfileWithInvalidPluginDeclaration(t *testing.T) { - pluginDeclaration := &plugin_entities.PluginDeclaration{} - _, err := GenerateDockerfile(pluginDeclaration) - if err == nil { - t.Fatalf("Expected error, got nil") - } -} diff --git a/internal/core/plugin_manager/serverless_runtime/dockerfile/python.go b/internal/core/plugin_manager/serverless_runtime/dockerfile/python.go deleted file mode 100644 index e99545baf..000000000 --- a/internal/core/plugin_manager/serverless_runtime/dockerfile/python.go +++ /dev/null @@ -1,24 +0,0 @@ -package dockerfile - -import ( - _ "embed" - "strings" - - "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities" -) - -var ( - pythonTemplates = map[string]func(configuration *plugin_entities.PluginDeclaration) (string, error){ - "3.12": GeneratePython312Dockerfile, - } -) - -//go:embed python312.dockerfile -var python312DockerfileTmpl string - -// GeneratePython312Dockerfile generates a dockerfile for python 3.12 -func GeneratePython312Dockerfile(configuration *plugin_entities.PluginDeclaration) (string, error) { - entrypoint := configuration.Meta.Runner.Entrypoint - - return strings.Replace(python312DockerfileTmpl, "{{entrypoint}}", entrypoint, -1), nil -} diff --git a/internal/core/plugin_manager/serverless_runtime/dockerfile/python312.dockerfile b/internal/core/plugin_manager/serverless_runtime/dockerfile/python312.dockerfile deleted file mode 100644 index dd5b9d56f..000000000 --- a/internal/core/plugin_manager/serverless_runtime/dockerfile/python312.dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM public.ecr.aws/docker/library/python:3.12.0-slim-bullseye -COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter - -WORKDIR /app -ADD . /app -RUN pip install -r requirements.txt - -CMD ["python", "-m", "{{entrypoint}}"] diff --git a/internal/core/plugin_manager/serverless_runtime/init.go b/internal/core/plugin_manager/serverless_runtime/init.go deleted file mode 100644 index 8814cb9c5..000000000 --- a/internal/core/plugin_manager/serverless_runtime/init.go +++ /dev/null @@ -1 +0,0 @@ -package serverless_runtime diff --git a/internal/core/plugin_manager/tester.go b/internal/core/plugin_manager/tester.go deleted file mode 100644 index 3b2ce4aba..000000000 --- a/internal/core/plugin_manager/tester.go +++ /dev/null @@ -1,105 +0,0 @@ -package plugin_manager - -/* -NOTE: tester is deprecated, maybe, in several months, we will support this again -*/ - -// func (p *PluginManager) TestPlugin( -// path string, -// request map[string]any, -// access_type access_types.PluginAccessType, -// access_action access_types.PluginAccessAction, -// timeout string, -// ) (*stream.Stream[any], error) { - -// // launch plugin runtime -// plugin, err := p.getLocalPluginRuntime(path) -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to load plugin")) -// } - -// // get assets -// assets, err := plugin.decoder.Assets() -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to get assets")) -// } - -// local_plugin_runtime := local_runtime.NewLocalPluginRuntime(p.pythonInterpreterPath) -// local_plugin_runtime.PluginRuntime = plugin.runtime -// local_plugin_runtime.PositivePluginRuntime = positive_manager.PositivePluginRuntime{ -// BasicPluginRuntime: basic_runtime.NewBasicPluginRuntime(p.mediaBucket), -// WorkingPath: plugin.runtime.State.WorkingPath, -// Decoder: plugin.decoder, -// } -// if err := local_plugin_runtime.RemapAssets( -// &local_plugin_runtime.Config, -// assets, -// ); err != nil { -// return nil, errors.Join(err, errors.New("failed to remap assets")) -// } - -// identity, err := local_plugin_runtime.Identity() -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to get identity")) -// } - -// // local plugin -// routine.Submit(func() { -// defer func() { -// if r := recover(); r != nil { -// // print stack trace -// buf := make([]byte, 1<<16) -// runtime.Stack(buf, true) -// log.Error("plugin runtime error: %v, stack trace: %s", r, string(buf)) -// } -// }() -// // delete the plugin from the storage when the plugin is stopped -// p.fullDuplexLifecycle(local_plugin_runtime, nil) -// }) - -// // wait for the plugin to start -// var timeout_duration time.Duration -// if timeout == "" { -// timeout_duration = 120 * time.Second -// } else { -// timeout_duration, err = time.ParseDuration(timeout) -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to parse timeout")) -// } -// } -// select { -// case <-local_plugin_runtime.WaitStarted(): -// case <-time.After(timeout_duration): -// return nil, errors.New("failed to start plugin after " + timeout_duration.String()) -// } - -// session := session_manager.NewSession( -// session_manager.NewSessionPayload{ -// TenantID: "test-tenant", -// UserID: "test-user", -// PluginUniqueIdentifier: identity, -// ClusterID: "test-cluster", -// InvokeFrom: access_type, -// Action: access_action, -// Declaration: plugin.runtime.Configuration(), -// BackwardsInvocation: manager.BackwardsInvocation(), -// IgnoreCache: true, -// }, -// ) -// session.BindRuntime(local_plugin_runtime) -// defer session.Close(session_manager.CloseSessionPayload{ -// IgnoreCache: true, -// }) - -// // try send request -// plugin_response, err := plugin_daemon.GenericInvokePlugin[map[string]any, any](session, &request, 1024) -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to invoke plugin")) -// } - -// plugin_response.OnClose(func() { -// local_plugin_runtime.Stop() -// }) - -// return plugin_response, nil -// } diff --git a/pkg/plugin_packager/signer/sign.go b/pkg/plugin_packager/signer/sign.go index e4fe907e8..1bd400b77 100644 --- a/pkg/plugin_packager/signer/sign.go +++ b/pkg/plugin_packager/signer/sign.go @@ -25,14 +25,3 @@ func SignPlugin(plugin []byte, verification *decoder.Verification) ([]byte, erro return withkey.SignPluginWithPrivateKey(plugin, verification, privateKey) } -// TraditionalSignPlugin, only used for testing -// WARNING: This function is deprecated, use SignPlugin instead -func TraditionalSignPlugin(plugin []byte) ([]byte, error) { - // load private key - privateKey, err := encryption.LoadPrivateKey(private_key.PRIVATE_KEY) - if err != nil { - return nil, err - } - - return withkey.TraditionalSignPlugin(plugin, privateKey) -} diff --git a/pkg/plugin_packager/signer/withkey/sign_with_key.go b/pkg/plugin_packager/signer/withkey/sign_with_key.go index 2fa8b88ca..fc73b8644 100644 --- a/pkg/plugin_packager/signer/withkey/sign_with_key.go +++ b/pkg/plugin_packager/signer/withkey/sign_with_key.go @@ -135,89 +135,3 @@ func SignPluginWithPrivateKey( return zipBuffer.Bytes(), nil } -// Only used for testing -// WARNING: This function is deprecated, use SignPluginWithPrivateKey instead -func TraditionalSignPlugin( - plugin []byte, - privateKey *rsa.PrivateKey, -) ([]byte, error) { - decoder, err := decoder.NewZipPluginDecoder(plugin) - if err != nil { - return nil, err - } - - // create a new zip writer - zipBuffer := new(bytes.Buffer) - zipWriter := zip.NewWriter(zipBuffer) - - defer zipWriter.Close() - // store temporary hash - data := new(bytes.Buffer) - // read one by one - err = decoder.Walk(func(filename, dir string) error { - file, err := decoder.ReadFile(path.Join(dir, filename)) - if err != nil { - return err - } - - // calculate sha256 hash of the file - hash := sha256.New() - hash.Write(file) - hashed := hash.Sum(nil) - - // write the hash into data - data.Write(hashed) - - // create a new file in the zip writer - fileWriter, err := zipWriter.Create(path.Join(dir, filename)) - if err != nil { - return err - } - - _, err = io.Copy(fileWriter, bytes.NewReader(file)) - if err != nil { - return err - } - - return nil - }) - - if err != nil { - return nil, err - } - - // get current time - ct := time.Now().Unix() - - // convert time to bytes - timeString := strconv.FormatInt(ct, 10) - - // write the time into data - data.Write([]byte(timeString)) - - // sign the data - signature, err := encryption.RSASign(privateKey, data.Bytes()) - if err != nil { - return nil, err - } - - // write the signature into the comment field of the zip file - comments := parser.MarshalJson(map[string]any{ - "signature": base64.StdEncoding.EncodeToString(signature), - "time": ct, - }) - - // write signature - err = zipWriter.SetComment(comments) - if err != nil { - return nil, err - } - - // close the zip writer - err = zipWriter.Close() - if err != nil { - return nil, err - } - - return zipBuffer.Bytes(), nil -}