feat: 支持 AI 插件
Signed-off-by: Ke Jie <chzealot@gmail.com>
This commit is contained in:
parent
61a64cc101
commit
3134437c29
|
|
@ -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
|
||||
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"}]}`,
|
||||
}
|
||||
//执行插件
|
||||
echoResponse := Echo(echoRequest)
|
||||
return echoResponse, nil
|
||||
}
|
||||
return nil, nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// 事件处理
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ const (
|
|||
DataFrameResponseStatusCodeKHandlerNotFound = 404
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue