Skip to content

Commit

Permalink
[app] Limit link detection - show all parts of messages which contain…
Browse files Browse the repository at this point in the history
… toolInvocations and text content
  • Loading branch information
alexhancock committed Nov 25, 2024
1 parent 8d9fc0d commit bd5294d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
51 changes: 27 additions & 24 deletions ui/desktop/src/components/GooseMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ interface GooseMessageProps {
}

export default function GooseMessage({ message, metadata, messages, append }: GooseMessageProps) {
// Find the preceding user message
// Extract URLs under a few conditions
// 1. The message is purely text
// 2. The link wasn't also present in the previous message
// 3. The message contains the explicit http:// or https:// protocol at the beginning
const messageIndex = messages?.findIndex(msg => msg.id === message.id);
const previousMessage = messageIndex > 0 ? messages[messageIndex - 1] : null;

// Get URLs from previous user message (if it exists)
const previousUrls = previousMessage
? extractUrls(previousMessage.content)
: [];

// Extract URLs from current message, excluding those from the previous user message
const urls = extractUrls(message.content, previousUrls);
const previousUrls = previousMessage ? extractUrls(previousMessage.content) : [];
const urls = !message.toolInvocations ? extractUrls(message.content, previousUrls) : [];

return (
<div className="flex mb-[16px]">
<div className="flex">
<div className="flex flex-col">
<div className="bg-goose-bubble text-white rounded-2xl p-4">
{message.toolInvocations ? (
{message.toolInvocations && (
<div className="bg-goose-bubble text-white rounded-2xl p-4 mb-[16px]">
<div className="space-y-4">
{message.toolInvocations.map((toolInvocation) => (
<ToolInvocation
Expand All @@ -38,18 +35,14 @@ export default function GooseMessage({ message, metadata, messages, append }: Go
/>
))}
</div>
) : (
metadata ? (
<GooseResponseForm
message={message.content}
metadata={metadata}
append={append}
/>
) : (
<ReactMarkdown className="prose">{message.content}</ReactMarkdown>
)
)}
</div>
</div>
)}

{message.content && (
<div className="bg-goose-bubble text-white rounded-2xl p-4 mb-[16px]">
<ReactMarkdown className="prose">{message.content}</ReactMarkdown>
</div>
)}

{urls.length > 0 && (
<div className="mt-2">
Expand All @@ -58,6 +51,16 @@ export default function GooseMessage({ message, metadata, messages, append }: Go
))}
</div>
)}

{false && metadata && (
<div className="bg-goose-bubble text-white rounded-2xl p-4 mb-[16px]">
<GooseResponseForm
message={message.content}
metadata={metadata}
append={append}
/>
</div>
)}
</div>
</div>
);
Expand Down
5 changes: 2 additions & 3 deletions ui/desktop/src/components/UserMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import LinkPreview from './LinkPreview'
import { extractUrls } from '../utils/urlUtils'

export default function UserMessage({ message }) {
// Extract URLs from current message
const urls = extractUrls(message.content, []); // No previous URLs to check against
console.log('User message URLs:', urls);
// Extract URLs which explicitly contain the http:// or https:// protocol
const urls = extractUrls(message.content, []);

return (
<div className="flex justify-end mb-[16px]">
Expand Down
21 changes: 8 additions & 13 deletions ui/desktop/src/utils/urlUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as linkify from 'linkifyjs';

// Helper to normalize URLs for comparison
function normalizeUrl(url: string): string {
try {
Expand All @@ -13,21 +11,18 @@ function normalizeUrl(url: string): string {
}

export function extractUrls(content: string, previousUrls: string[] = []): string[] {
// First extract markdown-style links using regex
const markdownLinkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
// Modified regex to only match markdown links with http:// or https://
const markdownLinkRegex = /\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g;
const markdownMatches = Array.from(content.matchAll(markdownLinkRegex));
const markdownUrls = markdownMatches.map(match => match[2]);

// Then use linkifyjs to find regular URLs
const links = linkify.find(content);

// Get URLs from current content
const linkifyUrls = links
.filter(link => link.type === 'url')
.map(link => link.href);
// Modified regex for standalone URLs with http:// or https://
const urlRegex = /(https?:\/\/[^\s<>"']+)/g;
const urlMatches = Array.from(content.matchAll(urlRegex));
const standardUrls = urlMatches.map(match => match[1]);

// Combine markdown URLs with linkify URLs
const currentUrls = [...new Set([...markdownUrls, ...linkifyUrls])];
// Combine markdown URLs with standard URLs
const currentUrls = [...new Set([...markdownUrls, ...standardUrls])];

// Normalize all URLs for comparison
const normalizedPreviousUrls = previousUrls.map(normalizeUrl);
Expand Down

0 comments on commit bd5294d

Please sign in to comment.