Browse Source

feat: add Python plugin pre-compilation and update plugin upgrade logic (#54)

* feat: add Python plugin pre-compilation and update plugin upgrade logic

This commit introduces two main changes:
1. Pre-compilation of Python plugins during environment initialization to improve initial runtime performance
2. Updated plugin upgrade logic to use PluginID instead of string identifier for agent strategy installation

* refactor: update plugin installation deletion to use PluginID consistently

Remove PluginUniqueIdentifier from deletion conditions in UninstallPlugin and UpgradePlugin methods, standardizing the use of PluginID across different installation types
Yeuoly 4 months ago
parent
commit
8e59ac8b9a

+ 82 - 0
internal/core/plugin_manager/local_runtime/environment_python.go

@@ -201,6 +201,88 @@ func (p *LocalPluginRuntime) InitPythonEnvironment() error {
 		return fmt.Errorf("failed to install dependencies: %s, output: %s", err, errMsg.String())
 		return fmt.Errorf("failed to install dependencies: %s, output: %s", err, errMsg.String())
 	}
 	}
 
 
+	// pre-compile the plugin to avoid costly compilation on first invocation
+	compileCmd := exec.CommandContext(ctx, pythonPath, "-m", "compileall", ".")
+	compileCmd.Dir = p.State.WorkingPath
+
+	// get stdout and stderr
+	compileStdout, err := compileCmd.StdoutPipe()
+	if err != nil {
+		return fmt.Errorf("failed to get stdout: %s", err)
+	}
+	defer compileStdout.Close()
+
+	compileStderr, err := compileCmd.StderrPipe()
+	if err != nil {
+		return fmt.Errorf("failed to get stderr: %s", err)
+	}
+	defer compileStderr.Close()
+
+	// start command
+	if err := compileCmd.Start(); err != nil {
+		return fmt.Errorf("failed to start command: %s", err)
+	}
+	defer func() {
+		if compileCmd.Process != nil {
+			compileCmd.Process.Kill()
+		}
+	}()
+
+	var compileErrMsg strings.Builder
+	var compileWg sync.WaitGroup
+	compileWg.Add(2)
+
+	routine.Submit(map[string]string{
+		"module":   "plugin_manager",
+		"function": "InitPythonEnvironment",
+	}, func() {
+		defer compileWg.Done()
+		// read compileStdout
+		for {
+			buf := make([]byte, 102400)
+			n, err := compileStdout.Read(buf)
+			if err != nil {
+				break
+			}
+			// split to first line
+			lines := strings.Split(string(buf[:n]), "\n")
+
+			for len(lines) > 0 && len(lines[0]) == 0 {
+				lines = lines[1:]
+			}
+
+			if len(lines) > 0 {
+				if len(lines) > 1 {
+					log.Info("pre-compiling %s - %s...", p.Config.Identity(), lines[0])
+				} else {
+					log.Info("pre-compiling %s - %s", p.Config.Identity(), lines[0])
+				}
+			}
+		}
+	})
+
+	routine.Submit(map[string]string{
+		"module":   "plugin_manager",
+		"function": "InitPythonEnvironment",
+	}, func() {
+		defer compileWg.Done()
+		// read stderr
+		buf := make([]byte, 1024)
+		for {
+			n, err := compileStderr.Read(buf)
+			if err != nil {
+				break
+			}
+			compileErrMsg.WriteString(string(buf[:n]))
+		}
+	})
+
+	compileWg.Wait()
+	if err := compileCmd.Wait(); err != nil {
+		return fmt.Errorf("failed to pre-compile the plugin: %s", compileErrMsg.String())
+	}
+
 	success = true
 	success = true
+
 	return nil
 	return nil
 }
 }

+ 12 - 15
internal/types/models/curd/atomic.go

