Skip to content

Commit f8bc8e3

Browse files
authored
Merge pull request #133 from Leizhenpeng/feat/support_built_in_roles
feat: support built in role list
2 parents 9ad9448 + 604572c commit f8bc8e3

File tree

13 files changed

+631
-182
lines changed

13 files changed

+631
-182
lines changed

code/go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ go 1.18
55
require github.com/larksuite/oapi-sdk-go/v3 v3.0.14
66

77
require (
8+
github.com/duke-git/lancet/v2 v2.1.17
89
github.com/gin-gonic/gin v1.8.2
910
github.com/google/uuid v1.3.0
11+
github.com/k0kubun/pp/v3 v3.2.0
1012
github.com/larksuite/oapi-sdk-gin v1.0.0
1113
github.com/patrickmn/go-cache v2.1.0+incompatible
1214
github.com/pion/opus v0.0.0-20230123082803-1052c3e89e58
1315
github.com/spf13/pflag v1.0.5
1416
github.com/spf13/viper v1.14.0
17+
gopkg.in/yaml.v2 v2.4.0
1518
)
1619

1720
require (
@@ -25,6 +28,7 @@ require (
2528
github.com/json-iterator/go v1.1.12 // indirect
2629
github.com/leodido/go-urn v1.2.1 // indirect
2730
github.com/magiconair/properties v1.8.7 // indirect
31+
github.com/mattn/go-colorable v0.1.13 // indirect
2832
github.com/mattn/go-isatty v0.0.17 // indirect
2933
github.com/mitchellh/mapstructure v1.5.0 // indirect
3034
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -37,11 +41,11 @@ require (
3741
github.com/subosito/gotenv v1.4.1 // indirect
3842
github.com/ugorji/go/codec v1.2.8 // indirect
3943
golang.org/x/crypto v0.5.0 // indirect
44+
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a // indirect
4045
golang.org/x/net v0.5.0 // indirect
4146
golang.org/x/sys v0.4.0 // indirect
4247
golang.org/x/text v0.6.0 // indirect
4348
google.golang.org/protobuf v1.28.1 // indirect
4449
gopkg.in/ini.v1 v1.67.0 // indirect
45-
gopkg.in/yaml.v2 v2.4.0 // indirect
4650
gopkg.in/yaml.v3 v3.0.1 // indirect
4751
)

code/go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
5050
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5151
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5252
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
53+
github.com/duke-git/lancet/v2 v2.1.17 h1:4u9oAGgmTPTt2D7AcjjLp0ubbcaQlova8xeTIuyupDw=
54+
github.com/duke-git/lancet/v2 v2.1.17/go.mod h1:hNcc06mV7qr+crH/0nP+rlC3TB0Q9g5OrVnO8/TGD4c=
5355
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
5456
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
5557
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -147,6 +149,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
147149
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
148150
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
149151
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
152+
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
153+
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
150154
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
151155
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
152156
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -165,6 +169,9 @@ github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
165169
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
166170
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
167171
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
172+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
173+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
174+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
168175
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
169176
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
170177
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
@@ -249,6 +256,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
249256
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
250257
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
251258
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
259+
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
260+
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
252261
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
253262
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
254263
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

code/handlers/card_clear_action.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
6+
"start-feishubot/services"
7+
)
8+
9+
func NewClearCardHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc {
10+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
11+
if cardMsg.Kind == ClearCardKind {
12+
newCard, err, done := CommonProcessClearCache(cardMsg, m.sessionCache)
13+
if done {
14+
return newCard, err
15+
}
16+
return nil, nil
17+
}
18+
return nil, ErrNextHandler
19+
}
20+
}
21+
22+
func CommonProcessClearCache(cardMsg CardMsg, session services.SessionServiceCacheInterface) (
23+
interface{}, error, bool) {
24+
if cardMsg.Value == "1" {
25+
session.Clear(cardMsg.SessionId)
26+
newCard, _ := newSendCard(
27+
withHeader("️🆑 机器人提醒", larkcard.TemplateGrey),
28+
withMainMd("已删除此话题的上下文信息"),
29+
withNote("我们可以开始一个全新的话题,继续找我聊天吧"),
30+
)
31+
//fmt.Printf("session: %v", newCard)
32+
return newCard, nil, true
33+
}
34+
if cardMsg.Value == "0" {
35+
newCard, _ := newSendCard(
36+
withHeader("️🆑 机器人提醒", larkcard.TemplateGreen),
37+
withMainMd("依旧保留此话题的上下文信息"),
38+
withNote("我们可以继续探讨这个话题,期待和您聊天。如果您有其他问题或者想要讨论的话题,请告诉我哦"),
39+
)
40+
return newCard, nil, true
41+
}
42+
return nil, nil, false
43+
}

