-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
461 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package history | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kardolus/chatgpt-cli/types" | ||
"strings" | ||
) | ||
|
||
const ( | ||
assistantRole = "assistant" | ||
systemRole = "system" | ||
userRole = "user" | ||
) | ||
|
||
type History struct { | ||
store HistoryStore | ||
} | ||
|
||
func NewHistory(store HistoryStore) *History { | ||
return &History{store: store} | ||
} | ||
|
||
func (h *History) Print(thread string) (string, error) { | ||
var result string | ||
|
||
messages, err := h.store.ReadThread(thread) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var ( | ||
lastRole string | ||
concatenatedMessage string | ||
) | ||
|
||
for _, message := range messages { | ||
if message.Role == userRole && lastRole == userRole { | ||
concatenatedMessage += message.Content | ||
} else { | ||
if lastRole == userRole && concatenatedMessage != "" { | ||
result += formatMessage(types.Message{Role: userRole, Content: concatenatedMessage}) | ||
concatenatedMessage = "" | ||
} | ||
|
||
if message.Role == userRole { | ||
concatenatedMessage = message.Content | ||
} else { | ||
result += formatMessage(message) | ||
} | ||
} | ||
|
||
lastRole = message.Role | ||
} | ||
|
||
// Handle the case where the last message is a user message and was concatenated | ||
if lastRole == userRole && concatenatedMessage != "" { | ||
result += formatMessage(types.Message{Role: userRole, Content: concatenatedMessage}) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func formatMessage(msg types.Message) string { | ||
var ( | ||
emoji string | ||
prefix string | ||
) | ||
|
||
switch msg.Role { | ||
case systemRole: | ||
emoji = "💻" | ||
prefix = "\n" | ||
case userRole: | ||
emoji = "👤" | ||
prefix = "---\n" | ||
case assistantRole: | ||
emoji = "🤖" | ||
prefix = "\n" | ||
} | ||
|
||
return fmt.Sprintf("%s**%s** %s:\n%s\n", prefix, strings.ToUpper(msg.Role), emoji, msg.Content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package history_test | ||
|
||
import ( | ||
"errors" | ||
"github.com/golang/mock/gomock" | ||
"github.com/kardolus/chatgpt-cli/history" | ||
"github.com/kardolus/chatgpt-cli/types" | ||
. "github.com/onsi/gomega" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
"testing" | ||
) | ||
|
||
//go:generate mockgen -destination=historymocks_test.go -package=history_test github.com/kardolus/chatgpt-cli/history HistoryStore | ||
|
||
var ( | ||
mockCtrl *gomock.Controller | ||
mockHistoryStore *MockHistoryStore | ||
subject *history.History | ||
) | ||
|
||
func TestUnitHistory(t *testing.T) { | ||
spec.Run(t, "Testing the History", testHistory, spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testHistory(t *testing.T, when spec.G, it spec.S) { | ||
it.Before(func() { | ||
RegisterTestingT(t) | ||
mockCtrl = gomock.NewController(t) | ||
mockHistoryStore = NewMockHistoryStore(mockCtrl) | ||
subject = history.NewHistory(mockHistoryStore) | ||
}) | ||
|
||
it.After(func() { | ||
mockCtrl.Finish() | ||
}) | ||
|
||
when("Print()", func() { | ||
const threadName = "threadName" | ||
|
||
it("throws an error when there is a problem talking to the store", func() { | ||
mockHistoryStore.EXPECT().ReadThread(threadName).Return(nil, errors.New("nope")).Times(1) | ||
|
||
_, err := subject.Print(threadName) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
it("concatenates multiple user messages", func() { | ||
messages := []types.Message{ | ||
{Role: "user", Content: "first message"}, | ||
{Role: "user", Content: " second message"}, | ||
{Role: "assistant", Content: "response"}, | ||
} | ||
|
||
mockHistoryStore.EXPECT().ReadThread(threadName).Return(messages, nil).Times(1) | ||
|
||
result, err := subject.Print(threadName) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(result).To(ContainSubstring("**USER** 👤:\nfirst message second message\n")) | ||
Expect(result).To(ContainSubstring("**ASSISTANT** 🤖:\nresponse\n")) | ||
}) | ||
|
||
it("prints all roles correctly", func() { | ||
messages := []types.Message{ | ||
{Role: "system", Content: "system message"}, | ||
{Role: "user", Content: "user message"}, | ||
{Role: "assistant", Content: "assistant message"}, | ||
} | ||
|
||
mockHistoryStore.EXPECT().ReadThread(threadName).Return(messages, nil).Times(1) | ||
|
||
result, err := subject.Print(threadName) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(result).To(ContainSubstring("**SYSTEM** 💻:\nsystem message\n")) | ||
Expect(result).To(ContainSubstring("\n---\n**USER** 👤:\nuser message\n")) | ||
Expect(result).To(ContainSubstring("**ASSISTANT** 🤖:\nassistant message\n")) | ||
}) | ||
|
||
it("handles the final user message concatenation", func() { | ||
messages := []types.Message{ | ||
{Role: "user", Content: "first message"}, | ||
{Role: "user", Content: " second message"}, | ||
} | ||
|
||
mockHistoryStore.EXPECT().ReadThread(threadName).Return(messages, nil).Times(1) | ||
|
||
result, err := subject.Print(threadName) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(result).To(ContainSubstring("**USER** 👤:\nfirst message second message\n")) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.