-
Notifications
You must be signed in to change notification settings - Fork 68
fix: Add conversational context to interactive CLI chat #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nithish-95
wants to merge
4
commits into
docker:main
Choose a base branch
from
nithish-95:issueChatHistory#351
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
011d5e3
feat: introduce conversation history management for chat commands
nithish-95 a642cbb
refactor: unify chat message type and improve conversation history ma…
nithish-95 e13f02c
refactor: Update chat message types to `desktop.OpenAIChatMessage`
nithish-95 4448de4
feat: Enable Intel Mac support, default server updates to false, and …
nithish-95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,121 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func TestChatWithNIM_Context(t *testing.T) { | ||
| // Save original port and restore after test | ||
| originalPort := nimDefaultPort | ||
| defer func() { nimDefaultPort = originalPort }() | ||
|
|
||
| // Track received messages | ||
| var receivedPayloads []struct { | ||
| Messages []Message `json:"messages"` | ||
| } | ||
|
|
||
| // Setup Mock Server | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path != "/v1/chat/completions" { | ||
| t.Errorf("Expected path /v1/chat/completions, got %s", r.URL.Path) | ||
| http.Error(w, "Not found", http.StatusNotFound) | ||
| return | ||
| } | ||
|
|
||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read request body: %v", err) | ||
| } | ||
|
|
||
| var payload struct { | ||
| Messages []Message `json:"messages"` | ||
| } | ||
| if err := json.Unmarshal(body, &payload); err != nil { | ||
| t.Fatalf("Failed to unmarshal request body: %v", err) | ||
| } | ||
|
|
||
| receivedPayloads = append(receivedPayloads, payload) | ||
|
|
||
| // Mock response (SSE format) | ||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| w.Write([]byte(`data: {"choices":[{"delta":{"content":"Response"}}]} | ||
| `)) | ||
| w.Write([]byte(`data: [DONE] | ||
| `)) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| // Parse server URL to get the port | ||
| u, err := url.Parse(server.URL) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse server URL: %v", err) | ||
| } | ||
| port, err := strconv.Atoi(u.Port()) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse port: %v", err) | ||
| } | ||
| nimDefaultPort = port | ||
|
|
||
| // Initialize messages slice | ||
| var messages []Message | ||
| cmd := &cobra.Command{} | ||
|
|
||
| // First interaction | ||
| err = chatWithNIM(cmd, "ai/model", &messages, "Hello") | ||
| if err != nil { | ||
| t.Fatalf("First chatWithNIM failed: %v", err) | ||
| } | ||
|
|
||
| // Verify first request | ||
| if len(receivedPayloads) != 1 { | ||
| t.Fatalf("Expected 1 request, got %d", len(receivedPayloads)) | ||
| } | ||
| if len(receivedPayloads[0].Messages) != 1 { | ||
| t.Errorf("Expected 1 message in first request, got %d", len(receivedPayloads[0].Messages)) | ||
| } | ||
| if receivedPayloads[0].Messages[0].Content != "Hello" { | ||
| t.Errorf("Expected content 'Hello', got '%s'", receivedPayloads[0].Messages[0].Content) | ||
| } | ||
|
|
||
| // Second interaction | ||
| err = chatWithNIM(cmd, "ai/model", &messages, "How are you?") | ||
| if err != nil { | ||
| t.Fatalf("Second chatWithNIM failed: %v", err) | ||
| } | ||
|
|
||
| // Verify second request | ||
| if len(receivedPayloads) != 2 { | ||
| t.Fatalf("Expected 2 requests, got %d", len(receivedPayloads)) | ||
| } | ||
|
|
||
| // This is where we expect it to fail if the issue exists | ||
| // We expect: | ||
| // 1. User: Hello | ||
| // 2. Assistant: Response | ||
| // 3. User: How are you? | ||
| if len(receivedPayloads[1].Messages) != 3 { | ||
| t.Errorf("Expected 3 messages in second request, got %d", len(receivedPayloads[1].Messages)) | ||
| for i, m := range receivedPayloads[1].Messages { | ||
| t.Logf("Message %d: Role=%s, Content=%s", i, m.Role, m.Content) | ||
| } | ||
| } else { | ||
| // Verify message content | ||
| if receivedPayloads[1].Messages[0].Content != "Hello" { | ||
| t.Errorf("Msg 0: Expected 'Hello', got '%s'", receivedPayloads[1].Messages[0].Content) | ||
| } | ||
| if receivedPayloads[1].Messages[1].Role != "assistant" { | ||
| t.Errorf("Msg 1: Expected role 'assistant', got '%s'", receivedPayloads[1].Messages[1].Role) | ||
| } | ||
| if receivedPayloads[1].Messages[2].Content != "How are you?" { | ||
| t.Errorf("Msg 2: Expected 'How are you?', got '%s'", receivedPayloads[1].Messages[2].Content) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.