-
-
Notifications
You must be signed in to change notification settings - Fork 175
Version-1 changes for Maple Chatbot pr_2198_bot #2218
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
vkavith
wants to merge
3
commits into
codeforboston:main
Choose a base branch
from
vkavith:maple_pr_2198_bot
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
3 commits
Select commit
Hold shift + click to select a range
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,91 @@ | ||
| .widget { | ||
| display: flex; | ||
| flex-direction: column; | ||
| max-width: 480px; | ||
| height: 480px; | ||
| border: 1px solid #d9d9d9; | ||
| border-radius: 8px; | ||
| overflow: hidden; | ||
| font-size: 0.9rem; | ||
| } | ||
|
|
||
| .header { | ||
| padding: 0.75rem 1rem; | ||
| border-bottom: 1px solid #d9d9d9; | ||
| } | ||
|
|
||
| .header h3 { | ||
| margin: 0; | ||
| font-size: 1rem; | ||
| } | ||
|
|
||
| .hint { | ||
| margin: 0.25rem 0 0; | ||
| font-size: 0.75rem; | ||
| color: #666; | ||
| } | ||
|
|
||
| .messages { | ||
| flex: 1; | ||
| overflow-y: auto; | ||
| padding: 0.75rem 1rem; | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 0.5rem; | ||
| } | ||
|
|
||
| .welcome { | ||
| color: #666; | ||
| } | ||
|
|
||
| .message { | ||
| padding: 0.5rem 0.75rem; | ||
| border-radius: 8px; | ||
| max-width: 85%; | ||
| white-space: pre-wrap; | ||
| } | ||
|
|
||
| .user { | ||
| align-self: flex-end; | ||
| background: #0a5c36; | ||
| color: #fff; | ||
| } | ||
|
|
||
| .assistant { | ||
| align-self: flex-start; | ||
| background: #f1f1f1; | ||
| color: #111; | ||
| } | ||
|
|
||
| .error { | ||
| color: #b00020; | ||
| font-size: 0.85rem; | ||
| } | ||
|
|
||
| .inputRow { | ||
| display: flex; | ||
| gap: 0.5rem; | ||
| padding: 0.75rem; | ||
| border-top: 1px solid #d9d9d9; | ||
| } | ||
|
|
||
| .input { | ||
| flex: 1; | ||
| padding: 0.5rem; | ||
| border: 1px solid #ccc; | ||
| border-radius: 4px; | ||
| } | ||
|
|
||
| .submit { | ||
| padding: 0.5rem 1rem; | ||
| border: none; | ||
| border-radius: 4px; | ||
| background: #0a5c36; | ||
| color: #fff; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .submit:disabled { | ||
| opacity: 0.5; | ||
| cursor: default; | ||
| } |
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,104 @@ | ||
| import { useState, useRef, useEffect } from "react" | ||
| import { httpsCallable } from "firebase/functions" | ||
| import { functions } from "components/firebase" | ||
| import { useAuth } from "components/auth" | ||
| import styles from "./ChatWidget.module.css" | ||
|
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. i think we need to add a mock frontend pag that just exposes this widget on a placeholder page for initial testing purposes. |
||
|
|
||
| type AskQuestionRequest = { question: string } | ||
| type AskQuestionResponse = { | ||
| answer: string | ||
| usage: { tokensUsed: number; isLoggedIn: boolean } | ||
| } | ||
|
|
||
| const askQuestion = httpsCallable<AskQuestionRequest, AskQuestionResponse>( | ||
| functions, | ||
| "askQuestion" | ||
| ) | ||
|
|
||
| type ChatMessage = { | ||
| id: string | ||
| role: "user" | "assistant" | ||
| content: string | ||
| } | ||
|
|
||
| /** | ||
| * Lightweight bill/policy Q&A chat widget backed by the LangGraph ReAct | ||
| * agent (functions/src/llm). Dependency-free beyond firebase/functions, so | ||
| * it can be dropped into any page. | ||
| */ | ||
| export function ChatWidget({ title = "Ask about bills & policy" }: { title?: string }) { | ||
| const { user } = useAuth() | ||
| const [messages, setMessages] = useState<ChatMessage[]>([]) | ||
| const [input, setInput] = useState("") | ||
| const [loading, setLoading] = useState(false) | ||
| const [error, setError] = useState<string | null>(null) | ||
| const endRef = useRef<HTMLDivElement>(null) | ||
|
|
||
| useEffect(() => { | ||
| endRef.current?.scrollIntoView({ behavior: "smooth" }) | ||
| }, [messages, loading]) | ||
|
|
||
| const handleSubmit = async (e: React.FormEvent) => { | ||
| e.preventDefault() | ||
| const question = input.trim() | ||
| if (!question || loading) return | ||
|
|
||
| setMessages(prev => [...prev, { id: crypto.randomUUID(), role: "user", content: question }]) | ||
| setInput("") | ||
| setLoading(true) | ||
| setError(null) | ||
|
|
||
| try { | ||
| const result = await askQuestion({ question }) | ||
| setMessages(prev => [ | ||
| ...prev, | ||
| { id: crypto.randomUUID(), role: "assistant", content: result.data.answer } | ||
| ]) | ||
| } catch (err: any) { | ||
| setError(err?.message ?? "Something went wrong. Please try again.") | ||
| } finally { | ||
| setLoading(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className={styles.widget}> | ||
| <div className={styles.header}> | ||
| <h3>{title}</h3> | ||
| {!user && <p className={styles.hint}>Sign in for a higher daily usage limit.</p>} | ||
| </div> | ||
|
|
||
| <div className={styles.messages}> | ||
| {messages.length === 0 && ( | ||
| <p className={styles.welcome}> | ||
| Ask a question about a bill, testimony, or ballot question. | ||
| </p> | ||
| )} | ||
| {messages.map(message => ( | ||
| <div key={message.id} className={`${styles.message} ${styles[message.role]}`}> | ||
| {message.content} | ||
| </div> | ||
| ))} | ||
| {loading && <div className={`${styles.message} ${styles.assistant}`}>Thinking…</div>} | ||
| {error && <div className={styles.error}>{error}</div>} | ||
| <div ref={endRef} /> | ||
| </div> | ||
|
|
||
| <form className={styles.inputRow} onSubmit={handleSubmit}> | ||
| <input | ||
| className={styles.input} | ||
| type="text" | ||
| value={input} | ||
| onChange={e => setInput(e.target.value)} | ||
| placeholder="e.g. What bills address education funding?" | ||
| disabled={loading} | ||
| /> | ||
| <button className={styles.submit} type="submit" disabled={loading || !input.trim()}> | ||
| Send | ||
| </button> | ||
| </form> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default ChatWidget | ||
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.
A big open question is if we should be authoring our own chat widget or adopting a framework like copilotkit instead.