-
Notifications
You must be signed in to change notification settings - Fork 0
Chat Interface: LLM Call Test & Branding/UI Revamp #127
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
Ayush8923
wants to merge
19
commits into
main
Choose a base branch
from
feat/chat-llm-call
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
19 commits
Select commit
Hold shift + click to select a range
1ea8046
feat(*): initate chat feature for llm call
Ayush8923 f9518fb
Merge branch 'main' into feat/chat-llm-call
Ayush8923 289f06a
fix(*): update the sidebar ordering and few update in primary color
Ayush8923 9d681b4
fix(*): update the sidebar and their branding
Ayush8923 fbdbd3e
fix(*): make the branding update and sidebar updates
Ayush8923 2e740c5
fix(*): update the child submenu with color pellete
Ayush8923 5c2adfc
fix(chat): cleanups
Ayush8923 68d2c0a
fix(chat): cleanups
Ayush8923 919d893
fix(chat): remove the js comments and cleanups
Ayush8923 44a5b9c
fix(*): remove the unused and unwanted svg
Ayush8923 9e9f179
fix(chat): remove the js comments and cleanups
Ayush8923 c2ff835
fix(*): ui updates and clenaups
Ayush8923 cb83ed0
fix(*): ui updates
Ayush8923 d577d3d
fix(*): cleanups and refactoring
Ayush8923 a4c55fb
Merge branch 'main' into feat/chat-llm-call
Ayush8923 5c84322
fix(*): remove the webhook implementation use the polling mechanism
Ayush8923 645d1b6
Merge branch 'feat/chat-llm-call' of https://github.com/ProjectTech4D…
Ayush8923 ef172e1
fix(*): remove the js comments
Ayush8923 a7f825e
fix(*): update the suggestion prompt
Ayush8923 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| BACKEND_URL=http://localhost:8000 | ||
| GUARDRAILS_URL = http://localhost:8001 | ||
| GUARDRAILS_TOKEN = | ||
| GUARDRAILS_TOKEN = | ||
| NEXT_PUBLIC_GOOGLE_CLIENT_ID=your-google-client-id.apps.googleusercontent.com | ||
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,334 @@ | ||
| /** | ||
| * Chat - conversational interface. | ||
| */ | ||
|
|
||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect, useRef, useState } from "react"; | ||
| import Sidebar from "@/app/components/Sidebar"; | ||
| import PageHeader from "@/app/components/PageHeader"; | ||
| import { useApp } from "@/app/lib/context/AppContext"; | ||
| import { useAuth } from "@/app/lib/context/AuthContext"; | ||
| import { useToast } from "@/app/components/Toast"; | ||
| import { LoginModal } from "@/app/components/auth"; | ||
| import { | ||
| ChatConfigPicker, | ||
| ChatEmptyState, | ||
| ChatInput, | ||
| ChatMessageList, | ||
| } from "@/app/components/chat"; | ||
| import { useConfigs } from "@/app/hooks"; | ||
| import { | ||
| configToBlob, | ||
| createLLMCall, | ||
| extractAssistantText, | ||
| pollLLMCall, | ||
| } from "@/app/lib/chatClient"; | ||
| import { | ||
| ChatMessage, | ||
| LLMCallRequest, | ||
| StoredSelection, | ||
| } from "@/app/lib/types/chat"; | ||
|
|
||
| const SELECTION_STORAGE_KEY = "kaapi_chat_selection"; | ||
|
|
||
| function loadStoredSelection(): StoredSelection | null { | ||
| if (typeof window === "undefined") return null; | ||
| try { | ||
| const raw = window.localStorage.getItem(SELECTION_STORAGE_KEY); | ||
| if (!raw) return null; | ||
| const parsed = JSON.parse(raw) as StoredSelection; | ||
| if (parsed && parsed.configId && parsed.version) return parsed; | ||
| } catch { | ||
| /* ignore */ | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function genId() { | ||
| if (typeof crypto !== "undefined" && "randomUUID" in crypto) { | ||
| return crypto.randomUUID(); | ||
| } | ||
| return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`; | ||
| } | ||
|
|
||
| export default function ChatPage() { | ||
| const { sidebarCollapsed } = useApp(); | ||
| const { isAuthenticated, activeKey, isHydrated } = useAuth(); | ||
| const apiKey = activeKey?.key ?? ""; | ||
| const toast = useToast(); | ||
| const { configs, loadSingleVersion, allConfigMeta } = useConfigs({ | ||
| pageSize: 0, | ||
| }); | ||
|
|
||
| const [messages, setMessages] = useState<ChatMessage[]>([]); | ||
| const [draft, setDraft] = useState(""); | ||
| const [isPending, setIsPending] = useState(false); | ||
| const [conversationId, setConversationId] = useState<string | null>(null); | ||
| const [configId, setConfigId] = useState(""); | ||
| const [configVersion, setConfigVersion] = useState(0); | ||
| const [showLoginModal, setShowLoginModal] = useState(false); | ||
|
|
||
| const abortRef = useRef<AbortController | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const stored = loadStoredSelection(); | ||
| if (stored) { | ||
| setConfigId(stored.configId); | ||
| setConfigVersion(stored.version); | ||
| } | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!configId || !configVersion) return; | ||
| try { | ||
| window.localStorage.setItem( | ||
| SELECTION_STORAGE_KEY, | ||
| JSON.stringify({ configId, version: configVersion }), | ||
| ); | ||
| } catch { | ||
| /* ignore quota errors */ | ||
| } | ||
| }, [configId, configVersion]); | ||
|
|
||
| // Cancel any in-flight poll when leaving the page. | ||
| useEffect(() => { | ||
| return () => abortRef.current?.abort(); | ||
| }, []); | ||
|
|
||
| const updateMessage = useCallback( | ||
| (id: string, patch: Partial<ChatMessage>) => { | ||
| setMessages((prev) => | ||
| prev.map((m) => (m.id === id ? { ...m, ...patch } : m)), | ||
| ); | ||
| }, | ||
| [], | ||
| ); | ||
|
|
||
| const handleNewChat = useCallback(() => { | ||
| abortRef.current?.abort(); | ||
| abortRef.current = null; | ||
| setMessages([]); | ||
| setConversationId(null); | ||
| setIsPending(false); | ||
| }, []); | ||
|
|
||
| const handleConfigSelect = useCallback( | ||
| (newConfigId: string, newVersion: number) => { | ||
| const isDifferent = | ||
| newConfigId !== configId || newVersion !== configVersion; | ||
| setConfigId(newConfigId); | ||
| setConfigVersion(newVersion); | ||
| if (isDifferent) { | ||
| setConversationId(null); | ||
| setMessages([]); | ||
| } | ||
| }, | ||
| [configId, configVersion], | ||
| ); | ||
|
|
||
| const sendMessage = useCallback( | ||
| async (text: string) => { | ||
| const trimmed = text.trim(); | ||
| if (!trimmed) return; | ||
|
|
||
| if (!isAuthenticated) { | ||
| setShowLoginModal(true); | ||
| return; | ||
| } | ||
| if (!configId || !configVersion) { | ||
| if (allConfigMeta.length === 0) { | ||
| toast.error( | ||
| "No configurations yet — create one in Configurations → Prompt Editor first.", | ||
| ); | ||
| } else { | ||
| toast.error("Select a configuration before sending a message."); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const userMessage: ChatMessage = { | ||
| id: genId(), | ||
| role: "user", | ||
| content: trimmed, | ||
| createdAt: Date.now(), | ||
| status: "complete", | ||
| }; | ||
| const assistantMessage: ChatMessage = { | ||
| id: genId(), | ||
| role: "assistant", | ||
| content: "", | ||
| createdAt: Date.now(), | ||
| status: "pending", | ||
| }; | ||
|
|
||
| setMessages((prev) => [...prev, userMessage, assistantMessage]); | ||
| setDraft(""); | ||
| setIsPending(true); | ||
|
|
||
| const controller = new AbortController(); | ||
| abortRef.current?.abort(); | ||
| abortRef.current = controller; | ||
|
|
||
| try { | ||
| const cached = configs.find( | ||
| (c) => c.config_id === configId && c.version === configVersion, | ||
| ); | ||
| const fullConfig = | ||
| cached ?? (await loadSingleVersion(configId, configVersion)); | ||
| if (!fullConfig) { | ||
| throw new Error( | ||
| "Couldn't load the selected configuration. Try picking it again.", | ||
| ); | ||
| } | ||
|
|
||
| const payload: LLMCallRequest = { | ||
| query: { | ||
| input: trimmed, | ||
| conversation: conversationId | ||
| ? { id: conversationId } | ||
| : { auto_create: true }, | ||
| }, | ||
| config: { blob: configToBlob(fullConfig) }, | ||
| include_provider_raw_response: true, | ||
| }; | ||
|
|
||
| const created = await createLLMCall(payload, apiKey); | ||
| if (!created.success || !created.data?.job_id) { | ||
| throw new Error(created.error || "Failed to start the request"); | ||
| } | ||
| const jobId = created.data.job_id; | ||
| updateMessage(assistantMessage.id, { jobId }); | ||
|
|
||
| const result = await pollLLMCall(jobId, apiKey, { | ||
| signal: controller.signal, | ||
| }); | ||
|
|
||
| const text = extractAssistantText(result.llm_response?.response); | ||
| const newConversationId = | ||
| result.llm_response?.response?.conversation_id ?? conversationId; | ||
| if (newConversationId && newConversationId !== conversationId) { | ||
| setConversationId(newConversationId); | ||
| } | ||
|
|
||
| updateMessage(assistantMessage.id, { | ||
| content: | ||
| text || | ||
| "(The assistant returned an empty response — try again or pick a different configuration.)", | ||
| status: "complete", | ||
| }); | ||
| } catch (err) { | ||
| if ((err as Error)?.name === "AbortError") { | ||
| updateMessage(assistantMessage.id, { | ||
| status: "error", | ||
| content: "Cancelled.", | ||
| error: "Cancelled", | ||
| }); | ||
| return; | ||
| } | ||
| const message = | ||
| err instanceof Error ? err.message : "Something went wrong"; | ||
| updateMessage(assistantMessage.id, { | ||
| status: "error", | ||
| content: message, | ||
| error: message, | ||
| }); | ||
| toast.error(message); | ||
| } finally { | ||
| if (abortRef.current === controller) { | ||
| abortRef.current = null; | ||
| } | ||
| setIsPending(false); | ||
| } | ||
| }, | ||
| [ | ||
| allConfigMeta, | ||
| apiKey, | ||
| configId, | ||
| configVersion, | ||
| configs, | ||
| conversationId, | ||
| isAuthenticated, | ||
| loadSingleVersion, | ||
| toast, | ||
| updateMessage, | ||
| ], | ||
| ); | ||
|
|
||
| const hasConversation = messages.length > 0; | ||
| const hasConfig = !!configId && !!configVersion; | ||
|
|
||
| return ( | ||
| <div className="w-full h-screen flex flex-col bg-bg-secondary"> | ||
| <div className="flex flex-1 overflow-hidden"> | ||
| <Sidebar collapsed={sidebarCollapsed} activeRoute="/chat" /> | ||
|
|
||
| <div className="flex-1 flex flex-col overflow-hidden bg-bg-primary"> | ||
| <PageHeader | ||
| title="Chat" | ||
| subtitle="Ask anything - answers come from your selected configuration" | ||
| actions={ | ||
| hasConversation ? ( | ||
| <button | ||
| type="button" | ||
| onClick={handleNewChat} | ||
| className="px-3 py-1.5 rounded-full text-xs font-medium border border-border bg-bg-primary text-text-primary hover:bg-neutral-50 transition-colors cursor-pointer" | ||
| > | ||
| New chat | ||
| </button> | ||
| ) : null | ||
| } | ||
| /> | ||
|
|
||
| {!isHydrated ? ( | ||
| <div className="flex-1" /> | ||
| ) : hasConversation ? ( | ||
| <ChatMessageList messages={messages} /> | ||
| ) : ( | ||
| <ChatEmptyState | ||
| hasConfig={hasConfig} | ||
| isAuthenticated={isAuthenticated} | ||
| onSuggestion={(text) => { | ||
| if (!isAuthenticated) { | ||
| setShowLoginModal(true); | ||
| return; | ||
| } | ||
| sendMessage(text); | ||
| }} | ||
| /> | ||
| )} | ||
|
|
||
| <ChatInput | ||
| value={draft} | ||
| onChange={setDraft} | ||
| onSend={() => sendMessage(draft)} | ||
| isPending={isPending} | ||
| placeholder={ | ||
| !isAuthenticated | ||
| ? "Log in to start chatting…" | ||
| : !hasConfig | ||
| ? "Select a configuration to start chatting…" | ||
| : "Message your assistant…" | ||
| } | ||
| trailingAccessory={ | ||
| isAuthenticated ? ( | ||
| <ChatConfigPicker | ||
| configId={configId} | ||
| version={configVersion} | ||
| onSelect={handleConfigSelect} | ||
| disabled={isPending} | ||
| openUp | ||
| /> | ||
| ) : null | ||
| } | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <LoginModal | ||
| open={showLoginModal} | ||
| onClose={() => setShowLoginModal(false)} | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
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.
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.