-
Notifications
You must be signed in to change notification settings - Fork 164
feat (AI): UI for sharing AI conversations #8664
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
ericpgreen2
wants to merge
6
commits into
main
Choose a base branch
from
ui-for-sharing-ai-conversations
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
736338a
feat (AI): UI for sharing AI conversations
ericpgreen2 10f9e10
Fix "untitled conversation" in popover
ericpgreen2 14a53b9
Disable "Share" button when N/A, don't hide it
ericpgreen2 7ab2e7b
Make all icon buttons consistent w/ tooltips
ericpgreen2 859c528
Self-review
ericpgreen2 1b150d9
Add unit tests
ericpgreen2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| import { queryClient } from "@rilldata/web-common/lib/svelte-query/globalQueryClient"; | ||
| import { | ||
| getRuntimeServiceGetConversationQueryKey, | ||
| type V1GetConversationResponse, | ||
| type V1Message, | ||
| } from "@rilldata/web-common/runtime-client"; | ||
| import { get } from "svelte/store"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { Conversation } from "./conversation"; | ||
| import { NEW_CONVERSATION_ID } from "./utils"; | ||
|
|
||
| // ============================================================================= | ||
| // MOCKS | ||
| // ============================================================================= | ||
|
|
||
| vi.mock("@rilldata/web-common/runtime-client", async (importOriginal) => { | ||
| const original = | ||
| await importOriginal< | ||
| typeof import("@rilldata/web-common/runtime-client") | ||
| >(); | ||
| return { | ||
| ...original, | ||
| runtimeServiceForkConversation: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock("@rilldata/web-common/runtime-client/runtime-store", () => ({ | ||
| runtime: { | ||
| subscribe: (fn: (value: { host: string }) => void) => { | ||
| fn({ host: "http://localhost:9009" }); | ||
| return () => {}; | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| import { runtimeServiceForkConversation } from "@rilldata/web-common/runtime-client"; | ||
|
|
||
| // ============================================================================= | ||
| // TEST CONSTANTS | ||
| // ============================================================================= | ||
|
|
||
| const INSTANCE_ID = "test-instance"; | ||
| const ORIGINAL_CONVERSATION_ID = "original-conv-123"; | ||
| const FORKED_CONVERSATION_ID = "forked-conv-456"; | ||
|
|
||
| // ============================================================================= | ||
| // HELPERS | ||
| // ============================================================================= | ||
|
|
||
| function getCacheKey(conversationId: string) { | ||
| return getRuntimeServiceGetConversationQueryKey(INSTANCE_ID, conversationId); | ||
| } | ||
|
|
||
| function getCachedData(conversationId: string) { | ||
| return queryClient.getQueryData<V1GetConversationResponse>( | ||
| getCacheKey(conversationId), | ||
| ); | ||
| } | ||
|
|
||
| function seedCache( | ||
| conversationId: string, | ||
| options: { | ||
| isOwner: boolean; | ||
| messages?: Partial<V1Message>[]; | ||
| title?: string; | ||
| createdOn?: string; | ||
| }, | ||
| ) { | ||
| queryClient.setQueryData<V1GetConversationResponse>( | ||
| getCacheKey(conversationId), | ||
| { | ||
| conversation: { | ||
| id: conversationId, | ||
| title: options.title, | ||
| createdOn: options.createdOn, | ||
| }, | ||
| messages: options.messages as V1Message[], | ||
| isOwner: options.isOwner, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| function mockForkSuccess(forkedId: string = FORKED_CONVERSATION_ID) { | ||
| vi.mocked(runtimeServiceForkConversation).mockResolvedValue({ | ||
| conversationId: forkedId, | ||
| }); | ||
| } | ||
|
|
||
| function mockForkFailure(error: Error = new Error("Fork failed")) { | ||
| vi.mocked(runtimeServiceForkConversation).mockRejectedValue(error); | ||
| } | ||
|
|
||
| function mockForkEmptyResponse() { | ||
| vi.mocked(runtimeServiceForkConversation).mockResolvedValue({}); | ||
| } | ||
|
|
||
| function createConversation(conversationId: string = ORIGINAL_CONVERSATION_ID) { | ||
| return new Conversation(INSTANCE_ID, conversationId); | ||
| } | ||
|
|
||
| async function sendMessageAndIgnoreStreamError( | ||
| conversation: Conversation, | ||
| message: string, | ||
| ) { | ||
| conversation.draftMessage.set(message); | ||
| await conversation.sendMessage({}).catch(() => {}); | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // TESTS | ||
| // ============================================================================= | ||
|
|
||
| describe("Conversation", () => { | ||
| beforeEach(() => { | ||
| queryClient.clear(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| queryClient.clear(); | ||
| }); | ||
|
|
||
| describe("forkConversation", () => { | ||
| it("forks and copies messages when non-owner sends a message", async () => { | ||
| // Arrange | ||
| seedCache(ORIGINAL_CONVERSATION_ID, { | ||
| isOwner: false, | ||
| title: "Shared conversation", | ||
| createdOn: "2024-01-01T00:00:00Z", | ||
| messages: [ | ||
| { id: "msg-1", role: "user", contentData: "Hello" }, | ||
| { id: "msg-2", role: "assistant", contentData: "Hi there!" }, | ||
| ], | ||
| }); | ||
| mockForkSuccess(); | ||
|
|
||
| const conversation = createConversation(); | ||
| let forkedId: string | null = null; | ||
| conversation.on("conversation-forked", (id) => (forkedId = id)); | ||
|
|
||
| // Act | ||
| conversation.draftMessage.set("My follow-up question"); | ||
| const sendPromise = conversation.sendMessage({}); | ||
| await vi.waitFor(() => expect(forkedId).toBe(FORKED_CONVERSATION_ID)); | ||
|
|
||
| // Assert: fork API called correctly | ||
| expect(runtimeServiceForkConversation).toHaveBeenCalledWith( | ||
| INSTANCE_ID, | ||
| ORIGINAL_CONVERSATION_ID, | ||
| {}, | ||
| ); | ||
|
|
||
| // Assert: cache updated with forked conversation | ||
| const forkedData = getCachedData(FORKED_CONVERSATION_ID); | ||
| expect(forkedData?.conversation?.id).toBe(FORKED_CONVERSATION_ID); | ||
| expect(forkedData?.conversation?.title).toBe("Shared conversation"); | ||
| expect(forkedData?.conversation?.createdOn).toBe("2024-01-01T00:00:00Z"); | ||
| expect(forkedData?.isOwner).toBe(true); | ||
|
|
||
| // Assert: messages copied + optimistic message added | ||
| expect(forkedData?.messages).toHaveLength(3); | ||
| expect(forkedData?.messages?.[0]?.contentData).toBe("Hello"); | ||
| expect(forkedData?.messages?.[1]?.contentData).toBe("Hi there!"); | ||
| expect(forkedData?.messages?.[2]?.role).toBe("user"); | ||
|
|
||
| // Cleanup | ||
| conversation.cleanup(); | ||
| await sendPromise.catch(() => {}); | ||
| }); | ||
|
|
||
| it("does NOT fork when owner sends a message", async () => { | ||
| // Arrange | ||
| seedCache(ORIGINAL_CONVERSATION_ID, { isOwner: true, messages: [] }); | ||
| const conversation = createConversation(); | ||
|
|
||
| // Act | ||
| await sendMessageAndIgnoreStreamError(conversation, "My message"); | ||
|
|
||
| // Assert | ||
| expect(runtimeServiceForkConversation).not.toHaveBeenCalled(); | ||
|
|
||
| conversation.cleanup(); | ||
| }); | ||
|
|
||
| it("does NOT fork for new conversations", async () => { | ||
| // Arrange | ||
| const conversation = createConversation(NEW_CONVERSATION_ID); | ||
|
|
||
| // Act | ||
| await sendMessageAndIgnoreStreamError(conversation, "First message"); | ||
|
|
||
| // Assert | ||
| expect(runtimeServiceForkConversation).not.toHaveBeenCalled(); | ||
|
|
||
| conversation.cleanup(); | ||
| }); | ||
|
|
||
| it("sets error and stops streaming if fork API fails", async () => { | ||
| // Arrange | ||
| seedCache(ORIGINAL_CONVERSATION_ID, { isOwner: false, messages: [] }); | ||
| mockForkFailure(); | ||
| const conversation = createConversation(); | ||
|
|
||
| // Act | ||
| conversation.draftMessage.set("My message"); | ||
| await conversation.sendMessage({}); | ||
|
|
||
| // Assert | ||
| expect(get(conversation.streamError)).toContain( | ||
| "Failed to create your copy", | ||
| ); | ||
| expect(get(conversation.isStreaming)).toBe(false); | ||
|
|
||
| conversation.cleanup(); | ||
| }); | ||
|
|
||
| it("sets error if fork response is missing conversation ID", async () => { | ||
| // Arrange | ||
| seedCache(ORIGINAL_CONVERSATION_ID, { isOwner: false, messages: [] }); | ||
| mockForkEmptyResponse(); | ||
| const conversation = createConversation(); | ||
|
|
||
| // Act | ||
| conversation.draftMessage.set("My message"); | ||
| await conversation.sendMessage({}); | ||
|
|
||
| // Assert | ||
| expect(get(conversation.streamError)).toContain( | ||
| "Failed to create your copy", | ||
| ); | ||
| expect(get(conversation.isStreaming)).toBe(false); | ||
|
|
||
| conversation.cleanup(); | ||
| }); | ||
|
|
||
| it("does NOT fork when cache is empty (optimistic ownership)", async () => { | ||
| // Arrange: no cache data seeded - ownership defaults to true | ||
| const conversation = createConversation(); | ||
|
|
||
| // Act | ||
| await sendMessageAndIgnoreStreamError(conversation, "Test"); | ||
|
|
||
| // Assert | ||
| expect(runtimeServiceForkConversation).not.toHaveBeenCalled(); | ||
|
|
||
| conversation.cleanup(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
nit: lets move the tests just below mocks. It reads better IMO, tests 1st then all the utils and constants.