Skip to content

Commit

Permalink
Merge pull request #133 from Leizhenpeng/feat/support_built_in_roles
Browse files Browse the repository at this point in the history
feat: support built in role list
  • Loading branch information
Leizhenpeng authored Mar 28, 2023
2 parents 9ad9448 + 604572c commit f8bc8e3
Show file tree
Hide file tree
Showing 13 changed files with 631 additions and 182 deletions.
6 changes: 5 additions & 1 deletion code/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ go 1.18
require github.com/larksuite/oapi-sdk-go/v3 v3.0.14

require (
github.com/duke-git/lancet/v2 v2.1.17
github.com/gin-gonic/gin v1.8.2
github.com/google/uuid v1.3.0
github.com/k0kubun/pp/v3 v3.2.0
github.com/larksuite/oapi-sdk-gin v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pion/opus v0.0.0-20230123082803-1052c3e89e58
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand All @@ -25,6 +28,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -37,11 +41,11 @@ require (
github.com/subosito/gotenv v1.4.1 // indirect
github.com/ugorji/go/codec v1.2.8 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions code/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/duke-git/lancet/v2 v2.1.17 h1:4u9oAGgmTPTt2D7AcjjLp0ubbcaQlova8xeTIuyupDw=
github.com/duke-git/lancet/v2 v2.1.17/go.mod h1:hNcc06mV7qr+crH/0nP+rlC3TB0Q9g5OrVnO8/TGD4c=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -147,6 +149,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -165,6 +169,9 @@ github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down Expand Up @@ -249,6 +256,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
43 changes: 43 additions & 0 deletions code/handlers/card_clear_action.go
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
}
43 changes: 43 additions & 0 deletions code/handlers/card_common_action.go
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
}
}
92 changes: 92 additions & 0 deletions code/handlers/card_pic_action.go
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
}
75 changes: 75 additions & 0 deletions code/handlers/card_role_action.go
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
}
21 changes: 21 additions & 0 deletions code/handlers/event_common_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"start-feishubot/initialization"
"start-feishubot/services/openai"
"start-feishubot/utils"
)
Expand Down Expand Up @@ -130,3 +131,23 @@ func (*BalanceAction) Execute(a *ActionInfo) bool {
}
return true
}

type RoleListAction struct { /*角色列表*/
}

func (*RoleListAction) Execute(a *ActionInfo) bool {
if _, foundSystem := utils.EitherTrimEqual(a.info.qParsed,
"/roles", "角色列表"); foundSystem {
//a.handler.sessionCache.Clear(*a.info.sessionId)
//systemMsg := append([]openai.Messages{}, openai.Messages{
// Role: "system", Content: system,
//})
//a.handler.sessionCache.SetMsg(*a.info.sessionId, systemMsg)
//sendSystemInstructionCard(*a.ctx, a.info.sessionId,
// a.info.msgId, system)
tags := initialization.GetAllUniqueTags()
SendRoleTagsCard(*a.ctx, a.info.sessionId, a.info.msgId, *tags)
return false
}
return true
}
Loading

0 comments on commit f8bc8e3

Please sign in to comment.