Skip to content

Commit 31d7c74

Browse files
authored
feat[0.1.0]: introduce 'run' command for local plugin execution (#283)
* feat: introduce 'run' command for local plugin execution - Added a new command `run` to launch plugins locally, allowing communication through stdin/stdout. - Removed the previous `test` command and its associated functionality to streamline the plugin testing process. * feat: enhance 'run' command with TCP support - Introduced a new `RunPluginPayload` structure to encapsulate plugin execution parameters. - Added TCP communication mode to the `run` command, allowing multiple client connections. - Updated command flags to configure run mode and logging options. - Implemented client handling and server creation for both stdin/stdout and TCP modes. * docs: improve comments in RunPlugin function for clarity - Enhanced comments to provide clearer explanations of the plugin decoding process and the creation of client streams for both stdin/stdout and TCP modes. - Updated comments to reflect the functionality and behavior of the plugin execution flow. * refactor: update command structure and enhance plugin invocation handling - Changed the command structure to add `runPluginCommand` under `pluginCommand` for better organization. - Introduced `InvokePluginPayload` type to encapsulate plugin invocation details. - Enhanced `RunPlugin` function to handle responses and errors more effectively, including logging to stdout. - Updated client handling to support session management and improved error handling during plugin invocation. - Renamed TCP server creation function for consistency. * feat: enhance plugin response handling and logging capabilities - Added a new flag `--response-format` to specify the output format (text or json) for plugin responses. - Introduced a `logger` to manage logging output to stdout with timestamps and file information. - Updated `logResponse` and `systemLog` functions to handle different response formats. - Enhanced `handleClient` and `RunPlugin` functions to utilize the new response format feature. - Implemented signal handling to clean up temporary directories on shutdown. * feat: enhance plugin response structure and logging - Added `InvokeID` to `InvokePluginPayload` and `GenericResponse` for better tracking of plugin invocations. - Updated `logResponse` to include `InvokeID` in error responses for improved debugging. - Enhanced client handling in `handleClient` to log plugin readiness and received requests. - Refactored client stream creation for better readability and consistency. * feat: add plugin invoke end response type and logging - Introduced `GENERIC_RESPONSE_TYPE_PLUGIN_INVOKE_END` to enhance response tracking for plugin invocations. - Updated `handleClient` to log the end of plugin invocation, improving visibility into the plugin lifecycle. * chore: remove fullfeature tags
1 parent a22b9c1 commit 31d7c74

File tree

12 files changed

+493
-130
lines changed

12 files changed

+493
-130
lines changed

cmd/commandline/run.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"github.com/langgenius/dify-plugin-daemon/cmd/commandline/run"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
/*
9+
Test is a very important component of Dify plugins, to ensure every plugin is working as expected
10+
We must provide a way to make the test a pipeline and use standard CI/CD tools to run the tests
11+
12+
However, developers prefer to write test codes in language like Python, it's hard to enforce them to use Go
13+
and what we need is actually a way to launch plugins locally
14+
15+
It makes things easier, the command should be `run`, instead of `test`, user could use `dify plugin run <plugin_id>`
16+
to launch and test it through stdin/stdout
17+
*/
18+
19+
var (
20+
runPluginPayload run.RunPluginPayload
21+
)
22+
23+
var (
24+
runPluginCommand = &cobra.Command{
25+
Use: "run [plugin_package_path]",
26+
Short: "run",
27+
Long: "Launch a plugin locally and communicate through stdin/stdout or TCP",
28+
Args: cobra.ExactArgs(1),
29+
Run: func(c *cobra.Command, args []string) {
30+
runPluginPayload.PluginPath = args[0]
31+
// launch plugin
32+
run.RunPlugin(runPluginPayload)
33+
},
34+
}
35+
)
36+
37+
func init() {
38+
pluginCommand.AddCommand(runPluginCommand)
39+
40+
runPluginCommand.Flags().StringVarP(&runPluginPayload.RunMode, "mode", "m", "stdio", "run mode, stdio or tcp")
41+
runPluginCommand.Flags().BoolVarP(&runPluginPayload.EnableLogs, "enable-logs", "l", false, "enable logs")
42+
runPluginCommand.Flags().StringVarP(&runPluginPayload.ResponseFormat, "response-format", "r", "text", "response format, text or json")
43+
}

cmd/commandline/run/entities.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package run
2+
3+
import (
4+
"context"
5+
"io"
6+
7+
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
8+
)
9+
10+
type RunMode = string
11+
12+
const (
13+
RUN_MODE_STDIO RunMode = "stdio"
14+
RUN_MODE_TCP RunMode = "tcp"
15+
)
16+
17+
type RunPluginPayload struct {
18+
PluginPath string
19+
RunMode RunMode
20+
EnableLogs bool
21+
22+
TcpServerPort int
23+
TcpServerHost string
24+
25+
ResponseFormat string
26+
}
27+
28+
type client struct {
29+
reader io.ReadCloser
30+
writer io.WriteCloser
31+
cancel context.CancelFunc
32+
}
33+
34+
type InvokePluginPayload struct {
35+
InvokeID string `json:"invoke_id"`
36+
Type access_types.PluginAccessType `json:"type"`
37+
Action access_types.PluginAccessAction `json:"action"`
38+
39+
Request map[string]any `json:"request"`
40+
}
41+
42+
type GenericResponseType = string
43+
44+
const (
45+
GENERIC_RESPONSE_TYPE_INFO GenericResponseType = "info"
46+
GENERIC_RESPONSE_TYPE_PLUGIN_READY GenericResponseType = "plugin_ready"
47+
GENERIC_RESPONSE_TYPE_ERROR GenericResponseType = "error"
48+
GENERIC_RESPONSE_TYPE_PLUGIN_RESPONSE GenericResponseType = "plugin_response"
49+
GENERIC_RESPONSE_TYPE_PLUGIN_INVOKE_END GenericResponseType = "plugin_invoke_end"
50+
)
51+
52+
type GenericResponse struct {
53+
InvokeID string `json:"invoke_id"`
54+
Type GenericResponseType `json:"type"`
55+
56+
Response map[string]any `json:"response"`
57+
}

cmd/commandline/run/logger.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package run
2+
3+
import (
4+
"log"
5+
"os"
6+
)
7+
8+
var logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)

0 commit comments

Comments
 (0)