1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package ticket
- import (
- _ "embed"
- "encoding/json"
- "fmt"
- "github.com/google/uuid"
- "github.com/sashabaranov/go-openai"
- "google.golang.org/protobuf/encoding/protojson"
- "time"
- )
- func (t *Ticket) Marshal() ([]byte, error) {
- return protojson.Marshal(t)
- }
- func (t *Ticket) AddMessage(m *Message) error {
- if m == nil {
- return fmt.Errorf("illegal argument: Message must not be a nil")
- }
- if m.TicketId == "" {
- m.TicketId = t.Id
- }
- if m.Id == "" {
- m.Id = uuid.NewString()
- }
- if m.Date == 0 {
- m.Date = time.Now().UnixMilli()
- }
- t.Thread = append(t.Thread, m)
- return nil
- }
- func (t *Ticket) AddGPTResponseMessage(src *openai.ChatCompletionChoice) (*Message, error) {
- var (
- message = &Message{
- Id: uuid.NewString(),
- TicketId: t.Id,
- Provider: MessageProvider_GptMessageProvider,
- Body: src.Message.Content,
- Date: time.Now().UnixMilli(),
- }
- buf []byte
- err error
- )
- if buf, err = json.Marshal(src.Message); err != nil {
- return nil, err
- }
- message.Raw = string(buf)
- if err = t.AddMessage(message); err != nil {
- return nil, err
- }
- return message, nil
- }
- //goland:noinspection GoUnusedExportedFunction
- func Unmarshal(d []byte) (*Ticket, error) {
- t := &Ticket{}
- if err := protojson.Unmarshal(d, t); err != nil {
- return nil, err
- }
- return t, nil
- }
|