feat: 支持 AI 插件

Signed-off-by: Ke Jie <chzealot@gmail.com>
This commit is contained in:
Ke Jie 2023-10-18 17:33:20 +08:00
parent 61a64cc101
commit 3134437c29
4 changed files with 30 additions and 27 deletions

View File

@ -34,20 +34,11 @@ func OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackData
} }
// 简单的插件处理实现 // 简单的插件处理实现
func OnPluginMessageReceived(ctx context.Context, message *plugin.PluginMessage) (interface{}, error) { func OnPluginMessageReceived(ctx context.Context, request *plugin.GraphRequest) (*plugin.GraphResponse, error) {
//可以根据message中的PluginId、PluginVersion、AbilityKey路由到具体一个能力 response := &plugin.GraphResponse{
if message.AbilityKey == "echo" { 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"}]}`,
echoRequest := &EchoRequest{}
//将数据转换成插件的请求参数
err := message.ParseRequest(echoRequest)
if err != nil {
return nil, err
}
//执行插件
echoResponse := Echo(echoRequest)
return echoResponse, nil
} }
return nil, nil return response, nil
} }
// 事件处理 // 事件处理

View File

@ -24,8 +24,8 @@ const (
DataFrameResponseStatusCodeKInternalError = 500 DataFrameResponseStatusCodeKInternalError = 500
DataFrameResponseStatusCodeKHandlerNotFound = 404 DataFrameResponseStatusCodeKHandlerNotFound = 404
BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic
PluginMessageCallbackTopic = "/v1.0/agi/plugins/callback" //AI插件消息统一回调topic PluginMessageCallbackTopic = "/v1.0/graph/api/invoke" //AI插件消息统一回调topic
) )
func GenerateMessageId(prefix string) string { func GenerateMessageId(prefix string) string {

View File

@ -4,17 +4,28 @@ import (
"encoding/json" "encoding/json"
) )
type PluginMessage struct { type GraphRequestLine struct {
PluginId string `json:"pluginId"` Method string `json:"method"`
PluginVersion string `json:"pluginVersion"` Uri string `json:"uri"`
AbilityKey string `json:"abilityKey"` }
Data interface{} `json:"data"` type GraphRequest struct {
RequestId string `json:"requestId"` 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 { func (req *GraphRequest) ParseRequest(pluginRequest interface{}) error {
data, err := json.Marshal(req.Data) data, err := json.Marshal(req.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@ -11,7 +11,7 @@ type CallbackResponse struct {
Response interface{} `json:"response"` 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 { type DefaultPluginFrameHandler struct {
defaultHandler IPluginMessageHandler defaultHandler IPluginMessageHandler
@ -24,7 +24,7 @@ func NewDefaultPluginFrameHandler(defaultHandler IPluginMessageHandler) *Default
} }
func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) { func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) {
msgData := &PluginMessage{} msgData := &GraphRequest{}
err := json.Unmarshal([]byte(df.Data), msgData) err := json.Unmarshal([]byte(df.Data), msgData)
if err != nil { if err != nil {
return nil, err return nil, err
@ -38,8 +38,9 @@ func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *pay
if err != nil { if err != nil {
return nil, err return nil, err
} }
pluginResponse := &PluginResponse{RequestId: msgData.RequestId, Result: result} result.StatusLine.Code = 200
callbackResponse := &CallbackResponse{Response: pluginResponse} result.StatusLine.Reason = "OK"
callbackResponse := &CallbackResponse{Response: result}
frameResp := payload.NewSuccessDataFrameResponse() frameResp := payload.NewSuccessDataFrameResponse()
if err = frameResp.SetJson(callbackResponse); err != nil { if err = frameResp.SetJson(callbackResponse); err != nil {
return nil, err return nil, err