forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: v2 - ai assistant chat window #2327
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
maxy-shpfy
wants to merge
1
commit into
master
Choose a base branch
from
05-26-feat_v2_-_ai_assistant_chat_window
base: master
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
29 changes: 29 additions & 0 deletions
29
src/routes/v2/pages/Editor/components/AiChat/AiChatContent.tsx
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,29 @@ | ||
| import { observer } from "mobx-react-lite"; | ||
|
|
||
| import { BlockStack } from "@/components/ui/layout"; | ||
| import useToastNotification from "@/hooks/useToastNotification"; | ||
|
|
||
| import { useAiChatStore } from "./AiChatStoreContext"; | ||
| import { ChatInput } from "./components/ChatInput"; | ||
| import { ChatMessageList } from "./components/ChatMessageList"; | ||
|
|
||
| export const AiChatContent = observer(function AiChatContent() { | ||
| const aiChat = useAiChatStore(); | ||
| const notify = useToastNotification(); | ||
|
|
||
| function handleSend(prompt: string) { | ||
| aiChat.sendMessage(prompt, { | ||
| onError: (msg) => notify(msg, "error"), | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <BlockStack className="h-full" gap="0"> | ||
| <ChatMessageList | ||
| messages={aiChat.messages} | ||
| thinkingText={aiChat.thinkingText} | ||
| /> | ||
| <ChatInput isPending={aiChat.isPending} onSubmit={handleSend} /> | ||
| </BlockStack> | ||
| ); | ||
| }); | ||
23 changes: 23 additions & 0 deletions
23
src/routes/v2/pages/Editor/components/AiChat/AiChatStoreContext.tsx
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,23 @@ | ||
| import type { ReactNode } from "react"; | ||
| import { useState } from "react"; | ||
|
|
||
| import { | ||
| createRequiredContext, | ||
| useRequiredContext, | ||
| } from "@/hooks/useRequiredContext"; | ||
|
|
||
| import { AiChatStore } from "./aiChatStore"; | ||
|
|
||
| const AiChatStoreCtx = createRequiredContext<AiChatStore>("AiChatStoreContext"); | ||
|
|
||
| export function AiChatStoreProvider({ children }: { children: ReactNode }) { | ||
| const [store] = useState(() => new AiChatStore()); | ||
|
|
||
| return ( | ||
| <AiChatStoreCtx.Provider value={store}>{children}</AiChatStoreCtx.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useAiChatStore(): AiChatStore { | ||
| return useRequiredContext(AiChatStoreCtx); | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/routes/v2/pages/Editor/components/AiChat/aiChat.types.ts
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,11 @@ | ||
| export interface ComponentRefData { | ||
| name: string; | ||
| yamlText: string; | ||
| } | ||
|
|
||
| export interface ChatMessage { | ||
| id: string; | ||
| role: "user" | "assistant"; | ||
| content: string; | ||
| componentReferences?: Record<string, ComponentRefData>; | ||
| } |
82 changes: 82 additions & 0 deletions
82
src/routes/v2/pages/Editor/components/AiChat/aiChatStore.ts
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,82 @@ | ||
| import { action, makeObservable, observable, runInAction } from "mobx"; | ||
|
|
||
| import { getErrorMessage } from "@/utils/string"; | ||
|
|
||
| import type { ChatMessage } from "./aiChat.types"; | ||
|
|
||
| function generateMessageId(): string { | ||
| return `msg-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`; | ||
| } | ||
|
Comment on lines
+7
to
+9
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't we already have a library for this? maybe even |
||
|
|
||
| interface SendMessageOptions { | ||
| onError: (message: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Stores AI chat state (messages, thread, pending status) outside the | ||
| * React component tree so it survives window minimize / hide / unmount. | ||
| * | ||
| * PR 1: `sendMessage` is a stub that appends a hardcoded assistant echo. | ||
| * The real worker / LLM round-trip lands in PR 2 / PR 3. | ||
| */ | ||
| export class AiChatStore { | ||
| @observable.shallow accessor messages: ChatMessage[] = []; | ||
| @observable accessor threadId: string | undefined = undefined; | ||
| @observable accessor thinkingText: string | null = null; | ||
| @observable accessor isPending = false; | ||
|
|
||
| private abortController: AbortController | null = null; | ||
|
|
||
| constructor() { | ||
| makeObservable(this); | ||
| } | ||
|
|
||
| @action resetState() { | ||
| this.messages = []; | ||
| this.threadId = undefined; | ||
| this.thinkingText = null; | ||
| this.isPending = false; | ||
| this.abortController?.abort(); | ||
| this.abortController = null; | ||
| } | ||
|
|
||
| abort() { | ||
| this.abortController?.abort(); | ||
| } | ||
|
|
||
| async sendMessage(prompt: string, options: SendMessageOptions) { | ||
| runInAction(() => { | ||
| this.messages = [ | ||
| ...this.messages, | ||
| { id: generateMessageId(), role: "user", content: prompt }, | ||
| ]; | ||
| this.isPending = true; | ||
| this.thinkingText = null; | ||
| }); | ||
|
|
||
| try { | ||
| /** | ||
| * Echo the user's message back to the user. | ||
| * TODO: replace with actual AI response. | ||
| */ | ||
| runInAction(() => { | ||
| this.messages = [ | ||
| ...this.messages, | ||
| { | ||
| id: generateMessageId(), | ||
| role: "assistant", | ||
| content: `${prompt}`, | ||
| }, | ||
| ]; | ||
| }); | ||
| } catch (error) { | ||
| options.onError(`AI request failed: ${getErrorMessage(error)}`); | ||
| } finally { | ||
| this.abortController = null; | ||
| runInAction(() => { | ||
| this.isPending = false; | ||
| this.thinkingText = null; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
src/routes/v2/pages/Editor/components/AiChat/components/ChatEntityChip.tsx
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,37 @@ | ||
| import { type ButtonHTMLAttributes, forwardRef } from "react"; | ||
|
|
||
| import { Icon, type IconName } from "@/components/ui/icon"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| interface ChatEntityChipProps extends Omit< | ||
| ButtonHTMLAttributes<HTMLButtonElement>, | ||
| "type" | ||
| > { | ||
| icon: IconName; | ||
| label: string; | ||
| } | ||
|
|
||
| export const ChatEntityChip = forwardRef< | ||
| HTMLButtonElement, | ||
| ChatEntityChipProps | ||
| >(function ChatEntityChip( | ||
| { icon, label, draggable, className, ...buttonProps }, | ||
| ref, | ||
| ) { | ||
| return ( | ||
| <button | ||
| ref={ref} | ||
| type="button" | ||
| draggable={draggable} | ||
| className={cn( | ||
| "inline-flex items-center gap-1 rounded-md border bg-background px-1.5 py-0.5 text-xs font-medium text-foreground hover:bg-accent transition-colors align-middle", | ||
| draggable ? "cursor-grab" : "cursor-pointer", | ||
| className, | ||
| )} | ||
| {...buttonProps} | ||
| > | ||
| <Icon name={icon} className="size-3 shrink-0 text-muted-foreground" /> | ||
| <span className="truncate max-w-[160px]">{label}</span> | ||
| </button> | ||
| ); | ||
| }); |
52 changes: 52 additions & 0 deletions
52
src/routes/v2/pages/Editor/components/AiChat/components/ChatInput.tsx
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,52 @@ | ||
| import { type KeyboardEvent, useState } from "react"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import { Icon } from "@/components/ui/icon"; | ||
| import { InlineStack } from "@/components/ui/layout"; | ||
| import { Textarea } from "@/components/ui/textarea"; | ||
|
|
||
| interface ChatInputProps { | ||
| isPending: boolean; | ||
| onSubmit: (prompt: string) => void; | ||
| } | ||
|
|
||
| export function ChatInput({ isPending, onSubmit }: ChatInputProps) { | ||
| const [prompt, setPrompt] = useState(""); | ||
|
|
||
| function handleSubmit() { | ||
| const trimmed = prompt.trim(); | ||
| if (!trimmed || isPending) return; | ||
| setPrompt(""); | ||
| onSubmit(trimmed); | ||
| } | ||
|
|
||
| function handleKeyDown(e: KeyboardEvent<HTMLTextAreaElement>) { | ||
| if (e.key === "Enter" && !e.shiftKey) { | ||
| e.preventDefault(); | ||
| handleSubmit(); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <InlineStack gap="2" className="border-t p-2 w-full" blockAlign="start"> | ||
| <Textarea | ||
| className="flex-1 resize-none max-h-32 overflow-y-auto" | ||
| rows={2} | ||
| placeholder="Ask about your pipeline..." | ||
| value={prompt} | ||
| onChange={(e) => setPrompt(e.target.value)} | ||
| onKeyDown={handleKeyDown} | ||
| disabled={isPending} | ||
| /> | ||
| <Button | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={handleSubmit} | ||
| disabled={isPending || !prompt.trim()} | ||
| aria-label="Send message" | ||
| > | ||
| <Icon name={isPending ? "Loader" : "Send"} /> | ||
| </Button> | ||
| </InlineStack> | ||
| ); | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/routes/v2/pages/Editor/components/AiChat/components/ChatMessage.tsx
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,27 @@ | ||
| import { Text } from "@/components/ui/typography"; | ||
| import type { ChatMessage as ChatMessageType } from "@/routes/v2/pages/Editor/components/AiChat/aiChat.types"; | ||
|
|
||
| import { MessageBubble } from "./MessageBubble"; | ||
| import { renderMarkdown } from "./renderMarkdown"; | ||
|
|
||
| interface ChatMessageProps { | ||
| message: ChatMessageType; | ||
| } | ||
|
|
||
| export function ChatMessage({ message }: ChatMessageProps) { | ||
| const isUser = message.role === "user"; | ||
|
|
||
| return ( | ||
| <MessageBubble variant={isUser ? "user" : "assistant"} gap="1"> | ||
| {isUser ? ( | ||
| <Text size="sm" className="break-words"> | ||
| {message.content} | ||
| </Text> | ||
| ) : ( | ||
| <div className="text-sm break-words min-w-0 overflow-x-auto"> | ||
| {renderMarkdown(message.content, message.componentReferences)} | ||
| </div> | ||
| )} | ||
| </MessageBubble> | ||
| ); | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/routes/v2/pages/Editor/components/AiChat/components/ChatMessageList.tsx
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,44 @@ | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| import { BlockStack } from "@/components/ui/layout"; | ||
| import { Text } from "@/components/ui/typography"; | ||
| import type { ChatMessage as ChatMessageType } from "@/routes/v2/pages/Editor/components/AiChat/aiChat.types"; | ||
|
|
||
| import { ChatMessage } from "./ChatMessage"; | ||
| import { ThinkingMessage } from "./ThinkingMessage"; | ||
|
|
||
| interface ChatMessageListProps { | ||
| messages: ChatMessageType[]; | ||
| thinkingText?: string | null; | ||
| } | ||
|
|
||
| export function ChatMessageList({ | ||
| messages, | ||
| thinkingText, | ||
| }: ChatMessageListProps) { | ||
| const bottomRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| bottomRef.current?.scrollIntoView({ behavior: "smooth" }); | ||
| }, [messages.length, thinkingText]); | ||
|
|
||
| if (messages.length === 0 && !thinkingText) { | ||
| return ( | ||
| <BlockStack align="center" inlineAlign="center" className="flex-1 p-4"> | ||
| <Text size="sm" tone="subdued"> | ||
| Ask anything about your pipeline | ||
| </Text> | ||
| </BlockStack> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <BlockStack gap="2" className="flex-1 overflow-y-auto p-3"> | ||
| {messages.map((msg) => ( | ||
| <ChatMessage key={msg.id} message={msg} /> | ||
| ))} | ||
| {thinkingText && <ThinkingMessage text={thinkingText} />} | ||
| <div ref={bottomRef} /> | ||
| </BlockStack> | ||
| ); | ||
| } |
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: Do we have a prop to handle
h-fullalready?