-
Notifications
You must be signed in to change notification settings - Fork 42
Reagan/make shopping experience awesome #442
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
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fc36cc6
HtmlBlock: add vertical margin so blocks breathe in the chat stream
Cheggin 3aaa82e
shell:open-external IPC for opening http(s) URLs in the default browser
Cheggin 7109d75
options block: require url+site, drop decorative images, default allo…
Cheggin 1383ee3
OptionList: shared-site header w/ favicon, View on {site} button, tal…
Cheggin 386d1b8
Teach agents to surface dense browser confirmations as HTML
Cheggin 2bdb2ba
ChosenReceipt: align with picker cards — solid border, favicon, bolde…
Cheggin 457c773
Keep dark HTML shadows structural
Cheggin f86ce1e
OptionList: keep Choose button enabled in single-select after card click
Cheggin 48bc907
Derive picker/ask submission state from the transcript
Cheggin 087594c
Harden transcript parsers against ambiguous Other replies
Cheggin 93d971a
AskForm: strip card chrome and tracked-out labels
Cheggin 9bdcd99
OptionList: drop outer container chrome to match AskForm
Cheggin 63048ec
AskForm: theme-aware checkbox checkmark via CSS mask
Cheggin cdc94dc
options block: require absolute http(s) URLs for url and image
Cheggin f479d33
Apply transcript-derived submission when it arrives after first render
Cheggin b755bda
Let transcript-derived state override cache bootstrap
Cheggin d658fb1
Re-run transcript hydration after a failed local submit
Cheggin 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
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 |
|---|---|---|
|
|
@@ -22,6 +22,10 @@ interface Props { | |
| complete: boolean; | ||
| error?: string; | ||
| sessionId?: string; | ||
| /** User reply turn that follows this form, if any. Used to reconstruct | ||
| * the submitted answers in historical sessions — see OptionList for the | ||
| * same pattern. */ | ||
| nextUserText?: string | null; | ||
| } | ||
|
|
||
| const OTHER_TOKEN = '__other__'; | ||
|
|
@@ -56,7 +60,7 @@ function decodeAskSelection(value: string): { question: string; label: string } | |
| } | ||
|
|
||
| export function AskForm(props: Props): React.ReactElement { | ||
| const { payload, complete, error, sessionId } = props; | ||
| const { payload, complete, error, sessionId, nextUserText } = props; | ||
| if (!payload) { | ||
| if (complete && error) { | ||
| return ( | ||
|
|
@@ -67,7 +71,7 @@ export function AskForm(props: Props): React.ReactElement { | |
| } | ||
| return <AskFormSkeleton />; | ||
| } | ||
| return <AskFormReady payload={payload} sessionId={sessionId} streaming={!complete} />; | ||
| return <AskFormReady payload={payload} sessionId={sessionId} streaming={!complete} nextUserText={nextUserText} />; | ||
| } | ||
|
|
||
| function AskFormSkeleton(): React.ReactElement { | ||
|
|
@@ -89,9 +93,10 @@ interface ReadyProps { | |
| payload: AskFormPayload; | ||
| sessionId?: string; | ||
| streaming?: boolean; | ||
| nextUserText?: string | null; | ||
| } | ||
|
|
||
| function AskFormReady({ payload, sessionId, streaming }: ReadyProps): React.ReactElement { | ||
| function AskFormReady({ payload, sessionId, streaming, nextUserText }: ReadyProps): React.ReactElement { | ||
| const { questions, prompt } = payload; | ||
| const formRef = useRef<HTMLDivElement | null>(null); | ||
|
|
||
|
|
@@ -104,11 +109,20 @@ function AskFormReady({ payload, sessionId, streaming }: ReadyProps): React.Reac | |
| }, [sessionId, questions]); | ||
| const cachedRecord = useMemo(() => getSubmissionRecord(cacheKey), [cacheKey]); | ||
|
|
||
| // Transcript-derived submission — read the user's next-turn reply for | ||
| // an "Answered: …" block and reconstruct selection. Wins over the | ||
| // in-memory cache so reopened sessions stay correct without persistence. | ||
| const transcriptSubmission = useMemo( | ||
| () => deriveAskSubmission(nextUserText, questions), | ||
| [nextUserText, questions], | ||
| ); | ||
|
|
||
| // Per-question selected labels. Use `Set<string>` so single + multi | ||
| // share the same state shape; "Other" picks store the literal | ||
| // OTHER_TOKEN. Per-question typed-other text in a parallel array. | ||
| const [selectedByQuestion, setSelectedByQuestion] = useState<Set<string>[]>( | ||
| () => questions.map((q) => { | ||
| () => questions.map((q, i) => { | ||
| if (transcriptSubmission) return new Set(transcriptSubmission.selection[i]); | ||
| if (!cachedRecord) return new Set(); | ||
| const qKey = questionCacheKey(q); | ||
| const restored = new Set<string>(); | ||
|
|
@@ -123,13 +137,39 @@ function AskFormReady({ payload, sessionId, streaming }: ReadyProps): React.Reac | |
| }), | ||
| ); | ||
| const [otherTextByQuestion, setOtherTextByQuestion] = useState<string[]>( | ||
| () => questions.map((q) => cachedRecord?.otherTextByKey?.[questionCacheKey(q)] ?? ''), | ||
| () => questions.map((q) => ( | ||
| transcriptSubmission?.otherTextByKey[questionCacheKey(q)] | ||
| ?? cachedRecord?.otherTextByKey?.[questionCacheKey(q)] | ||
| ?? '' | ||
| )), | ||
| ); | ||
| const [submitted, setSubmitted] = useState<boolean>( | ||
| transcriptSubmission !== null || cachedRecord !== null, | ||
| ); | ||
| const [submitted, setSubmitted] = useState<boolean>(cachedRecord !== null); | ||
| const [submitError, setSubmitError] = useState<string | null>(null); | ||
| // Tracks "user clicked Confirm in this mount" vs "bootstrapped from | ||
| // cache". Only a local submit makes our state authoritative — a cache | ||
| // bootstrap is just a hint and should yield to a later-arriving | ||
| // transcript, which is the durable source of truth. State (not a ref) | ||
| // so toggling it back after a failed submit re-runs the hydration | ||
| // effect to pick up any transcript that was already waiting. | ||
| const [localSubmit, setLocalSubmit] = useState<boolean>(false); | ||
|
|
||
| const locked = submitted; | ||
|
|
||
| // Late-arriving transcript: if nextUserText hydrates after first paint | ||
| // (streaming session restore, async transcript fetch), apply the derived | ||
| // submission. Skip only when the user has submitted locally in this | ||
| // mount — bootstrap-from-cache must not block transcript hydration. | ||
| useEffect(() => { | ||
| if (!transcriptSubmission || localSubmit) return; | ||
| setSelectedByQuestion(questions.map((_, i) => new Set(transcriptSubmission.selection[i]))); | ||
| setOtherTextByQuestion(questions.map((q) => ( | ||
| transcriptSubmission.otherTextByKey[questionCacheKey(q)] ?? '' | ||
| ))); | ||
| setSubmitted(true); | ||
| }, [transcriptSubmission, localSubmit, questions]); | ||
|
|
||
| const togglePick = useCallback((qIdx: number, label: string): void => { | ||
| const q = questions[qIdx]; | ||
| if (!q) return; | ||
|
|
@@ -192,13 +232,15 @@ function AskFormReady({ payload, sessionId, streaming }: ReadyProps): React.Reac | |
| return; | ||
| } | ||
| const message = formatAnswerMessage(questions, selectedByQuestion, otherTextByQuestion); | ||
| setLocalSubmit(true); | ||
| setSubmitted(true); | ||
| setSubmitError(null); | ||
| try { | ||
| const result = await window.electronAPI?.sessions?.resume(sessionId, message); | ||
| if (result?.error) { | ||
| setSubmitError(result.error); | ||
| setSubmitted(false); | ||
| setLocalSubmit(false); | ||
| } else { | ||
| // Persist enough to restore submitted view on remount. | ||
| const flat: string[] = []; | ||
|
|
@@ -214,6 +256,7 @@ function AskFormReady({ payload, sessionId, streaming }: ReadyProps): React.Reac | |
| } catch (err) { | ||
| setSubmitError((err as Error).message); | ||
| setSubmitted(false); | ||
| setLocalSubmit(false); | ||
| } | ||
| }, [canSubmit, locked, sessionId, questions, selectedByQuestion, otherTextByQuestion, cacheKey]); | ||
|
|
||
|
|
@@ -313,7 +356,6 @@ function QuestionCard({ question, selected, otherText, locked, onToggle, onOther | |
| return ( | ||
| <div className="chatv2-askform__question"> | ||
| <div className="chatv2-askform__question-head"> | ||
| {question.header && <span className="chatv2-askform__question-header">{question.header}</span>} | ||
| <span className="chatv2-askform__question-text">{question.question}</span> | ||
| </div> | ||
| <ul className="chatv2-askform__options" role={question.multiSelect ? 'group' : 'radiogroup'}> | ||
|
|
@@ -360,6 +402,67 @@ function QuestionCard({ question, selected, otherText, locked, onToggle, onOther | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse of formatAnswerMessage: parse the user-reply turn that follows | ||
| * this form and reconstruct which options were chosen per question. | ||
| * Returns null when the text isn't an "Answered: …" reply for this form. | ||
| * | ||
| * Exported for tests. | ||
| */ | ||
| export function deriveAskSubmission( | ||
| text: string | null | undefined, | ||
| questions: AskQuestion[], | ||
| ): { selection: Set<string>[]; otherTextByKey: Record<string, string> } | null { | ||
| if (!text) return null; | ||
| const head = text.trimStart(); | ||
| if (!head.startsWith('Answered:')) return null; | ||
|
|
||
| const selection: Set<string>[] = questions.map(() => new Set<string>()); | ||
| const otherTextByKey: Record<string, string> = {}; | ||
|
|
||
| for (const rawLine of text.split('\n')) { | ||
| const m = rawLine.match(/^-\s*([^:]+):\s*(.+)$/); | ||
| if (!m) continue; | ||
| const labelPrefix = m[1].trim(); | ||
| const valuesStr = m[2].trim(); | ||
| const qIdx = questions.findIndex((q) => (q.header || q.question) === labelPrefix); | ||
| if (qIdx < 0) continue; | ||
|
|
||
| // Values are comma-separated. A free-text `Other: <text>` answer can | ||
| // itself contain commas, so we can't blindly split. Strategy: find | ||
| // ", Other:" (or a leading "Other:") and treat everything from there | ||
| // to the end of the line as a single Other value. The remainder is | ||
| // safe to split on /,\s+/ because predefined option labels are | ||
| // controlled by the agent and don't carry user free text. | ||
| let valuesPart = valuesStr; | ||
| let otherTail: string | null = null; | ||
| const otherIdx = (() => { | ||
| const leading = valuesPart.match(/^Other(?::|$)/); | ||
|
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. P1: The new Prompt for AI agents |
||
| if (leading) return 0; | ||
| const m = valuesPart.match(/,\s+Other(?::|$)/); | ||
| return m && m.index !== undefined ? m.index + m[0].indexOf('Other') : -1; | ||
| })(); | ||
| if (otherIdx >= 0) { | ||
| otherTail = valuesPart.slice(otherIdx); | ||
| valuesPart = valuesPart.slice(0, otherIdx).replace(/,\s*$/, ''); | ||
| } | ||
| for (const raw of valuesPart.split(/,\s+/)) { | ||
| const v = raw.trim(); | ||
| if (!v) continue; | ||
| selection[qIdx].add(v); | ||
| } | ||
| if (otherTail !== null) { | ||
| selection[qIdx].add(OTHER_TOKEN); | ||
| if (otherTail.startsWith('Other:')) { | ||
| otherTextByKey[questionCacheKey(questions[qIdx])] = otherTail.slice('Other:'.length).trim(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (selection.every((s) => s.size === 0)) return null; | ||
| return { selection, otherTextByKey }; | ||
| } | ||
|
|
||
| function formatAnswerMessage( | ||
| questions: AskQuestion[], | ||
| selectedByQuestion: Set<string>[], | ||
|
|
||
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.