Skip to content

Commit

Permalink
Refactoring responses and dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-one committed Apr 3, 2023
1 parent d175de6 commit 09da21b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 30 deletions.
9 changes: 5 additions & 4 deletions gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pavel-one/EdgeGPT-Go/internal/CookieManager"
"github.com/pavel-one/EdgeGPT-Go/internal/Helpers"
"github.com/pavel-one/EdgeGPT-Go/internal/Logger"
"github.com/pavel-one/EdgeGPT-Go/responses"
"io"
"net/http"
"time"
Expand All @@ -20,8 +21,8 @@ const (
StyleCreative = "h3relaxedimg"
StyleBalanced = "galileo"
StylePrecise = "h3precise"
Delimiter = "\x1e"
DelimiterByte = uint8(30)
Delimiter = "\x1e"
)

type GPT struct {
Expand Down Expand Up @@ -130,7 +131,7 @@ Example:
log.Println(mw.Answer.GetAnswer())
}
*/
func (g *GPT) AskAsync(message string) (*MessageWrapper, error) {
func (g *GPT) AskAsync(message string) (*responses.MessageWrapper, error) {

if len(message) > 2000 {
return nil, fmt.Errorf("message very long, max: %d", 2000)
Expand All @@ -141,7 +142,7 @@ func (g *GPT) AskAsync(message string) (*MessageWrapper, error) {
}

// AskSync getting answer sync
func (g *GPT) AskSync(message string) (*MessageWrapper, error) {
func (g *GPT) AskSync(message string) (*responses.MessageWrapper, error) {
if len(message) > 2000 {
return nil, fmt.Errorf("message very long, max: %d", 2000)
}
Expand All @@ -153,7 +154,7 @@ func (g *GPT) AskSync(message string) (*MessageWrapper, error) {

go m.Worker()

for _ = range m.Chan {
for range m.Chan {
if m.Final {
break
}
Expand Down
5 changes: 3 additions & 2 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/gorilla/websocket"
"github.com/pavel-one/EdgeGPT-Go/internal/Helpers"
"github.com/pavel-one/EdgeGPT-Go/responses"
"sync"
)

Expand Down Expand Up @@ -32,7 +33,7 @@ func (c *Hub) initialHandshake() error {
}

// send new message to websocket
func (c *Hub) send(message string) (*MessageWrapper, error) {
func (c *Hub) send(message string) (*responses.MessageWrapper, error) {
c.mu.Lock()

m, err := json.Marshal(c.getRequest(message))
Expand All @@ -46,7 +47,7 @@ func (c *Hub) send(message string) (*MessageWrapper, error) {
return nil, err
}

return NewMessageWrapper(message, &c.mu, c.conn), nil
return responses.NewMessageWrapper(message, &c.mu, c.conn), nil
}

// Close hub and connection
Expand Down
16 changes: 0 additions & 16 deletions interface.go

This file was deleted.

6 changes: 4 additions & 2 deletions responseFinal.go → responses/final.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package EdgeGPT
package responses

import "time"
import (
"time"
)

// FinalResponse response for final generate message
type FinalResponse struct {
Expand Down
9 changes: 9 additions & 0 deletions responses/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package responses

type GptResponse interface {
GetAnswer() string
GetType() int
GetMaxUnit() int
GetUserUnit() int
GetSuggestions() []*Suggestion
}
10 changes: 6 additions & 4 deletions message.go → responses/message.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package EdgeGPT
package responses

import (
"encoding/json"
Expand All @@ -7,6 +7,8 @@ import (
"time"
)

const DelimiterByte = uint8(30)

const (
TypeUpdate float64 = 1
TypeFinish float64 = 2
Expand Down Expand Up @@ -72,7 +74,7 @@ func (m *MessageWrapper) Worker() error {

var response map[string]any
var updateResponse UpdateResponse
var finishResponse FinalResponse
var finalResponse FinalResponse
var undefinedResponse UndefinedResponse

for {
Expand Down Expand Up @@ -104,11 +106,11 @@ func (m *MessageWrapper) Worker() error {
m.Answer = &updateResponse
break
case TypeFinish:
if err := json.Unmarshal(message, &finishResponse); err != nil {
if err := json.Unmarshal(message, &finalResponse); err != nil {
return err
}

m.Answer = &finishResponse
m.Answer = &finalResponse
m.Final = true
m.Chan <- message
close(m.Chan)
Expand Down
2 changes: 1 addition & 1 deletion responseUndefined.go → responses/undefined.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package EdgeGPT
package responses

// UndefinedResponse response for unused messages
type UndefinedResponse struct {
Expand Down
2 changes: 1 addition & 1 deletion responseUpdate.go → responses/update.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package EdgeGPT
package responses

// UpdateResponse response for work generate message
type UpdateResponse struct {
Expand Down

0 comments on commit 09da21b

Please sign in to comment.