@@ -234,9 +234,8 @@ func UninstallPlugin(
 		// delete tool installation
 		// delete tool installation
 		if declaration.Tool != nil {
 		if declaration.Tool != nil {
 			toolInstallation := &models.ToolInstallation{
 			toolInstallation := &models.ToolInstallation{
-				PluginID:               pluginToBeReturns.PluginID,
-				PluginUniqueIdentifier: pluginToBeReturns.PluginUniqueIdentifier,
-				TenantID:               tenant_id,
+				PluginID: pluginToBeReturns.PluginID,
+				TenantID: tenant_id,
 			}
 			}
 
 
 			err := db.DeleteByCondition(&toolInstallation, tx)
 			err := db.DeleteByCondition(&toolInstallation, tx)
@@ -248,9 +247,8 @@ func UninstallPlugin(
 		// delete agent installation
 		// delete agent installation
 		if declaration.AgentStrategy != nil {
 		if declaration.AgentStrategy != nil {
 			agentStrategyInstallation := &models.AgentStrategyInstallation{
 			agentStrategyInstallation := &models.AgentStrategyInstallation{
-				PluginID:               pluginToBeReturns.PluginID,
-				PluginUniqueIdentifier: pluginToBeReturns.PluginUniqueIdentifier,
-				TenantID:               tenant_id,
+				PluginID: pluginToBeReturns.PluginID,
+				TenantID: tenant_id,
 			}
 			}
 
 
 			err := db.DeleteByCondition(&agentStrategyInstallation, tx)
 			err := db.DeleteByCondition(&agentStrategyInstallation, tx)
@@ -262,9 +260,8 @@ func UninstallPlugin(
 		// delete model installation
 		// delete model installation
 		if declaration.Model != nil {
 		if declaration.Model != nil {
 			modelInstallation := &models.AIModelInstallation{
 			modelInstallation := &models.AIModelInstallation{
-				PluginID:               pluginToBeReturns.PluginID,
-				PluginUniqueIdentifier: pluginToBeReturns.PluginUniqueIdentifier,
-				TenantID:               tenant_id,
+				PluginID: pluginToBeReturns.PluginID,
+				TenantID: tenant_id,
 			}
 			}
 
 
 			err := db.DeleteByCondition(&modelInstallation, tx)
 			err := db.DeleteByCondition(&modelInstallation, tx)
@@ -408,8 +405,8 @@ func UpgradePlugin(
 		if originalDeclaration.Model != nil {
 		if originalDeclaration.Model != nil {
 			// delete the original ai model installation
 			// delete the original ai model installation
 			err := db.DeleteByCondition(&models.AIModelInstallation{
 			err := db.DeleteByCondition(&models.AIModelInstallation{
-				PluginUniqueIdentifier: original_plugin_unique_identifier.String(),
-				TenantID:               tenant_id,
+				PluginID: original_plugin_unique_identifier.PluginID(),
+				TenantID: tenant_id,
 			}, tx)
 			}, tx)
 
 
 			if err != nil {
 			if err != nil {
@@ -436,8 +433,8 @@ func UpgradePlugin(
 		if originalDeclaration.Tool != nil {
 		if originalDeclaration.Tool != nil {
 			// delete the original tool installation
 			// delete the original tool installation
 			err := db.DeleteByCondition(&models.ToolInstallation{
 			err := db.DeleteByCondition(&models.ToolInstallation{
-				PluginUniqueIdentifier: original_plugin_unique_identifier.String(),
-				TenantID:               tenant_id,
+				PluginID: original_plugin_unique_identifier.PluginID(),
+				TenantID: tenant_id,
 			}, tx)
 			}, tx)
 
 
 			if err != nil {
 			if err != nil {
@@ -464,8 +461,8 @@ func UpgradePlugin(
 		if originalDeclaration.AgentStrategy != nil {
 		if originalDeclaration.AgentStrategy != nil {
 			// delete the original agent installation
 			// delete the original agent installation
 			err := db.DeleteByCondition(&models.AgentStrategyInstallation{
 			err := db.DeleteByCondition(&models.AgentStrategyInstallation{
-				PluginUniqueIdentifier: original_plugin_unique_identifier.String(),
-				TenantID:               tenant_id,
+				PluginID: original_plugin_unique_identifier.PluginID(),
+				TenantID: tenant_id,
 			}, tx)
 			}, tx)
 
 
 			if err != nil {
 			if err != nil {