1
1
'use client' ;
2
2
3
3
import { useChat } from '@ai-sdk/react' ;
4
+ import { runAsynchronously } from '@stackframe/stack-shared/dist/utils/promises' ;
4
5
import { ExternalLink , FileText , Maximize2 , Minimize2 , Send , X } from 'lucide-react' ;
5
6
import { useEffect , useRef , useState } from 'react' ;
6
7
import { useSidebar } from '../layouts/sidebar-context' ;
@@ -36,12 +37,11 @@ const ToolCallDisplay = ({
36
37
const docId = toolCall . args ?. id ;
37
38
let docTitle = "Loading..." ;
38
39
39
- if ( toolCall . result && toolCall . result . content . length > 0 ) {
40
- const newDocTitle =
41
- toolCall . result . content [ 0 ] . text . match ( / T i t l e : \s * ( .* ) / ) ;
42
- if ( newDocTitle && newDocTitle [ 1 ] ) {
43
- docTitle = newDocTitle [ 1 ] . trim ( ) ;
44
- }
40
+ const titleMatch = toolCall . result ?. content [ 0 ] ?. text . match ( / T i t l e : \s * ( .* ) / ) ;
41
+ if ( titleMatch ?. [ 1 ] ) {
42
+ docTitle = titleMatch [ 1 ] . trim ( ) ;
43
+ } else {
44
+ toolCall . result = { content : [ { text : "No title found" } ] } ;
45
45
}
46
46
47
47
return (
@@ -52,7 +52,7 @@ const ToolCallDisplay = ({
52
52
</ span >
53
53
{ docId && (
54
54
< a
55
- href = { `https://docs.stack-auth.com${ docId } ` }
55
+ href = { `https://docs.stack-auth.com${ docId . startsWith ( '/' ) ? docId : `/ ${ docId } ` } ` }
56
56
target = "_blank"
57
57
rel = "noopener noreferrer"
58
58
className = "flex items-center gap-1 text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-200 transition-colors"
@@ -192,10 +192,7 @@ export function AIChatDrawer() {
192
192
} ,
193
193
onFinish : ( message ) => {
194
194
// Send AI response to Discord
195
- // eslint-disable-next-line no-restricted-syntax
196
- sendAIResponseToDiscord ( message . content ) . catch ( error => {
197
- console . error ( 'Failed to send AI response to Discord:' , error ) ;
198
- } ) ;
195
+ runAsynchronously ( ( ) => sendAIResponseToDiscord ( message . content ) ) ;
199
196
} ,
200
197
} ) ;
201
198
@@ -284,10 +281,7 @@ export function AIChatDrawer() {
284
281
} ) ) ;
285
282
286
283
// Send message to Discord webhook
287
- // eslint-disable-next-line no-restricted-syntax
288
- sendToDiscord ( input . trim ( ) ) . catch ( error => {
289
- console . error ( 'Discord webhook error:' , error ) ;
290
- } ) ;
284
+ runAsynchronously ( ( ) => sendToDiscord ( input . trim ( ) ) ) ;
291
285
292
286
// Continue with normal chat submission
293
287
handleSubmit ( e ) ;
@@ -317,6 +311,11 @@ export function AIChatDrawer() {
317
311
handleInputChange ( { target : { value : prompt } } as React . ChangeEvent < HTMLInputElement > ) ;
318
312
} ;
319
313
314
+ // Helper function for safe async event handling
315
+ const handleSubmitSafely = ( ) => {
316
+ runAsynchronously ( ( ) => handleChatSubmit ( { } as React . FormEvent ) ) ;
317
+ } ;
318
+
320
319
return (
321
320
< div
322
321
className = { `fixed ${ topPosition } right-0 ${ height } bg-fd-background border-l border-fd-border flex flex-col transition-all duration-300 ease-out z-50 ${
@@ -475,10 +474,7 @@ export function AIChatDrawer() {
475
474
onKeyDown = { ( e ) => {
476
475
if ( e . key === "Enter" && ! e . shiftKey ) {
477
476
e . preventDefault ( ) ;
478
- // eslint-disable-next-line no-restricted-syntax
479
- handleChatSubmit ( { } as React . FormEvent ) . catch ( error => {
480
- console . error ( 'Chat submit error:' , error ) ;
481
- } ) ;
477
+ handleSubmitSafely ( ) ;
482
478
}
483
479
} }
484
480
onPaste = { ( e ) => {
@@ -496,12 +492,7 @@ export function AIChatDrawer() {
496
492
</ div >
497
493
< button
498
494
disabled = { ! input . trim ( ) || isLoading }
499
- onClick = { ( ) => {
500
- // eslint-disable-next-line no-restricted-syntax
501
- handleChatSubmit ( { } as React . FormEvent ) . catch ( error => {
502
- console . error ( 'Chat submit error:' , error ) ;
503
- } ) ;
504
- } }
495
+ onClick = { handleSubmitSafely }
505
496
className = "h-8 w-8 rounded-full p-0 shrink-0 bg-fd-primary text-fd-primary-foreground hover:bg-fd-primary/90 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center"
506
497
>
507
498
< Send className = "w-4 h-4" />
0 commit comments