code/handlers/card_common_action.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
8+
)
9+
10+
type CardHandlerMeta func(cardMsg CardMsg, m MessageHandler) CardHandlerFunc
11+
12+
type CardHandlerFunc func(ctx context.Context, cardAction *larkcard.CardAction) (
13+
interface{}, error)
14+
15+
var ErrNextHandler = fmt.Errorf("next handler")
16+
17+
func NewCardHandler(m MessageHandler) CardHandlerFunc {
18+
handlers := []CardHandlerMeta{
19+
NewClearCardHandler,
20+
NewPicResolutionHandler,
21+
NewPicTextMoreHandler,
22+
NewPicModeChangeHandler,
23+
NewRoleTagCardHandler,
24+
NewRoleCardHandler,
25+
}
26+
27+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
28+
var cardMsg CardMsg
29+
actionValue := cardAction.Action.Value
30+
actionValueJson, _ := json.Marshal(actionValue)
31+
json.Unmarshal(actionValueJson, &cardMsg)
32+
//pp.Println(cardMsg)
33+
for _, handler := range handlers {
34+
h := handler(cardMsg, m)
35+
i, err := h(ctx, cardAction)
36+
if err == ErrNextHandler {
37+
continue
38+
}
39+
return i, err
40+
}
41+
return nil, nil
42+
}
43+
}

code/handlers/card_pic_action.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
6+
"start-feishubot/services"
7+
)
8+
9+
func NewPicResolutionHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc {
10+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
11+
if cardMsg.Kind == PicResolutionKind {
12+
CommonProcessPicResolution(cardMsg, cardAction, m.sessionCache)
13+
return nil, nil
14+
}
15+
return nil, ErrNextHandler
16+
}
17+
}
18+
19+
func NewPicModeChangeHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc {
20+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
21+
if cardMsg.Kind == PicModeChangeKind {
22+
newCard, err, done := CommonProcessPicModeChange(cardMsg, m.sessionCache)
23+
if done {
24+
return newCard, err
25+
}
26+
return nil, nil
27+
}
28+
return nil, ErrNextHandler
29+
}
30+
}
31+
func NewPicTextMoreHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc {
32+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
33+
if cardMsg.Kind == PicTextMoreKind {
34+
go func() {
35+
m.CommonProcessPicMore(cardMsg)
36+
}()
37+
return nil, nil
38+
}
39+
return nil, ErrNextHandler
40+
}
41+
}
42+
43+
func CommonProcessPicResolution(msg CardMsg,
44+
cardAction *larkcard.CardAction,
45+
cache services.SessionServiceCacheInterface) {
46+
option := cardAction.Action.Option
47+
//fmt.Println(larkcore.Prettify(msg))
48+
cache.SetPicResolution(msg.SessionId, services.Resolution(option))
49+
//send text
50+
replyMsg(context.Background(), "已更新图片分辨率为"+option,
51+
&msg.MsgId)
52+
}
53+
54+
func (m MessageHandler) CommonProcessPicMore(msg CardMsg) {
55+
resolution := m.sessionCache.GetPicResolution(msg.SessionId)
56+
//fmt.Println("resolution: ", resolution)
57+
//fmt.Println("msg: ", msg)
58+
question := msg.Value.(string)
59+
bs64, _ := m.gpt.GenerateOneImage(question, resolution)
60+
replayImageCardByBase64(context.Background(), bs64, &msg.MsgId,
61+
&msg.SessionId, question)
62+
}
63+
64+
func CommonProcessPicModeChange(cardMsg CardMsg,
65+
session services.SessionServiceCacheInterface) (
66+
interface{}, error, bool) {
67+
if cardMsg.Value == "1" {
68+
69+
sessionId := cardMsg.SessionId
70+
session.Clear(sessionId)
71+
session.SetMode(sessionId,
72+
services.ModePicCreate)
73+
session.SetPicResolution(sessionId,
74+
services.Resolution256)
75+
76+
newCard, _ :=
77+
newSendCard(
78+
withHeader("🖼️ 已进入图片创作模式", larkcard.TemplateBlue),
79+
withPicResolutionBtn(&sessionId),
80+
withNote("提醒:回复文本或图片,让AI生成相关的图片。"))
81+
return newCard, nil, true
82+
}
83+
if cardMsg.Value == "0" {
84+
newCard, _ := newSendCard(
85+
withHeader("️🎒 机器人提醒", larkcard.TemplateGreen),
86+
withMainMd("依旧保留此话题的上下文信息"),
87+
withNote("我们可以继续探讨这个话题,期待和您聊天。如果您有其他问题或者想要讨论的话题,请告诉我哦"),
88+
)
89+
return newCard, nil, true
90+
}
91+
return nil, nil, false
92+
}

