-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchannel_context.go
More file actions
36 lines (31 loc) · 897 Bytes
/
channel_context.go
File metadata and controls
36 lines (31 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis/v8"
)
type ChannelContext struct {
redisClient *redis.Client
}
func NewChannelContext(redisClient *redis.Client) *ChannelContext {
return &ChannelContext{
redisClient: redisClient,
}
}
// Keeps track of the last 100 messages in a channel or the last 15 minutes, whichever is shorter
// This is used to help provide context to the handlers if they need it
// It's particularly useful for ChatGPT because it's context sensitive
func (c *ChannelContext) AddMessage(channelID, message string) error {
ctx := context.Background()
key := fmt.Sprintf("channel_context:%s", channelID)
err := c.redisClient.LPush(ctx, key, message).Err()
if err != nil {
return err
}
err = c.redisClient.LTrim(ctx, key, 0, 99).Err()
if err != nil {
return err
}
return c.redisClient.Expire(ctx, key, 15*time.Minute).Err()
}