Your always-on AI coworkers that move between text, calls, and Slack.
TypeScript
fix(research): keep balanced brackets in URLs from the research prompt (#99)
Charan Rathore0aee4540af46
diff --git a/src/server/research.ts b/src/server/research.tsindex a5954b9..c925c40 100644--- a/src/server/research.ts+++ b/src/server/research.ts@@ -20,6 +20,24 @@ const modelResponse = z.object({.array(z.object({ message: z.object({ content: z.string().min(1) }) })).min(1),});+const closers: Record<string, string> = { ')': '(', ']': '[' };+export function requestedUrls(prompt: string): string[] {+ // Split Markdown link boundaries so a URL used as a link label does not+ // swallow the destination: [https://a](https://b) holds two URLs.+ const text = prompt.replace(/\]\(/g, '] (');+ const urls = (text.match(/https?:\/\/[^\s<>"']+/gi) ?? []).map((raw) => {+ let url = raw;+ for (;;) {+ const last = url.at(-1)!;+ const open = closers[last];+ const unbalanced =+ open !== undefined && url.split(last).length > url.split(open).length;+ if (/[.,;!?:]/.test(last) || unbalanced) url = url.slice(0, -1);+ else return url;+ }+ });+ return [...new Set(urls)];+}export function configured(config: Config): boolean {return (config.mode === 'sample' ||@@ -76,11 +94,9 @@ export async function research(const limitations: string[] = [];let screenshot: string | undefined;if ((config.webSearchProvider ?? 'parallel') === 'parallel') {- const urls = prompt- .match(/https?:\/\/[^\s<>"'\])]+/gi)- ?.map((url) => url.replace(/[.,;!?]+$/, ''));+ const urls = requestedUrls(prompt);progress(- urls?.length+ urls.length? 'Reading the requested sources with Parallel.': 'Searching and reading public sources with Parallel.',);@@ -97,12 +113,11 @@ export async function research(signal,);} else {- const match = prompt.match(/https?:\/\/[^\s<>"'\])]+/i);- if (!match)+ const url = requestedUrls(prompt)[0];+ if (!url)throw new Error('Please include a public https:// page URL. Open-ended web search is not configured; OpenDots will not invent sources.',);- const url = match[0].replace(/[.,;!?]+$/, '');progress('Reading the requested public page in the isolated browser.');const response = await fetch(`${config.browserUrl!.replace(/\/$/, '')}/browse`,diff --git a/tests/research.test.ts b/tests/research.test.tsindex 73a96d4..9b23a06 100644--- a/tests/research.test.ts+++ b/tests/research.test.ts@@ -1,5 +1,9 @@import { afterEach, describe, expect, it, vi } from 'vitest';-import { research, type Config } from '../src/server/research.js';+import {+ requestedUrls,+ research,+ type Config,+} from '../src/server/research.js';const signal = new AbortController().signal;const config: Config = {mode: 'live',@@ -92,4 +96,33 @@ describe('research adapters', () => {research('Read https://example.com', [], config, signal, () => {}),).rejects.toThrow('429');});+ it('keeps balanced brackets in requested URLs and trims wrapping ones', () => {+ expect(+ requestedUrls(+ 'Read https://en.wikipedia.org/wiki/Rust_(programming_language).',+ ),+ ).toEqual(['https://en.wikipedia.org/wiki/Rust_(programming_language)']);+ expect(+ requestedUrls(+ 'See (https://example.com/a) and [https://example.com/b], or [x](https://example.com/c).',+ ),+ ).toEqual([+ 'https://example.com/a',+ 'https://example.com/b',+ 'https://example.com/c',+ ]);+ expect(+ requestedUrls('Read https://example.com twice https://example.com'),+ ).toEqual(['https://example.com']);+ });+ it('parses Markdown links whose label is also a URL as separate occurrences', () => {+ expect(+ requestedUrls('Summarize [https://example.com](https://example.com).'),+ ).toEqual(['https://example.com']);+ expect(+ requestedUrls(+ 'See [https://example.com/label](https://example.com/dest) for details.',+ ),+ ).toEqual(['https://example.com/label', 'https://example.com/dest']);+ });});