code/handlers/card_role_action.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
6+
"start-feishubot/initialization"
7+
"start-feishubot/services"
8+
"start-feishubot/services/openai"
9+
)
10+
11+
func NewRoleTagCardHandler(cardMsg CardMsg,
12+
m MessageHandler) CardHandlerFunc {
13+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
14+
15+
if cardMsg.Kind == RoleTagsChooseKind {
16+
newCard, err, done := CommonProcessRoleTag(cardMsg, cardAction,
17+
m.sessionCache)
18+
if done {
19+
return newCard, err
20+
}
21+
return nil, nil
22+
}
23+
return nil, ErrNextHandler
24+
}
25+
}
26+
27+
func NewRoleCardHandler(cardMsg CardMsg,
28+
m MessageHandler) CardHandlerFunc {
29+
return func(ctx context.Context, cardAction *larkcard.CardAction) (interface{}, error) {
30+
31+
if cardMsg.Kind == RoleChooseKind {
32+
newCard, err, done := CommonProcessRole(cardMsg, cardAction,
33+
m.sessionCache)
34+
if done {
35+
return newCard, err
36+
}
37+
return nil, nil
38+
}
39+
return nil, ErrNextHandler
40+
}
41+
}
42+
43+
func CommonProcessRoleTag(msg CardMsg, cardAction *larkcard.CardAction,
44+
cache services.SessionServiceCacheInterface) (interface{},
45+
error, bool) {
46+
option := cardAction.Action.Option
47+
//replyMsg(context.Background(), "已选择tag:"+option,
48+
// &msg.MsgId)
49+
roles := initialization.GetTitleListByTag(option)
50+
//fmt.Printf("roles: %s", roles)
51+
SendRoleListCard(context.Background(), &msg.SessionId,
52+
&msg.MsgId, option, *roles)
53+
return nil, nil, true
54+
}
55+
56+
func CommonProcessRole(msg CardMsg, cardAction *larkcard.CardAction,
57+
cache services.SessionServiceCacheInterface) (interface{},
58+
error, bool) {
59+
option := cardAction.Action.Option
60+
contentByTitle, error := initialization.GetFirstRoleContentByTitle(option)
61+
if error != nil {
62+
return nil, error, true
63+
}
64+
cache.Clear(msg.SessionId)
65+
systemMsg := append([]openai.Messages{}, openai.Messages{
66+
Role: "system", Content: contentByTitle,
67+
})
68+
cache.SetMsg(msg.SessionId, systemMsg)
69+
//pp.Println("systemMsg: ", systemMsg)
70+
sendSystemInstructionCard(context.Background(), &msg.SessionId,
71+
&msg.MsgId, contentByTitle)
72+
//replyMsg(context.Background(), "已选择角色:"+contentByTitle,
73+
// &msg.MsgId)
74+
return nil, nil, true
75+
}

code/handlers/event_common_action.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
7+
"start-feishubot/initialization"
78
"start-feishubot/services/openai"
89
"start-feishubot/utils"
910
)
@@ -130,3 +131,23 @@ func (*BalanceAction) Execute(a *ActionInfo) bool {
130131
}
131132
return true
132133
}
134+
135+
type RoleListAction struct { /*角色列表*/
136+
}
137+
138+
func (*RoleListAction) Execute(a *ActionInfo) bool {
139+
if _, foundSystem := utils.EitherTrimEqual(a.info.qParsed,
140+
"/roles", "角色列表"); foundSystem {
141+
//a.handler.sessionCache.Clear(*a.info.sessionId)
142+
//systemMsg := append([]openai.Messages{}, openai.Messages{
143+
// Role: "system", Content: system,
144+
//})
145+
//a.handler.sessionCache.SetMsg(*a.info.sessionId, systemMsg)
146+
//sendSystemInstructionCard(*a.ctx, a.info.sessionId,
147+
// a.info.msgId, system)
148+
tags := initialization.GetAllUniqueTags()
149+
SendRoleTagsCard(*a.ctx, a.info.sessionId, a.info.msgId, *tags)
150+
return false
151+
}
152+
return true
153+
}

0 commit comments

Comments
 (0)