repo_name stringlengths 1 62 | dataset stringclasses 1
value | lang stringclasses 11
values | pr_id int64 1 20.1k | owner stringlengths 2 34 | reviewer stringlengths 2 39 | diff_hunk stringlengths 15 262k | code_review_comment stringlengths 1 99.6k |
|---|---|---|---|---|---|---|---|
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,117 @@
+package smr
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+
+ "github.com/nekomeowww/insights-bot/internal/services/smr/types"
+ "github.com/redis/rueidis"
+ "github.com/sourcegraph/conc/pool"
+)
+
+func (s *Service) AddTask(taskInfo types.TaskInfo) error {
+ result, err := json.Marshal(&taskI... | 用 context.CancelFunc 关? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,117 @@
+package smr
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+
+ "github.com/nekomeowww/insights-bot/internal/services/smr/types"
+ "github.com/redis/rueidis"
+ "github.com/sourcegraph/conc/pool"
+)
+
+func (s *Service) AddTask(taskInfo types.TaskInfo) error {
+ result, err := json.Marshal(&taskI... | 可以放 select 上面方便读 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -12,5 +13,6 @@ func NewModules() fx.Option {
fx.Provide(health.NewHealth()),
fx.Provide(pprof.NewPprof()),
fx.Provide(autorecap.NewAutoRecapService()),
+ fx.Provide(smr.NewService()), | 其实我没有太理解为什么要单独加一个 service,是为了避免并发造成的 OOM 吗? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -39,20 +31,16 @@ func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
return nil, tgbot.NewExceptionError(err)
}
- ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
- defer cancel()
+ err = h.smrService.AddTask(types.TaskInfo{
+ Platform: smr.FromPlatformTelegram,
+ U... | URL |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,157 @@
+package handlers
+
+import (
+ "context"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nek... | ```suggestion
h.logger.WithField("error", err.Error()).Warn("failed to bind request body, type definition of slack request body may have changed")
``` |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,157 @@
+package handlers
+
+import (
+ "context"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nek... | ```suggestion
}).Trace("slack: command received: /smr %s", body.Text)
``` |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,157 @@
+package handlers
+
+import (
+ "context"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nek... | 这种也可以用 AbortWithStatus 么? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -1,34 +1,26 @@
package summarize
import (
- "context"
- "errors"
- "net/url"
- "time"
-
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
- "github.com/samber/lo"
-
"github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "gi... | ```suggestion
return nil, tgbot.NewExceptionError(err).WithReply(c.Update.Message)
``` |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -39,20 +31,16 @@ func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
return nil, tgbot.NewExceptionError(err)
}
- ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
- defer cancel()
+ err = h.smrQueue.AddTask(types.TaskInfo{
+ Platform: smr.FromPlatformTelegram,
+ Url... | ```suggestion
return nil, tgbot.NewExceptionError(err).WithMessage("量子速读失败了。可以再试试?").WithEdit(&processingMessage)
```
文案上不需要包含这种细节,可以用 ExceptionError + err 传递的方式处理掉 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,9 @@
+package smr
+
+import "errors"
+
+var (
+ ErrNoLink = errors.New("没有找到链接,可以发送一个有效的链接吗?用法:/smr <链接>")
+ ErrParse = errors.New("你发来的链接无法被理解,可以重新发一个试试。用法:/smr <链接>")
+ ErrScheme = errors.New("你发来的链接无法被理解,可以重新发一个试试。用法:/smr <链接>") | 最好不要把这种有程序错误作为上游的错误文案放到 error 里面,文案应该是业务 specific 的。比如 Telegram 的 `/smr <链接>` 应该用 `<code></code>` 扩起来,其他平台不行。 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,129 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "time"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/snowflake/v2"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+ "github.com/nekomeowww/insights-bot/i... | 直接写 default 吧 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,129 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "time"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/snowflake/v2"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+ "github.com/nekomeowww/insights-bot/i... | 太长了,链式调用换个行吧 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,129 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "time"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/snowflake/v2"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+ "github.com/nekomeowww/insights-bot/i... | ```suggestion
s.logger.WithError(err).
WithField("platform", info.Platform).
Warn("smr service: failed to get team's access token")
``` |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,129 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "time"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/snowflake/v2"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+ "github.com/nekomeowww/insights-bot/i... | ```suggestion
s.logger.WithError(err).
WithField("platform", info.Platform).
Warn("smr service: failed to send result message")
``` |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,129 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "time"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/snowflake/v2"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+ "github.com/nekomeowww/insights-bot/i... | ```suggestion
s.logger.WithError(err).
WithField("platform", info.Platform).
Warn("smr service: failed to send result message")
``` |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,129 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "time"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/snowflake/v2"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+ "github.com/nekomeowww/insights-bot/i... | 返回 bool 的函数用动词 verb 开头,isBotExists 或者 hasBot |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,160 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.c... | 用 lazy init 的 `cancelFunc context.CancelFunc` 来解决呗? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,160 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.c... | 用 `context.Context.Done()` 可以吗 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,160 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.c... | 会 busy loop 吗? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,55 @@
+package types
+
+import (
+ "sync"
+
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+)
+
+type TaskInfo struct {
+ Platform smr.FromPlatform `json:"platform"`
+ Url string `json:"url"` // url to summarize
+
+ ChatID int64 `json:"chatID"` // only for telegram
+ MessageI... | `internal/services/smr/smr.go` L100 又定义了一个 pool,和这个有啥区别? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,55 @@
+package types
+
+import (
+ "sync"
+
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+)
+
+type TaskInfo struct {
+ Platform smr.FromPlatform `json:"platform"`
+ Url string `json:"url"` // url to summarize
+
+ ChatID int64 `json:"chatID"` // only for telegram
+ MessageI... | 能拆成三个附属结构体的吗? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,92 @@
+package discordbot
+
+import (
+ "context"
+ "crypto/ed25519"
+ "errors"
+ "net"
+
+ "github.com/disgoorg/disgo"
+ "github.com/disgoorg/disgo/bot"
+ "github.com/disgoorg/disgo/httpserver"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "githu... | 这个泛型写的我有点懵 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,29 @@
+package smrutils
+
+import (
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/services/smr"
+ "net/url"
+
+ "github.com/samber/lo"
+)
+
+func CheckUrl(urlString string) error {
+ if urlString == "" {
+ return smr.ErrNoLink
+ }
+
+ parsedURL, err2 := url.Parse(urlString)
+ if err2 != nil {
+ ... | 能正确传递 err 链吗?写成这样的话外面怎么打日志呢? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,160 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.c... | 用 if err := recover; err != nil { 不就不会影响到 scope 了? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,160 @@
+package smr
+
+import (
+ "context"
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.c... | 无论如何都要 continue 的话就把表达式反转一下把日志塞进去呗 |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,161 @@
+package handlers
+
+import (
+ "context"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nek... | 能用 `ctx.AbortWithStatusJSON()` 么? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,161 @@
+package handlers
+
+import (
+ "context"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nek... | 能用 `ctx.AbortWithStatusJSON()` 么? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -0,0 +1,161 @@
+package handlers
+
+import (
+ "context"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
+ "github.com/nekomeowww/insights-bot/internal/services/smr/smrutils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nek... | 能用 `ctx.AbortWithStatusJSON()` 么? |
insights-bot | github_2023 | go | 119 | nekomeowww | nekomeowww | @@ -31,105 +29,43 @@ type NewSlackBotParam struct {
Config *configs.Config
Logger *logger.Logger
- SMR *smr.Model
-
- Ent *datastore.Ent
+ Handlers *handlers.Handlers
+ Services *services.Services
}
-var _ healthchecker.HealthChecker = (*Bot)(nil)
-
-type Bot struct {
- config *configs.Config
- logger *logger.... | Lifecycle 本身有 timeout 信号的,不用自己写 |
insights-bot | github_2023 | others | 91 | nekomeowww | nekomeowww | @@ -31,6 +31,7 @@ A bot works with OpenAI GPT models to provide insights for your info flows.
## Support IMs | 把 #78 完成了 |
insights-bot | github_2023 | others | 91 | nekomeowww | nekomeowww | @@ -176,19 +177,22 @@ docker buildx build --platform linux/arm64,linux/amd64 -t <tag> -f Dockerfile .
### Environment variables
-| Name | Required | Default | Description |
-| ---- | -------- | ------- | ----------- |
-| `TELEGRAM_BOT_TOKEN` | `true` | | Telegram Bot API token, you can create one and obtain the to... | 必须是 6060 吗?能递增吗? |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -0,0 +1,107 @@
+package discord
+
+import (
+ "context"
+ "crypto/ed25519"
+ "github.com/samber/lo"
+ "net"
+
+ "github.com/disgoorg/disgo"
+ "github.com/disgoorg/disgo/bot"
+ "github.com/disgoorg/disgo/httpserver"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal... | 实现一下 `pkg/healthchecker` 的 `healthchecker.HealthChecker`,并且增加 health check 模块到 `internal/services/health` 里面 |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -30,11 +31,13 @@ func main() {
fx.Options(services.NewModules()),
fx.Options(telegram.NewModules()),
fx.Options(slack.NewModules()),
+ fx.Options(discord.NewModules()), | gofmt |
insights-bot | github_2023 | others | 91 | nekomeowww | nekomeowww | @@ -31,6 +34,9 @@ services:
- OPENAI_API_HOST
- SLACK_CLIENT_ID
- SLACK_CLIENT_SECRET
+ - DISCORD_BOT_TOKEN
+ - DISCORD_BOT_PUBLIC_KEY
+ - DISCORD_BOT_WEBHOOK_PORT | 加一下 ports mapping |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -0,0 +1,107 @@
+package discord
+
+import (
+ "context"
+ "crypto/ed25519"
+ "github.com/samber/lo"
+ "net"
+
+ "github.com/disgoorg/disgo"
+ "github.com/disgoorg/disgo/bot"
+ "github.com/disgoorg/disgo/httpserver"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal... | embedding 和其他的字段中间需要间隔一行 |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -0,0 +1,107 @@
+package discord
+
+import (
+ "context"
+ "crypto/ed25519"
+ "github.com/samber/lo"
+ "net"
+
+ "github.com/disgoorg/disgo"
+ "github.com/disgoorg/disgo/bot"
+ "github.com/disgoorg/disgo/httpserver"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal... | 按照当前 repo 的规范的话应该叫 `NewXXXParam(s)` |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -0,0 +1,92 @@
+package discord
+
+import (
+ "context"
+ "errors"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "net/url"
+
+ "github.com/disgoorg/disgo/discord"
+ "github.com/disgoorg/disgo/events"
+ "github.com/samber/lo"
+)
+
+func (b *DiscordBot) smrCmd(event *events.ApplicationCommandInteractionC... | 这段是之前也写过一遍吗?会需要封装么 |
insights-bot | github_2023 | others | 91 | nekomeowww | nekomeowww | @@ -10,9 +10,13 @@ services:
- OPENAI_API_HOST
- SLACK_CLIENT_ID
- SLACK_CLIENT_SECRET
+ - DISCORD_BOT_TOKEN
+ - DISCORD_BOT_PUBLIC_KEY
+ - DISCORD_BOT_WEBHOOK_PORT
- DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable
... | 草?这样可以的嘛? |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -0,0 +1,108 @@
+package discord
+
+import (
+ "context"
+ "crypto/ed25519"
+ "github.com/samber/lo"
+ "net"
+
+ "github.com/disgoorg/disgo"
+ "github.com/disgoorg/disgo/bot"
+ "github.com/disgoorg/disgo/httpserver"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal... | 要换行的话就都换了 |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -0,0 +1,108 @@
+package discord
+
+import (
+ "context"
+ "crypto/ed25519"
+ "github.com/samber/lo"
+ "net"
+
+ "github.com/disgoorg/disgo"
+ "github.com/disgoorg/disgo/bot"
+ "github.com/disgoorg/disgo/httpserver"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal... | 实现一下 pkg/healthchecker 的 healthchecker.HealthChecker,并且增加 health check 模块到 internal/services/health 里面 |
insights-bot | github_2023 | go | 91 | nekomeowww | nekomeowww | @@ -44,6 +46,10 @@ func NewHealth() func(NewHealthParams) (*Health, error) {
Name: "telegram_bot",
Check: params.TelegramBot.Check,
}),
+ health.WithCheck(health.Check{
+ Name: "discord_bot",
+ Check: params.DiscordBot.Check,
+ }), | 得学 slack 那样 append?而且如果不启动 Discord 的话 .Check 会变成访问空指针的属性? |
insights-bot | github_2023 | go | 72 | nekomeowww | nekomeowww | @@ -24,7 +24,7 @@ func checkTogglingRecapPermission(chatID, userID int64, update tgbotapi.Update,
telegram.MemberStatusCreator,
telegram.MemberStatusAdministrator,
}, telegram.MemberStatus(member.Status)) {
- return tgbot.NewMessageError("你没有权限关闭聊天记录回顾功能哦!").WithReply(update.Message)
+ return tgbot.NewMessage... | 不行,需要返回一下 true/false 让外部决定要返回什么...或者让外部穿参数进来 |
insights-bot | github_2023 | go | 61 | nekomeowww | nekomeowww | @@ -82,11 +86,49 @@ func FullNameFromFirstAndLastName(firstName, lastName string) string {
}
func ExtractTextFromMessage(message *tgbotapi.Message) string {
- if message.Caption != "" {
- return message.Caption
+ text := lo.Ternary(message.Caption != "", message.Caption, message.Text)
+ type MarkdownLink struct { | ```suggestion
type MarkdownLink struct {
``` |
insights-bot | github_2023 | go | 61 | nekomeowww | nekomeowww | @@ -82,11 +86,49 @@ func FullNameFromFirstAndLastName(firstName, lastName string) string {
}
func ExtractTextFromMessage(message *tgbotapi.Message) string {
- if message.Caption != "" {
- return message.Caption
+ text := lo.Ternary(message.Caption != "", message.Caption, message.Text)
+ type MarkdownLink struct {
... | snake_case is not idiomatic in Go, should change to camelCase |
insights-bot | github_2023 | go | 61 | nekomeowww | nekomeowww | @@ -4,12 +4,12 @@ import (
"fmt"
"testing"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/stretchr/testify/assert"
)
func TestReplaceMarkdownTitlesToBoldTexts(t *testing.T) {
prefix := ""
-
for i := 0; i < 6; i++ { | ```suggestion
for i := 0; i < 6; i++ {
``` |
insights-bot | github_2023 | go | 61 | nekomeowww | nekomeowww | @@ -82,11 +86,51 @@ func FullNameFromFirstAndLastName(firstName, lastName string) string {
}
func ExtractTextFromMessage(message *tgbotapi.Message) string {
- if message.Caption != "" {
- return message.Caption
+ text := lo.Ternary(message.Caption != "", message.Caption, message.Text)
+
+ type MarkdownLink struct ... | This err should be logged, but this err doesnt really matter... How about adding an err field to MarkdownLink to handle the outside functions? |
insights-bot | github_2023 | go | 61 | nekomeowww | nekomeowww | @@ -82,11 +86,51 @@ func FullNameFromFirstAndLastName(firstName, lastName string) string {
}
func ExtractTextFromMessage(message *tgbotapi.Message) string {
- if message.Caption != "" {
- return message.Caption
+ text := lo.Ternary(message.Caption != "", message.Caption, message.Text)
+
+ type MarkdownLink struct ... | According to the Go naming convention, Utf should be UTF. |
insights-bot | github_2023 | go | 61 | nekomeowww | nekomeowww | @@ -82,11 +86,51 @@ func FullNameFromFirstAndLastName(firstName, lastName string) string {
}
func ExtractTextFromMessage(message *tgbotapi.Message) string {
- if message.Caption != "" {
- return message.Caption
+ text := lo.Ternary(message.Caption != "", message.Caption, message.Text)
+
+ type MarkdownLink struct ... | What about err != nil? |
insights-bot | github_2023 | others | 61 | nekomeowww | nekomeowww | @@ -4,6 +4,7 @@ go 1.19
require (
entgo.io/ent v0.12.2
+ github.com/Junzki/link-preview v0.0.0-20190616153136-4d94cb3f9ebf | This package had a very ugly implementation and didn't quite follow the regulations and best practices in Go.
However, I think such logic is simple enough to wrap one by ourselves. I will make one for you. |
insights-bot | github_2023 | go | 65 | nekomeowww | nekomeowww | @@ -9,3 +13,64 @@ func NewSlackWebhookMessage(msg string) *slack.WebhookMessage {
ResponseType: slack.ResponseTypeInChannel,
}
}
+
+type HttpClientForSlack interface {
+ Do(req *http.Request) (*http.Response, error)
+}
+
+type SlackCli struct {
+ *slack.Client
+
+ httpclient HttpClientForSlack | httpclient -> httpClient |
insights-bot | github_2023 | go | 65 | nekomeowww | nekomeowww | @@ -9,3 +13,64 @@ func NewSlackWebhookMessage(msg string) *slack.WebhookMessage {
ResponseType: slack.ResponseTypeInChannel,
}
}
+
+type HttpClientForSlack interface {
+ Do(req *http.Request) (*http.Response, error)
+}
+
+type SlackCli struct {
+ *slack.Client
+
+ httpclient HttpClientForSlack
+ clientID st... | ```suggestion
channelID, msgTimestamp, respText, err = cli.SendMessage(channel, options...)
if err == nil || err.Error() != "token_expired" {
return
}
``` |
insights-bot | github_2023 | go | 65 | nekomeowww | nekomeowww | @@ -9,3 +13,64 @@ func NewSlackWebhookMessage(msg string) *slack.WebhookMessage {
ResponseType: slack.ResponseTypeInChannel,
}
}
+
+type HttpClientForSlack interface { | 就叫 HttpClient 就好吧? |
insights-bot | github_2023 | go | 65 | nekomeowww | nekomeowww | @@ -9,3 +13,64 @@ func NewSlackWebhookMessage(msg string) *slack.WebhookMessage {
ResponseType: slack.ResponseTypeInChannel,
}
}
+
+type HttpClientForSlack interface {
+ Do(req *http.Request) (*http.Response, error)
+}
+
+type SlackCli struct { | 直接叫 Client 就好了 |
insights-bot | github_2023 | go | 54 | nekomeowww | OverflowCat | @@ -93,9 +93,9 @@ func (m *Model) SaveOneTelegramChatHistory(message *tgbotapi.Message) error {
return nil
}
if message.ForwardFrom != nil {
- telegramChatHistoryCreate.SetText("转发了来自" + tgbot.FullNameFromFirstAndLastName(message.ForwardFrom.FirstName, message.ForwardFrom.LastName) + "的消息:" + text)
+ telegramC... | `[forwarded from %s]: %s` |
insights-bot | github_2023 | go | 54 | nekomeowww | OverflowCat | @@ -234,16 +238,20 @@ func (m *Model) SummarizeChatHistories(chatID int64, histories []*ent.ChatHistor
"msgId:%d: %s 发送:%s", | `"msgId:%d: [%s sent]: %s",` |
insights-bot | github_2023 | go | 54 | nekomeowww | OverflowCat | @@ -234,16 +238,20 @@ func (m *Model) SummarizeChatHistories(chatID int64, histories []*ent.ChatHistor
"msgId:%d: %s 发送:%s",
message.MessageID,
formatFullNameAndUsername(message.FullName, message.Username),
- message.Text,
+ formatChatHistoryTextContent(message.Text),
))
} else {
- replied... | ```go
"msgId:%d: [%s replying to %s]: %s",
``` |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | ```suggestion
param.Logger.WithError(err).Fatalf("slack bot token auth test failed")
return
```
`return`? |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | 所以这两个是做什么用的? |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -50,10 +50,24 @@ var (
ErrContentNotSupported = errors.New("content not supported")
)
-func (m *Model) SummarizeInputURL(url string) (string, error) {
+type URLSummarizationOutput struct {
+ URL string
+ Title string
+ Msg string
+}
+
+func (u *URLSummarizationOutput) FormatSummarizationAsHTML() string {
+ ... | 👍 |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | 其实注入的都可以默认 unexported |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | 端口号是写死的吗?这个是 Slack 的要求么? |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | 如果 NewSlackBot 的时候返回了 nil,这里 Run 的时候 `fx` 会报错说 not provided 吗? |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | Listen 的时候如果出现端口冲突,应该告诉 `fx` 不要把整个 app boot 起来了。用 Listen then Serve 模式:
```suggestion
func Run() func(bot *SlackBot) error {
return func(bot *SlackBot) error {
if bot == nil {
return
}
listener, err := net.Listen("tcp", bot.server.Addr)
if err != nil {
return err
}
go func() {
er... |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http" | 如果接受了 Listen and Serve 模式的修改的话记得也改一下 import
```suggestion
"net"
"net/http"
``` |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,105 @@
+package slack
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/models/smr"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/slack-go/slack"
+ "go.uber.or... | 用 10 个单位的 buffer channel 的用意是什么呢?smr model 本质上是协程安全的 |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,67 @@
+package slack
+
+import (
+ "errors"
+ "net/http"
+ "net/url"
+
+ "github.com/gin-gonic/gin"
+ "github.com/samber/lo"
+ "github.com/slack-go/slack"
+)
+
+type recivedCommandInfo struct {
+ Command string `form:"command"`
+ Text string `form:"text"`
+ ResponseUrl string `form:"response_url"`... | ```suggestion
s.Logger.WithFields(logrus.Fields{
``` |
insights-bot | github_2023 | go | 36 | nekomeowww | nekomeowww | @@ -0,0 +1,67 @@
+package slack
+
+import (
+ "errors"
+ "net/http"
+ "net/url"
+
+ "github.com/gin-gonic/gin"
+ "github.com/samber/lo"
+ "github.com/slack-go/slack"
+)
+
+type recivedCommandInfo struct {
+ Command string `form:"command"`
+ Text string `form:"text"`
+ ResponseUrl string `form:"response_url"`... | 如果要加前缀就统一一下吧 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -7,8 +7,9 @@ import (
const (
EnvTelegramBotToken = "TELEGRAM_BOT_TOKEN"
- EnvSlackBotToken = "SLACK_BOT_TOKEN"
- EnvSlackBotPort = "SLACK_BOT_PORT"
+ EnvSlackClientId = "SLACK_CLIENT_ID"
+ EnvSlackClientSecret = "SLACK_CLIENT_SECRET"
+ EnvSlackBotPort = "SLACK_BOT_PORT" | `README.md` 里面也同步更新一下? |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -32,8 +33,9 @@ type SectionPinecone struct {
}
type SectionSlack struct {
- Port string
- BotToken string
+ Port string
+ ClientId string | `ClientId` -> `ClientID` |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -7,8 +7,9 @@ import (
const (
EnvTelegramBotToken = "TELEGRAM_BOT_TOKEN"
- EnvSlackBotToken = "SLACK_BOT_TOKEN"
- EnvSlackBotPort = "SLACK_BOT_PORT"
+ EnvSlackClientId = "SLACK_CLIENT_ID" | `EnvSlackClientId` -> `EnvSlackClientID` |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -0,0 +1,16 @@
+package slack
+
+type recivedCommandInfo struct {
+ Command string `form:"command"`
+ Text string `form:"text"`
+ ResponseUrl string `form:"response_url"`
+ UserId string `form:"user_id"`
+ ChannelId string `form:"channel_id"`
+ TeamId string `form:"team_id"` | 我发现这几个 ID 也没遵守 Go 命名规范。
顺便改一下吧,辛苦了。 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -0,0 +1,16 @@
+package slack
+
+type recivedCommandInfo struct {
+ Command string `form:"command"`
+ Text string `form:"text"`
+ ResponseUrl string `form:"response_url"`
+ UserId string `form:"user_id"`
+ ChannelId string `form:"channel_id"`
+ TeamId string `form:"team_id"`
+}
+
+type smrReque... | 同上 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -52,9 +47,67 @@ func (s *SlackBot) postCommandInfo(ctx *gin.Context) {
return
}
+ // get access token
+ token, err := s.ent.SlackOAuthCredentials.Query().Where(
+ slackoauthcredentials.TeamID(body.TeamId),
+ ).First(context.Background())
+ if err != nil {
+ s.logger.WithField("error", err.Error()).Warn("sla... | 先 check exist 再 create。 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -52,9 +47,67 @@ func (s *SlackBot) postCommandInfo(ctx *gin.Context) {
return
}
+ // get access token
+ token, err := s.ent.SlackOAuthCredentials.Query().Where(
+ slackoauthcredentials.TeamID(body.TeamId),
+ ).First(context.Background())
+ if err != nil {
+ s.logger.WithField("error", err.Error()).Warn("sla... | `Exec(...)` 和 `Save(...)` 的区别是啥?能换成 `Save(...)` 吗? |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -52,9 +47,67 @@ func (s *SlackBot) postCommandInfo(ctx *gin.Context) {
return
}
+ // get access token
+ token, err := s.ent.SlackOAuthCredentials.Query().Where(
+ slackoauthcredentials.TeamID(body.TeamId),
+ ).First(context.Background())
+ if err != nil {
+ s.logger.WithField("error", err.Error()).Warn("sla... | 这里应该有个 `err`?不判断的话显式地 omit assign 一下 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -52,9 +47,67 @@ func (s *SlackBot) postCommandInfo(ctx *gin.Context) {
return
}
+ // get access token
+ token, err := s.ent.SlackOAuthCredentials.Query().Where(
+ slackoauthcredentials.TeamID(body.TeamId),
+ ).First(context.Background())
+ if err != nil {
+ s.logger.WithField("error", err.Error()).Warn("sla... | 数据库相关的都补一下单测。 |
insights-bot | github_2023 | others | 39 | nekomeowww | Garfield550 | @@ -21,8 +22,9 @@ services:
environment:
- TELEGRAM_BOT_TOKEN=<Telegram Bot API Token>
- OPENAI_API_SECRET=<OpenAI API Secret Key>
- - DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable | 这里的 db 字符串被删掉了。 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -38,42 +41,42 @@ type SlackBot struct {
smrModel *smr.Model
- server *http.Server
- slackCli *slack.Client
+ server *http.Server
+
+ ent *datastore.Ent
alreadyClosed bool
closeChan chan struct{}
- processChan chan recivedCommandInfo
+ processChan chan smrRequestInfo
}
func NewSlackBot() func(... | port 没有 : 会报错吧? |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -0,0 +1,82 @@
+package slack
+
+import (
+ "context"
+ "log"
+ "testing"
+
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/sirupsen/logrus"
+ "github.com/stretchr/testify/assert"
+ "githu... | ```suggestion
config := configs.NewTestConfig()()
ent, err := datastore.NewEnt()(datastore.NewEntParams{
Configs: config,
Lifecycle: lifeCycleMock{},
})
if err != nil {
log.Fatal("datastore init failed")
}
``` |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -0,0 +1,33 @@
+package slack
+
+import (
+ "context"
+
+ "github.com/nekomeowww/insights-bot/ent/slackoauthcredentials"
+)
+
+func (b *SlackBot) createNewSlackCredential(teamID, accessToken string) error {
+ affectRows, err := b.ent.SlackOAuthCredentials.Update().
+ Where(slackoauthcredentials.TeamID(teamID)).
+ S... | ```suggestion
affectRows, err := b.ent.SlackOAuthCredentials.Update().
Where(slackoauthcredentials.TeamID(teamID)).
SetAccessToken(accessToken).
Save(context.Background())
if err != nil {
b.logger.WithError(err).Warn("slack: failed to update access token")
return err
}
```
中间没空行是会报错嘛?是的话这个算 fal... |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -7,8 +7,9 @@ import (
const (
EnvTelegramBotToken = "TELEGRAM_BOT_TOKEN" //nolint:gosec
- EnvSlackBotToken = "SLACK_BOT_TOKEN" //nolint:gosec
- EnvSlackBotPort = "SLACK_BOT_PORT"
+ EnvSlackClientID = "SLACK_CLIENT_ID"
+ EnvSlackClientSecret = "SLACK_CLIENT_SECRET"
+ EnvSlackBotPort = "SLACK_BOT_PORT" | 可以叫 `SLACK_BOT_WEBHOOK_PORT` 吗? |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -0,0 +1,82 @@
+package slack
+
+import (
+ "context"
+ "log"
+ "testing"
+
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/sirupsen/logrus"
+ "github.com/stretchr/testify/assert"
+ "githu... | 哦,你知道我们有 `tutils.NewEmptyLifecycle` 可以用吗 |
insights-bot | github_2023 | go | 39 | nekomeowww | nekomeowww | @@ -0,0 +1,82 @@
+package slack
+
+import (
+ "context"
+ "log"
+ "testing"
+
+ "github.com/nekomeowww/insights-bot/internal/configs"
+ "github.com/nekomeowww/insights-bot/internal/datastore"
+ "github.com/nekomeowww/insights-bot/pkg/logger"
+ "github.com/sirupsen/logrus"
+ "github.com/stretchr/testify/assert"
+ "githu... | 我突然意识到,你可以用 `TestMain` 初始化呀! |
insights-bot | github_2023 | others | 38 | nekomeowww | nekomeowww | @@ -31,3 +31,15 @@ services:
dockerfile: ./Dockerfile
profiles:
- local
+
+ db_local:
+ image: postgres:alpine
+ restart: unless-stopped
+ environment:
+ - POSTGRES_PASSWORD=123456
+ - POSTGRES_USER=postgres
+ - POSTGRES_DB=postgres | 这两个好像都是默认值?默认值的话也可以省略? |
insights-bot | github_2023 | others | 38 | nekomeowww | nekomeowww | @@ -21,7 +21,7 @@ services:
environment:
- TELEGRAM_BOT_TOKEN=<Telegram Bot API Token>
- OPENAI_API_SECRET=<OpenAI API Secret Key>
- - DB_CONNECTION_STR=<PostgresSQL database URL>
+ - DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable | `search_path=public` 可以省略呢 |
insights-bot | github_2023 | others | 28 | nekomeowww | nekomeowww | @@ -36,6 +36,12 @@ Remember to replace your token and cookie in `docker-compose.yml`
docker-compose up -d
```
+If you prefer run docker image from local codes, | 👍 |
insights-bot | github_2023 | go | 24 | nekomeowww | nekomeowww | @@ -0,0 +1,23 @@
+package help
+
+import (
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+
+ "github.com/nekomeowww/insights-bot/pkg/handler"
+)
+
+func (h *Handler) HandleHelpCommand(c *handler.Context) {
+ message := tgbotapi.NewMessage(c.Update.Message.Chat.ID, `
+你好,欢迎使用 Insights Bot!
+量子速读 用法:/sm... | 可以直接用 `c.Bot.MustSend(message)` |
insights-bot | github_2023 | go | 24 | nekomeowww | nekomeowww | @@ -0,0 +1,23 @@
+package help
+
+import (
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+
+ "github.com/nekomeowww/insights-bot/pkg/handler"
+)
+
+func (h *Handler) HandleHelpCommand(c *handler.Context) {
+ message := tgbotapi.NewMessage(c.Update.Message.Chat.ID, `
+你好,欢迎使用 Insights Bot!
+量子速读 用法:/sm... | ```suggestion
c.Bot.MustSend(message)
``` |
insights-bot | github_2023 | go | 24 | nekomeowww | nekomeowww | @@ -0,0 +1,23 @@
+package help
+
+import (
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+
+ "github.com/nekomeowww/insights-bot/pkg/handler"
+)
+
+func (h *Handler) HandleHelpCommand(c *handler.Context) {
+ message := tgbotapi.NewMessage(c.Update.Message.Chat.ID, `
+你好,欢迎使用 Insights Bot!
+量子速读 用法:/sm... | MustSend 里面会处理好错误记录的,而且返回的也不是 `error`,是 `tgbotapi.Message`,所以这段可以省略了。
```suggestion
``` |
insights-bot | github_2023 | go | 17 | nekomeowww | nekomeowww | @@ -29,12 +28,25 @@ func NewClient(apiSecret string, apiHost string) *Client {
// truncateContentBasedOnTokens 基于 token 计算的方式截断文本
func (c *Client) TruncateContentBasedOnTokens(textContent string, limits int) (string, error) {
- tokens, err := tokenizer.CalToken(textContent)
+ r, err := tokenizer.Encode(textContent)... | How about we early return `textContent, nil` when `len(r.Bpe) <= limits`? And then doing the truncate. |
insights-bot | github_2023 | go | 17 | nekomeowww | nekomeowww | @@ -29,12 +28,25 @@ func NewClient(apiSecret string, apiHost string) *Client {
// truncateContentBasedOnTokens 基于 token 计算的方式截断文本
func (c *Client) TruncateContentBasedOnTokens(textContent string, limits int) (string, error) {
- tokens, err := tokenizer.CalToken(textContent)
+ r, err := tokenizer.Encode(textContent)... | Could we have more comments by this line to say: `把解码后错位的汉字元素抛弃` (discard the wrong Chinese character elements after decoding).
For better understanding, you could also try to rewrite this line as: `string(lo.DropRight(truncated, 1))` |
insights-bot | github_2023 | go | 17 | nekomeowww | nekomeowww | @@ -97,7 +97,7 @@ func (s *ChatHistoryRecapService) SendChatHistoriesRecap() {
continue
}
- s.Logger.Info("sending chat histories recap for chat %d", chatID)
+ s.Logger.Infof("sending chat histories recap for chat %d", chatID) | Thanks! |
insights-bot | github_2023 | go | 17 | nekomeowww | nekomeowww | @@ -3,18 +3,23 @@ package openai
import (
"context"
"fmt"
- "math"
"net/url"
+ "unicode/utf8"
- "github.com/pandodao/tokenizer-go"
+ "github.com/pkoukk/tiktoken-go"
"github.com/sashabaranov/go-openai"
)
type Client struct {
- OpenAIClient *openai.Client
+ tiktokenEncoding *tiktoken.Tiktoken
+ OpenAIClien... | Is `*tiktoken.Tiktoken` concurrent-safe? |
insights-bot | github_2023 | go | 17 | nekomeowww | nekomeowww | @@ -15,9 +15,12 @@ var h *Handler
func TestMain(m *testing.M) {
logger := logger.NewLogger(logrus.DebugLevel, "insights-bot", "", nil)
config := configs.NewConfig()()
- openaiClient := thirdparty.NewOpenAIClient()(thirdparty.NewOpenAIClientParam{
+ openaiClient, err := thirdparty.NewOpenAIClient()(thirdparty.NewOp... | ```suggestion
panic(err)
``` |
insights-bot | github_2023 | go | 17 | nekomeowww | nekomeowww | @@ -23,47 +28,47 @@ func NewClient(apiSecret string, apiHost string) *Client {
client := openai.NewClientWithConfig(config)
return &Client{
- OpenAIClient: client,
- }
+ OpenAIClient: client,
+ tiktokenEncoding: tokenizer,
+ }, nil
}
// truncateContentBasedOnTokens 基于 token 计算的方式截断文本
-func (c *Client) ... | Nice comments and explanations btw |
insights-bot | github_2023 | go | 2 | nekomeowww | nekomeowww | @@ -0,0 +1,44 @@
+package utils
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestReplaceMarkdownTitlesToBoldTexts(t *testing.T) {
+ prefix := ""
+ for i := 0; i < 6; i++ {
+ t.Run(fmt.Sprintf("title level %d", i+1), func(t *testing.T) {
+ a := assert.New(t)
+
+ prefix += "#"
... | 使用 `a.NoError(err)` 来代替 `a.Empty(err)` |
insights-bot | github_2023 | go | 2 | nekomeowww | nekomeowww | @@ -0,0 +1,44 @@
+package utils
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestReplaceMarkdownTitlesToBoldTexts(t *testing.T) {
+ prefix := ""
+ for i := 0; i < 6; i++ {
+ t.Run(fmt.Sprintf("title level %d", i+1), func(t *testing.T) {
+ a := assert.New(t)
+
+ prefix += "#"
... | 使用 `a.NoError(err)` 来代替 `a.Empty(err)` |
insights-bot | github_2023 | go | 2 | nekomeowww | nekomeowww | @@ -0,0 +1,44 @@
+package utils
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestReplaceMarkdownTitlesToBoldTexts(t *testing.T) {
+ prefix := ""
+ for i := 0; i < 6; i++ {
+ t.Run(fmt.Sprintf("title level %d", i+1), func(t *testing.T) { | 有空格的 sub test 的标题会出问题吗? |
insights-bot | github_2023 | go | 2 | nekomeowww | nekomeowww | @@ -0,0 +1,44 @@
+package utils
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestReplaceMarkdownTitlesToBoldTexts(t *testing.T) {
+ prefix := ""
+ for i := 0; i < 6; i++ {
+ t.Run(fmt.Sprintf("title level %d", i+1), func(t *testing.T) {
+ a := assert.New(t)
+
+ prefix += "#"
... | 使用 `a.NoError(err)` 来代替 `a.Empty(err)` |
insights-bot | github_2023 | go | 2 | nekomeowww | nekomeowww | @@ -53,3 +57,27 @@ func ContainsCJKChar(s string) bool {
return false
}
+
+// ReplaceMarkdownTitlesToBoldTexts
+func ReplaceMarkdownTitlesToBoldTexts(text string) (string, error) {
+ exp, err := regexp.Compile(`(?m)^(#){1,6} (.)*(\n)?`) | 把正则提升为包级 private 变量,然后使用 `regexp.MustCompile(...)` 来创建正则,这样可以在包第一次导入之后就能了解到 regexp 是否正确,也能让 regexp 能被重复利用(相当于缓存)。
关联阅读:[How to cache the compiled regex in Go - Stack Overflow](https://stackoverflow.com/a/72185182) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.