Skip to content

Commit

Permalink
refactor: Rename message addition methods to use more descriptive con…
Browse files Browse the repository at this point in the history
…tent-based names
  • Loading branch information
presbrey committed Jan 30, 2025
1 parent 5e854e2 commit 8a0cfef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ err = chat.Load("chat-123")
err = chat.Delete("chat-123")
```

### Direct Access and Helper Methods

The `Chat` and `Message` structs are designed to be transparent - you are welcome to access their members directly in your applications. For example, you can directly access `chat.Messages`, `chat.Meta`, or `message.Role`.

For convenience, the package also provides several helper methods:

- `AddRoleContent(role, content)`: Add a message with any role and content
- `AddUserContent(content)`: Add a user message
- `AddAssistantContent(content)`: Add an assistant message
- `AddToolRawContent(name, toolCallID, content)`: Add a tool message with raw content
- `AddToolContent(name, toolCallID, content)`: Add a tool message with JSON-encoded content if needed
- `AddAssistantToolCall(toolCalls)`: Add an assistant message with tool calls
- `LastMessage()`: Get the most recent message
- `LastMessageRole()`: Get the role of the most recent message
- `Range(fn)`: Iterate through messages with a callback function

```go
// Example of direct member access
fmt.Println(chat.ID, chat.LastUpdated)
for _, msg := range chat.Messages {
fmt.Println(msg.Role, msg.Content)
}

// Example of helper method usage
chat.AddUserContent("Hello")
if last := chat.LastMessage(); last != nil {
fmt.Println("Last message was from:", last.Role)
}
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.
Expand Down
12 changes: 6 additions & 6 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestChat(t *testing.T) {
session := &aichat.Chat{ID: "test-id", Options: aichat.Options{S3: s3}}

// Test adding user message
session.AddUserMessage("What is the weather like in Boston?")
session.AddUserContent("What is the weather like in Boston?")

// Test adding assistant tool call
toolCalls := []aichat.ToolCall{
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestChatWithAssistantMessage(t *testing.T) {
session := &aichat.Chat{ID: "test-id", Options: aichat.Options{S3: s3}}

content := "The weather in Boston is sunny and 22°C."
session.AddAssistantMessage(content)
session.AddAssistantContent(content)

if len(session.Messages) != 1 {
t.Errorf("Expected 1 message, got %d", len(session.Messages))
Expand All @@ -102,7 +102,7 @@ func TestLastMessage(t *testing.T) {
}

// Add a message and test
chat.AddUserMessage("Hello")
chat.AddUserContent("Hello")
msg := chat.LastMessage()
if msg == nil {
t.Fatal("Expected non-nil message after adding user message")
Expand All @@ -112,7 +112,7 @@ func TestLastMessage(t *testing.T) {
}

// Add another message and test
chat.AddAssistantMessage("Hi there")
chat.AddAssistantContent("Hi there")
msg = chat.LastMessage()
if msg == nil {
t.Fatal("Expected non-nil message after adding assistant message")
Expand All @@ -132,13 +132,13 @@ func TestLastMessageRole(t *testing.T) {
}

// Test user message
chat.AddUserMessage("Hello")
chat.AddUserContent("Hello")
if role := chat.LastMessageRole(); role != "user" {
t.Errorf("Expected role 'user', got %q", role)
}

// Test assistant message
chat.AddAssistantMessage("Hi there")
chat.AddAssistantContent("Hi there")
if role := chat.LastMessageRole(); role != "assistant" {
t.Errorf("Expected role 'assistant', got %q", role)
}
Expand Down
4 changes: 2 additions & 2 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestChatStorage(t *testing.T) {
session := &aichat.Chat{ID: "test-id", Options: aichat.Options{S3: s3}}

// Add some test data
session.AddUserMessage("Test message")
session.AddUserContent("Test message")
session.Meta = make(map[string]any)
session.Meta["test"] = "value"

Expand Down Expand Up @@ -191,7 +191,7 @@ func TestStorageLoad(t *testing.T) {
ID: "test-id",
Options: opts,
}
originalChat.AddUserMessage("Test message")
originalChat.AddUserContent("Hello")
if err := originalChat.Save(ctx, "test-key"); err != nil {
t.Fatalf("Failed to save chat: %v", err)
}
Expand Down

0 comments on commit 8a0cfef

Please sign in to comment.