diff --git a/example/example.go b/example/example.go index 7d100f1..6b36ad1 100644 --- a/example/example.go +++ b/example/example.go @@ -34,20 +34,11 @@ func OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackData } // 简单的插件处理实现 -func OnPluginMessageReceived(ctx context.Context, message *plugin.PluginMessage) (interface{}, error) { - //可以根据message中的PluginId、PluginVersion、AbilityKey路由到具体一个能力 - if message.AbilityKey == "echo" { - echoRequest := &EchoRequest{} - //将数据转换成插件的请求参数 - err := message.ParseRequest(echoRequest) - if err != nil { - return nil, err - } - //执行插件 - echoResponse := Echo(echoRequest) - return echoResponse, nil +func OnPluginMessageReceived(ctx context.Context, request *plugin.GraphRequest) (*plugin.GraphResponse, error) { + response := &plugin.GraphResponse{ + Body: `{"text": "hello world", "content": [{"title": "1", "description": "2", "url":"https://www.zhihu.com/question/626551401"},{"title": "2", "description": "2", "url":"https://www.zhihu.com/question/626551401"}]}`, } - return nil, nil + return response, nil } // 事件处理 diff --git a/payload/utils.go b/payload/utils.go index 39ee57e..dbd89f8 100644 --- a/payload/utils.go +++ b/payload/utils.go @@ -24,8 +24,8 @@ const ( DataFrameResponseStatusCodeKInternalError = 500 DataFrameResponseStatusCodeKHandlerNotFound = 404 - BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic - PluginMessageCallbackTopic = "/v1.0/agi/plugins/callback" //AI插件消息统一回调topic + BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic + PluginMessageCallbackTopic = "/v1.0/graph/api/invoke" //AI插件消息统一回调topic ) func GenerateMessageId(prefix string) string { diff --git a/plugin/model.go b/plugin/model.go index a8970f6..fdebdfa 100644 --- a/plugin/model.go +++ b/plugin/model.go @@ -4,17 +4,28 @@ import ( "encoding/json" ) -type PluginMessage struct { - PluginId string `json:"pluginId"` - PluginVersion string `json:"pluginVersion"` - AbilityKey string `json:"abilityKey"` - Data interface{} `json:"data"` - RequestId string `json:"requestId"` +type GraphRequestLine struct { + Method string `json:"method"` + Uri string `json:"uri"` +} +type GraphRequest struct { + RequestLine GraphRequestLine `json:"requestLine"` + Headers map[string]string `json:"headers"` + Body string `json:"body"` +} +type GraphStatusLine struct { + Code int `json:"code"` + Reason string `json:"reasonPhrase"` +} +type GraphResponse struct { + StatusLine GraphStatusLine `json:"statusLine"` + Headers map[string]string `json:"headers"` + Body string `json:"body"` } // 用于将数据转换成插件的请求参数 -func (req *PluginMessage) ParseRequest(pluginRequest interface{}) error { - data, err := json.Marshal(req.Data) +func (req *GraphRequest) ParseRequest(pluginRequest interface{}) error { + data, err := json.Marshal(req.Body) if err != nil { return err } diff --git a/plugin/plugin_handler.go b/plugin/plugin_handler.go index 994ff1b..12d5a81 100644 --- a/plugin/plugin_handler.go +++ b/plugin/plugin_handler.go @@ -11,7 +11,7 @@ type CallbackResponse struct { Response interface{} `json:"response"` } -type IPluginMessageHandler func(c context.Context, data *PluginMessage) (interface{}, error) +type IPluginMessageHandler func(c context.Context, data *GraphRequest) (*GraphResponse, error) type DefaultPluginFrameHandler struct { defaultHandler IPluginMessageHandler @@ -24,7 +24,7 @@ func NewDefaultPluginFrameHandler(defaultHandler IPluginMessageHandler) *Default } func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) { - msgData := &PluginMessage{} + msgData := &GraphRequest{} err := json.Unmarshal([]byte(df.Data), msgData) if err != nil { return nil, err @@ -38,8 +38,9 @@ func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *pay if err != nil { return nil, err } - pluginResponse := &PluginResponse{RequestId: msgData.RequestId, Result: result} - callbackResponse := &CallbackResponse{Response: pluginResponse} + result.StatusLine.Code = 200 + result.StatusLine.Reason = "OK" + callbackResponse := &CallbackResponse{Response: result} frameResp := payload.NewSuccessDataFrameResponse() if err = frameResp.SetJson(callbackResponse); err != nil { return nil, err