-
Notifications
You must be signed in to change notification settings - Fork 953
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from Leizhenpeng/feat/support_built_in_roles
feat: support built in role list
- Loading branch information
Showing
13 changed files
with
631 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
larkcard "github.com/larksuite/oapi-sdk-go/v3/card" | ||
"start-feishubot/services" | ||
) | ||
|
||
func NewClearCardHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc { | ||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
if cardMsg.Kind == ClearCardKind { | ||
newCard, err, done := CommonProcessClearCache(cardMsg, m.sessionCache) | ||
if done { | ||
return newCard, err | ||
} | ||
return nil, nil | ||
} | ||
return nil, ErrNextHandler | ||
} | ||
} | ||
|
||
func CommonProcessClearCache(cardMsg CardMsg, session services.SessionServiceCacheInterface) ( | ||
interface{}, error, bool) { | ||
if cardMsg.Value == "1" { | ||
session.Clear(cardMsg.SessionId) | ||
newCard, _ := newSendCard( | ||
withHeader("️🆑 机器人提醒", larkcard.TemplateGrey), | ||
withMainMd("已删除此话题的上下文信息"), | ||
withNote("我们可以开始一个全新的话题,继续找我聊天吧"), | ||
) | ||
//fmt.Printf("session: %v", newCard) | ||
return newCard, nil, true | ||
} | ||
if cardMsg.Value == "0" { | ||
newCard, _ := newSendCard( | ||
withHeader("️🆑 机器人提醒", larkcard.TemplateGreen), | ||
withMainMd("依旧保留此话题的上下文信息"), | ||
withNote("我们可以继续探讨这个话题,期待和您聊天。如果您有其他问题或者想要讨论的话题,请告诉我哦"), | ||
) | ||
return newCard, nil, true | ||
} | ||
return nil, nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
larkcard "github.com/larksuite/oapi-sdk-go/v3/card" | ||
) | ||
|
||
type CardHandlerMeta func(cardMsg CardMsg, m MessageHandler) CardHandlerFunc | ||
|
||
type CardHandlerFunc func(ctx context.Context, cardAction *larkcard.CardAction) ( | ||
interface{}, error) | ||
|
||
var ErrNextHandler = fmt.Errorf("next handler") | ||
|
||
func NewCardHandler(m MessageHandler) CardHandlerFunc { | ||
handlers := []CardHandlerMeta{ | ||
NewClearCardHandler, | ||
NewPicResolutionHandler, | ||
NewPicTextMoreHandler, | ||
NewPicModeChangeHandler, | ||
NewRoleTagCardHandler, | ||
NewRoleCardHandler, | ||
} | ||
|
||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
var cardMsg CardMsg | ||
actionValue := cardAction.Action.Value | ||
actionValueJson, _ := json.Marshal(actionValue) | ||
json.Unmarshal(actionValueJson, &cardMsg) | ||
//pp.Println(cardMsg) | ||
for _, handler := range handlers { | ||
h := handler(cardMsg, m) | ||
i, err := h(ctx, cardAction) | ||
if err == ErrNextHandler { | ||
continue | ||
} | ||
return i, err | ||
} | ||
return nil, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
larkcard "github.com/larksuite/oapi-sdk-go/v3/card" | ||
"start-feishubot/services" | ||
) | ||
|
||
func NewPicResolutionHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc { | ||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
if cardMsg.Kind == PicResolutionKind { | ||
CommonProcessPicResolution(cardMsg, cardAction, m.sessionCache) | ||
return nil, nil | ||
} | ||
return nil, ErrNextHandler | ||
} | ||
} | ||
|
||
func NewPicModeChangeHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc { | ||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
if cardMsg.Kind == PicModeChangeKind { | ||
newCard, err, done := CommonProcessPicModeChange(cardMsg, m.sessionCache) | ||
if done { | ||
return newCard, err | ||
} | ||
return nil, nil | ||
} | ||
return nil, ErrNextHandler | ||
} | ||
} | ||
func NewPicTextMoreHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc { | ||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
if cardMsg.Kind == PicTextMoreKind { | ||
go func() { | ||
m.CommonProcessPicMore(cardMsg) | ||
}() | ||
return nil, nil | ||
} | ||
return nil, ErrNextHandler | ||
} | ||
} | ||
|
||
func CommonProcessPicResolution(msg CardMsg, | ||
cardAction *larkcard.CardAction, | ||
cache services.SessionServiceCacheInterface) { | ||
option := cardAction.Action.Option | ||
//fmt.Println(larkcore.Prettify(msg)) | ||
cache.SetPicResolution(msg.SessionId, services.Resolution(option)) | ||
//send text | ||
replyMsg(context.Background(), "已更新图片分辨率为"+option, | ||
&msg.MsgId) | ||
} | ||
|
||
func (m MessageHandler) CommonProcessPicMore(msg CardMsg) { | ||
resolution := m.sessionCache.GetPicResolution(msg.SessionId) | ||
//fmt.Println("resolution: ", resolution) | ||
//fmt.Println("msg: ", msg) | ||
question := msg.Value.(string) | ||
bs64, _ := m.gpt.GenerateOneImage(question, resolution) | ||
replayImageCardByBase64(context.Background(), bs64, &msg.MsgId, | ||
&msg.SessionId, question) | ||
} | ||
|
||
func CommonProcessPicModeChange(cardMsg CardMsg, | ||
session services.SessionServiceCacheInterface) ( | ||
interface{}, error, bool) { | ||
if cardMsg.Value == "1" { | ||
|
||
sessionId := cardMsg.SessionId | ||
session.Clear(sessionId) | ||
session.SetMode(sessionId, | ||
services.ModePicCreate) | ||
session.SetPicResolution(sessionId, | ||
services.Resolution256) | ||
|
||
newCard, _ := | ||
newSendCard( | ||
withHeader("🖼️ 已进入图片创作模式", larkcard.TemplateBlue), | ||
withPicResolutionBtn(&sessionId), | ||
withNote("提醒:回复文本或图片,让AI生成相关的图片。")) | ||
return newCard, nil, true | ||
} | ||
if cardMsg.Value == "0" { | ||
newCard, _ := newSendCard( | ||
withHeader("️🎒 机器人提醒", larkcard.TemplateGreen), | ||
withMainMd("依旧保留此话题的上下文信息"), | ||
withNote("我们可以继续探讨这个话题,期待和您聊天。如果您有其他问题或者想要讨论的话题,请告诉我哦"), | ||
) | ||
return newCard, nil, true | ||
} | ||
return nil, nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
larkcard "github.com/larksuite/oapi-sdk-go/v3/card" | ||
"start-feishubot/initialization" | ||
"start-feishubot/services" | ||
"start-feishubot/services/openai" | ||
) | ||
|
||
func NewRoleTagCardHandler(cardMsg CardMsg, | ||
m MessageHandler) CardHandlerFunc { | ||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
|
||
if cardMsg.Kind == RoleTagsChooseKind { | ||
newCard, err, done := CommonProcessRoleTag(cardMsg, cardAction, | ||
m.sessionCache) | ||
if done { | ||
return newCard, err | ||
} | ||
return nil, nil | ||
} | ||
return nil, ErrNextHandler | ||
} | ||
} | ||
|
||
func NewRoleCardHandler(cardMsg CardMsg, | ||
m MessageHandler) CardHandlerFunc { | ||
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) { | ||
|
||
if cardMsg.Kind == RoleChooseKind { | ||
newCard, err, done := CommonProcessRole(cardMsg, cardAction, | ||
m.sessionCache) | ||
if done { | ||
return newCard, err | ||
} | ||
return nil, nil | ||
} | ||
return nil, ErrNextHandler | ||
} | ||
} | ||
|
||
func CommonProcessRoleTag(msg CardMsg, cardAction *larkcard.CardAction, | ||
cache services.SessionServiceCacheInterface) (interface{}, | ||
error, bool) { | ||
option := cardAction.Action.Option | ||
//replyMsg(context.Background(), "已选择tag:"+option, | ||
// &msg.MsgId) | ||
roles := initialization.GetTitleListByTag(option) | ||
//fmt.Printf("roles: %s", roles) | ||
SendRoleListCard(context.Background(), &msg.SessionId, | ||
&msg.MsgId, option, *roles) | ||
return nil, nil, true | ||
} | ||
|
||
func CommonProcessRole(msg CardMsg, cardAction *larkcard.CardAction, | ||
cache services.SessionServiceCacheInterface) (interface{}, | ||
error, bool) { | ||
option := cardAction.Action.Option | ||
contentByTitle, error := initialization.GetFirstRoleContentByTitle(option) | ||
if error != nil { | ||
return nil, error, true | ||
} | ||
cache.Clear(msg.SessionId) | ||
systemMsg := append([]openai.Messages{}, openai.Messages{ | ||
Role: "system", Content: contentByTitle, | ||
}) | ||
cache.SetMsg(msg.SessionId, systemMsg) | ||
//pp.Println("systemMsg: ", systemMsg) | ||
sendSystemInstructionCard(context.Background(), &msg.SessionId, | ||
&msg.MsgId, contentByTitle) | ||
//replyMsg(context.Background(), "已选择角色:"+contentByTitle, | ||
// &msg.MsgId) | ||
return nil, nil, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.