-
Notifications
You must be signed in to change notification settings - Fork 3
feat(ai): List saved assistant sessions so past chats can be resumed #181
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
Merged
Merged
Changes from all commits
Commits
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
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,76 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestSessionTitle(t *testing.T) { | ||
| // First visible user turn becomes the title. | ||
| s := NewSession(SessionScopeSystem, "", true, SessionActor{ID: "u1"}, "sys") | ||
| s.AddUserMessage("api.example.com:443 request with logs...", "why is wordpress unhealthy?", true) // hidden, skipped | ||
| s.AddUserMessage("summarize recent security events", "", false) | ||
| if got := s.Title(); got != "summarize recent security events" { | ||
| t.Errorf("title should come from the first visible user turn, got %q", got) | ||
| } | ||
|
|
||
| // No user turn yet: fall back to the deployment name. | ||
| empty := NewSession(SessionScopeDeployment, "shop", true, SessionActor{ID: "u1"}, "sys") | ||
| if got := empty.Title(); got != "shop chat" { | ||
| t.Errorf("deployment fallback title, got %q", got) | ||
| } | ||
|
|
||
| // System scope with no user turn. | ||
| sys := NewSession(SessionScopeSystem, "", true, SessionActor{ID: "u1"}, "sys") | ||
| if got := sys.Title(); got != "System chat" { | ||
| t.Errorf("system fallback title, got %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestSessionStoreList(t *testing.T) { | ||
| st := NewSessionStore(t.TempDir()) | ||
|
|
||
| older := NewSession(SessionScopeSystem, "", true, SessionActor{ID: "u1", Name: "alice"}, "sys") | ||
| older.AddUserMessage("first question", "", false) | ||
| if err := st.Save(older); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| newer := NewSession(SessionScopeDeployment, "shop", true, SessionActor{ID: "u2", Name: "bob"}, "sys") | ||
| newer.UpdatedAt = time.Now().UTC().Add(time.Minute) | ||
| if err := st.Save(newer); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| list, err := st.List() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(list) != 2 { | ||
| t.Fatalf("expected 2 summaries, got %d", len(list)) | ||
| } | ||
| if list[0].ID != newer.ID { | ||
| t.Errorf("expected the most recently updated session first") | ||
| } | ||
| if list[0].Title != "shop chat" { | ||
| t.Errorf("unexpected title %q", list[0].Title) | ||
| } | ||
| if list[1].Title != "first question" { | ||
| t.Errorf("unexpected title %q", list[1].Title) | ||
| } | ||
| // The owner must ride along so the handler can filter by actor. | ||
| if list[1].CreatedBy.ID != "u1" { | ||
| t.Errorf("summary should carry the creating actor, got %q", list[1].CreatedBy.ID) | ||
| } | ||
| } | ||
|
|
||
| func TestSessionStoreListEmpty(t *testing.T) { | ||
| st := NewSessionStore(t.TempDir()) | ||
| list, err := st.List() | ||
| if err != nil { | ||
| t.Fatalf("empty store should not error: %v", err) | ||
| } | ||
| if len(list) != 0 { | ||
| t.Errorf("expected no sessions, got %d", len(list)) | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While functional, this implementation has a few performance and efficiency considerations:
st.Getunmarshals the full session JSON, including potentially large transcripts (hundreds of messages). For a listing view, consider decoding into a 'metadata-only' struct that ignores themessagesarray to save memory and CPU.summariesslice capacity to avoid repeated re-allocations during the loop.slices.SortFuncis preferred oversort.Slicefor type safety and performance.