-
Notifications
You must be signed in to change notification settings - Fork 904
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
aichat: code formatting #21342
aichat: code formatting #21342
Conversation
A Storybook has been deployed to preview UI for the latest push |
<div | ||
className={styles.message} | ||
> | ||
<div dangerouslySetInnerHTML={{ __html: escapeHTMLPolicy.createHTML(turn.text |
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.
Can we get around the need to use innerHTML. One option would be to use your regex here to build an array of matches and then render everything in a loop
- regular text block in between a match
- code block text wrapped with special element or class
And for each regular block you could use the same pattern for any inline code block matches.
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.
ok, changed. i'm building the string into parts and wrapping relevant html around it. this works, nicely.
however, i'm curious how expensive using regex is here
@@ -53,6 +53,8 @@ The current time and date is <ph name="DATE">$1</ph> | |||
Your name is Leo, a helpful, respectful and honest AI assistant created by the company Brave. You will be replying to a user of the Brave browser. Always respond in a neutral tone. Be polite and courteous. Answer concisely in no more than 50-80 words. | |||
|
|||
Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. | |||
|
|||
If you are asked to write code or json, then use this format: ```language``` |
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.
How about something like "Use unicode symbols for formatting where appropriate. Use backticks (`) to wrap inline coding-related words and triple backticks (```) to wrap blocks of code or data." cc @LorenzoMinto
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.
changed
0f932c5
to
3c0c7a9
Compare
A Storybook has been deployed to preview UI for the latest push |
3c0c7a9
to
746e06a
Compare
A Storybook has been deployed to preview UI for the latest push |
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.
strings
++
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
@brave/devops-team why was audit-deps not automatically tagged on this PR? |
@diracdeltas the label is added automatically based on the files touched by the PR. The files modified here didn't match any of the existing patterns. |
A Storybook has been deployed to preview UI for the latest push |
i don't understand, since this PR does modify package.json and package-lock.json |
Hmm, I thought the commit that touched package*.json happened after you asked the question, but actually it looks like it happened 7min before. I think we need to set up this github action differently. It currently doesn't trigger on all the relevant events. I'll look into it. |
@diracdeltas should be fixed |
A Storybook has been deployed to preview UI for the latest push |
Since this adds new deps I'm tagging @bcaller to do sec review here when he's back. |
function buildMessage(str: string) { | ||
const parts: any = [] | ||
|
||
for (const match of str.matchAll(/```([^\n`]+)?\n?([\s\S]*?)```|`(.*?)`|([^`]+)/gs)) { |
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.
No issue with adding the dependency https://github.com/react-syntax-highlighter/react-syntax-highlighter |
1fc5a0d
to
cfbf6ce
Compare
fe0a1e9
to
1caa09b
Compare
A Storybook has been deployed to preview UI for the latest push |
function buildMessage(str: string) { | ||
const parts: any = [] | ||
|
||
for (const match of str.matchAll(/```([^\n`]+)?\n?([\s\S]*?)```|`(.*?)`|([^`]+)/gs)) { |
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.
Is it more optimal to not create this regex every time the function is run? i.e. put it at the module level?
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.
updated
<div | ||
className={styles.message} | ||
> | ||
{buildMessage(turn.text)} |
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.
With the amount of time this gets re-rendered, should we memoize the result of buildMessage
, dependent on turn.text
?
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.
memoized
c6dab91
to
9655031
Compare
A Storybook has been deployed to preview UI for the latest push |
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.
If you fix the minor issues that are left, then this looks good to go from me!
text: string | ||
} | ||
|
||
function FormattedTextRenderer(props: FormattedTextProps): JSX.Element { |
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: Well, now that this is so simple, you could more simply just wrap this function in React.memo
const SUGGESTION_STATUS_SHOW_BUTTON: mojom.SuggestionGenerationStatus[] = [ | ||
mojom.SuggestionGenerationStatus.CanGenerate, | ||
mojom.SuggestionGenerationStatus.IsGenerating | ||
] | ||
|
||
const regexp = /```([^\n`]+)?\n?([\s\S]*?)```|`(.*?)`|([^`]+)/gs |
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.
this name is just the type - how about something more meaningful, like codeFormatRegexp
?
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.
Also could you add a comment explaining what the regex is attempting to achieve - something about splitting the string in to groups by backtick sequences (if I'm correct about that!)
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.
renamed and added comment
package.json
Outdated
@@ -90,6 +90,7 @@ | |||
"@types/react": "17.0.2", | |||
"@types/react-dom": "17.0.18", | |||
"@types/react-redux": "7.1.25", | |||
"@types/react-syntax-highlighter": "^15.5.11", |
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.
needs to be exact version
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.
fixed
package.json
Outdated
@@ -190,6 +191,7 @@ | |||
"react-json-view-lite": "^0.9.5", | |||
"react-router": "^5.2.1", | |||
"react-router-dom": "^5.3.0", | |||
"react-syntax-highlighter": "^15.5.0", |
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.
needs to be exact version. I know some others here aren't, but it should be.
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.
fixed
I restarted android |
9655031
to
c7b4215
Compare
A Storybook has been deployed to preview UI for the latest push |
The section should be approximately quadratic:
because a long string of 'A's which doesn't end in three backticks will match both the first However, causing any slow down would require the AI to produce such string with a length > 10000 I think and then it would only be client-side and for a second or two. Double the length would quadruple the time taken to process but I'd hope the AI rarely goes that crazy. |
Verified
|
* Merge pull request #21134 from brave/cr121 Upgrade from Chromium 120 to Chromium 121. * Remove the assert for patch_ffmpeg.py (#21184) * Merge pull request #21539 from brave/ffmpeg-dynamic-alloc Use dynamic allocation for ffmpeg fft tables on Windows. * Merge pull request #21585 from brave/issues/35318 Remove dynamic allocation of ffmpeg ff_tx tables. * Disables a flaky upstream browser test. * Merge pull request #21584 from brave/fix_new_tab_button_plus_misaligned Fixed new tab button's plus icon is mis-aligned with horizontal tab * Merge pull request #21600 from brave/121.0.6167.75_master Upgrade from Chromium 121.0.6167.57 to Chromium 121.0.6167.75. * Merge pull request #21628 from brave/maxk-disable-reading-mode Hides `Open in Reading Mode` context menu item. * [Uplift 1.62.x] AI chat issues cr121 1.62.x (#21629) * aichat: input is growable (#21124) * aichat: scroll is interruptable (#21235) * aichat: model maker text shouldnt look like a link (#21220) * aichat: code formatting (#21342) * make claude output formatted code (#21599) --------- Co-authored-by: Mikhail <matuchin@brave.com> Co-authored-by: Aleksey Khoroshilov <5928869+goodov@users.noreply.github.com> Co-authored-by: Simon Hong <shong@brave.com> Co-authored-by: taher <8665427+nullhook@users.noreply.github.com>
Resolves brave/brave-browser#33668
this PR adds formatted code UI rendering. it uses a lightweight component where syntax highlighted is limited for selected languages only.
production size:
dev size:
Submitter Checklist:
QA/Yes
orQA/No
;release-notes/include
orrelease-notes/exclude
;OS/...
) to the associated issuenpm run test -- brave_browser_tests
,npm run test -- brave_unit_tests
wikinpm run lint
,npm run presubmit
wiki,npm run gn_check
,npm run tslint
git rebase master
(if needed)Reviewer Checklist:
gn
After-merge Checklist:
changes has landed on
Test Plan: