Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(reasoning): open links in reasoning blocks through ExternalAnchor

James Dam2076fa01da47
diff --git a/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx b/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx
index b092312fb..08149f647 100644
--- a/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx
+++ b/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx
@@ -10,8 +10,28 @@ import {
useReasoning,
} from '../reasoning'
+const streamdown = vi.hoisted(() => ({
+ components: undefined as
+ | Record<string, (props: Record<string, unknown>) => ReactNode>
+ | undefined,
+}))
vi.mock('streamdown', () => ({
- Streamdown: ({ children }: { children: string }) => <span>{children}</span>,
+ Streamdown: ({
+ children,
+ components,
+ }: {
+ children: string
+ components?: typeof streamdown.components
+ }) => {
+ streamdown.components = components
+ return <span>{children}</span>
+ },
+}))
+
+const openExternalUrl = vi.hoisted(() => vi.fn())
+vi.mock('@/lib/externalLink', async (importActual) => ({
+ ...(await importActual<typeof import('@/lib/externalLink')>()),
+ openExternalUrl,
}))
vi.mock('../shimmer', () => ({
@@ -194,3 +214,33 @@ describe('useReasoning', () => {
expect(typeof result.current.setIsOpen).toBe('function')
})
})
+
+describe('ReasoningContent links', () => {
+ const renderLink = (href: string) => {
+ render(
+ <Reasoning>
+ <ReasoningContent>see the docs</ReasoningContent>
+ </Reasoning>
+ )
+ const A = streamdown.components!.a
+ render(<A href={href}>docs</A>)
+ return screen.getByText('docs')
+ }
+
+ it('opens a web link outside the app', () => {
+ openExternalUrl.mockClear()
+ const link = renderLink('https://jan.ai/docs')
+ expect(link).toHaveAttribute('href', 'https://jan.ai/docs')
+ expect(link).toHaveAttribute('target', '_blank')
+ act(() => link.click())
+ expect(openExternalUrl).toHaveBeenCalledWith('https://jan.ai/docs')
+ })
+
+ it('renders any other link as inert text', () => {
+ openExternalUrl.mockClear()
+ const link = renderLink('javascript:alert(1)')
+ expect(link).not.toHaveAttribute('href')
+ act(() => link.click())
+ expect(openExternalUrl).not.toHaveBeenCalled()
+ })
+})
diff --git a/web-app/src/components/ai-elements/reasoning.tsx b/web-app/src/components/ai-elements/reasoning.tsx
index 708155d7c..b94e5d07d 100644
--- a/web-app/src/components/ai-elements/reasoning.tsx
+++ b/web-app/src/components/ai-elements/reasoning.tsx
@@ -17,6 +17,8 @@ import {
useState,
} from 'react'
import { Streamdown } from 'streamdown'
+import type { Components } from 'react-markdown'
+import { ExternalAnchor } from '@/components/ExternalAnchor'
import { Shimmer } from './shimmer'
type ReasoningContextValue = {
@@ -166,6 +168,17 @@ export type ReasoningContentProps = ComponentProps<
children: string
}
+/** Reasoning is model output, so its links take the same path as the message
+ * body's: a web or mail link opens outside the app, anything else is inert. */
+const REASONING_COMPONENTS: Components = {
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ a: ({ node: _node, href, children, ...rest }) => (
+ <ExternalAnchor {...rest} href={href}>
+ {children}
+ </ExternalAnchor>
+ ),
+}
+
export const ReasoningContent = memo(
({ className, children, ...props }: ReasoningContentProps) => (
<CollapsibleContent
@@ -177,7 +190,12 @@ export const ReasoningContent = memo(
{...props}
>
<div className="ml-2 pl-4 border-l-2 border-dotted">
- <Streamdown animate={true} animationDuration={500} {...props}>
+ <Streamdown
+ animate={true}
+ animationDuration={500}
+ {...props}
+ components={REASONING_COMPONENTS}
+ >
{children}
</Streamdown>
</div>