From 4809d75f0ef9b9ed02adfc673fff914747f5c630 Mon Sep 17 00:00:00 2001 From: TheNolle Date: Tue, 10 Nov 2026 14:09:20 +0100 Subject: [PATCH] fix: export reliability, code-language fallback and CodeQL findings; release 1.1.2 --- ai/chatgpt/CHANGELOG.md | 11 + ai/chatgpt/docs/troubleshooting.md | 4 + ai/chatgpt/package.json | 2 +- ai/chatgpt/src/dom-markdown.ts | 221 +++++++++-------- ai/perplexity/CHANGELOG.md | 12 + ai/perplexity/docs/code-and-export.md | 10 + ai/perplexity/package.json | 2 +- ai/perplexity/src/export-source.ts | 252 +++++++++++++++----- bun.lock | 6 +- core/ai/CHANGELOG.md | 6 + core/ai/package.json | 2 +- core/ai/src/features/code-blocks/guess.ts | 4 +- core/ai/src/features/exporter/dialog.ts | 3 +- core/ai/src/features/exporter/markdown.ts | 274 ++++++++++++---------- core/ai/src/placeholders.ts | 58 ++++- 15 files changed, 562 insertions(+), 305 deletions(-) diff --git a/ai/chatgpt/CHANGELOG.md b/ai/chatgpt/CHANGELOG.md index f2f15e5..4f351db 100644 --- a/ai/chatgpt/CHANGELOG.md +++ b/ai/chatgpt/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## [1.1.2] - 2026-11-10 + +### Fixed + +- Fixed language detection crashing when code has no recognized language signals. +- Replaced the placeholder regular expression with a single-pass delimiter scanner. +- Fixed Markdown table escaping for backslashes and pipes. +- Fixed shared export parsing of escaped table cells. +- Export failures are now logged with their actual error. + ## [1.1.1] - 2026-11-10 ### Export and reliability @@ -67,3 +77,4 @@ [1.0.0]: https://github.com/thenolle/userscripts/releases/tag/better-chatgpt-v1.0.0 [1.1.0]: https://github.com/thenolle/userscripts/releases/tag/better-chatgpt-v1.1.0 [1.1.1]: https://github.com/thenolle/userscripts/releases/tag/better-chatgpt-v1.1.1 +[1.1.2]: https://github.com/thenolle/userscripts/releases/tag/better-chatgpt-v1.1.2 diff --git a/ai/chatgpt/docs/troubleshooting.md b/ai/chatgpt/docs/troubleshooting.md index 13d5cf0..145f554 100644 --- a/ai/chatgpt/docs/troubleshooting.md +++ b/ai/chatgpt/docs/troubleshooting.md @@ -13,3 +13,7 @@ Open the browser console on chatgpt.com and run `betterChatgptDump()`. It prints - **Export finds no messages:** ChatGPT changed how it marks messages. The dump shows `page.turns`; if both counts are 0 while messages are on screen, report it. - **Limits are empty:** send a message or reload the page. - **Update check failed (404):** the first release has not been published yet. + +## Code and table fixes in 1.1.2 + +Code with no recognized language is displayed using the plain-text fallback. Markdown table exports escape backslashes and pipes; the shared document parser preserves those cell contents. diff --git a/ai/chatgpt/package.json b/ai/chatgpt/package.json index 9835dca..f870aa1 100644 --- a/ai/chatgpt/package.json +++ b/ai/chatgpt/package.json @@ -1,6 +1,6 @@ { "name": "@nolly/ai-chatgpt", - "version": "1.1.1", + "version": "1.1.2", "private": true, "type": "module", "license": "PolyForm-Strict-1.0.0", diff --git a/ai/chatgpt/src/dom-markdown.ts b/ai/chatgpt/src/dom-markdown.ts index bbb0a45..5a94083 100644 --- a/ai/chatgpt/src/dom-markdown.ts +++ b/ai/chatgpt/src/dom-markdown.ts @@ -11,123 +11,144 @@ const tex = (element: HTMLElement): string => element.querySelector('annotation[ const wrap = (mark: string, value: string): string => (value.trim() ? `${mark}${value.trim()}${mark}` : value) const inline = (node: Node): string => { - if (node.nodeType === Node.TEXT_NODE) return (node.textContent ?? '').replace(/\s+/g, ' ') - if (!(node instanceof HTMLElement) || skipped(node)) return '' - if (node.classList.contains('katex-display')) return `\n\n$$${tex(node)}$$\n\n` - if (node.classList.contains('katex')) return `$${tex(node)}$` - if (node.hasAttribute('data-pill')) { - const href = node.querySelector('a[href]')?.getAttribute('href') - const label = (node.textContent ?? '').trim() - return href && label ? ` [${label}](${href})` : '' - } - const inner = (): string => [...node.childNodes].map(inline).join('') - if (node.hasAttribute('data-d-default-strong')) return wrap('**', inner()) - switch (node.tagName) { - case 'BR': - return ' \n' - case 'STRONG': - case 'B': - return wrap('**', inner()) - case 'EM': - case 'I': - return wrap('*', inner()) - case 'DEL': - case 'S': - return wrap('~~', inner()) - case 'CODE': - return `\`${node.textContent ?? ''}\`` - case 'A': { - const href = node.getAttribute('href') - const label = inner().trim() - return href && label ? `[${label}](${href})` : label - } - case 'IMG': { - const source = node.getAttribute('src') - return source ? `![${node.getAttribute('alt') ?? ''}](${source})` : '' - } - default: - return inner() - } + if (node.nodeType === Node.TEXT_NODE) return (node.textContent ?? '').replace(/\s+/g, ' ') + if (!(node instanceof HTMLElement) || skipped(node)) return '' + if (node.classList.contains('katex-display')) return `\n\n$$${tex(node)}$$\n\n` + if (node.classList.contains('katex')) return `$${tex(node)}$` + if (node.hasAttribute('data-pill')) { + const href = node.querySelector('a[href]')?.getAttribute('href') + const label = (node.textContent ?? '').trim() + return href && label ? ` [${label}](${href})` : '' + } + const inner = (): string => [...node.childNodes].map(inline).join('') + if (node.hasAttribute('data-d-default-strong')) return wrap('**', inner()) + switch (node.tagName) { + case 'BR': + return ' \n' + case 'STRONG': + case 'B': + return wrap('**', inner()) + case 'EM': + case 'I': + return wrap('*', inner()) + case 'DEL': + case 'S': + return wrap('~~', inner()) + case 'CODE': + return `\`${node.textContent ?? ''}\`` + case 'A': { + const href = node.getAttribute('href') + const label = inner().trim() + return href && label ? `[${label}](${href})` : label + } + case 'IMG': { + const source = node.getAttribute('src') + return source ? `![${node.getAttribute('alt') ?? ''}](${source})` : '' + } + default: + return inner() + } } const fence = (pre: HTMLElement): string => { - const raw = pre.closest('[data-chatgpt-copy-code]')?.getAttribute('data-chatgpt-copy-code') ?? (pre.querySelector('code') ?? pre).textContent ?? '' - const label = pre.closest('[data-code-block]')?.querySelector('[data-markdown-copy="exclude"] .truncate')?.textContent ?? '' - const language = label.trim().toLowerCase().replace(/\s+/g, '-') - return `\`\`\`${language}\n${raw.replace(/\n$/, '')}\n\`\`\`` + const raw = pre.closest('[data-chatgpt-copy-code]')?.getAttribute('data-chatgpt-copy-code') ?? (pre.querySelector('code') ?? pre).textContent ?? '' + const label = pre.closest('[data-code-block]')?.querySelector('[data-markdown-copy="exclude"] .truncate')?.textContent ?? '' + const language = label.trim().toLowerCase().replace(/\s+/g, '-') + return `\`\`\`${language}\n${raw.replace(/\n$/, '')}\n\`\`\`` +} + +const escapeTableCell = (value: string): string => { + const parts: string[] = [] + + for (const character of value) { + if (character === '\\' || character === '|') parts.push('\\') + parts.push(character) + } + + return parts.join('').replace(/\s+/g, ' ').trim() } const table = (element: HTMLElement): string => { - const rows = [...element.querySelectorAll('tr')].map((row) => [...row.children].map((cell) => inline(cell).replace(/\|/g, '\\|').replace(/\s+/g, ' ').trim())) - if (!rows.length) return '' - const width = Math.max(...rows.map((row) => row.length)) - const line = (row: string[]): string => `| ${Array.from({ length: width }, (_, index) => row[index] ?? '').join(' | ')} |` - const [head, ...body] = rows - return [line(head ?? []), `| ${Array.from({ length: width }, () => '---').join(' | ')} |`, ...body.map(line)].join('\n') + const rows = [...element.querySelectorAll('tr')].filter((row) => row.closest('table') === element).map((row) => [...row.children].map((cell) => escapeTableCell(inline(cell)))) + + if (!rows.length) return '' + + const width = rows.reduce((maximum, row) => Math.max(maximum, row.length), 0) + if (!width) return '' + + const line = (row: readonly string[]): string => `| ${Array.from({ length: width }, (_, index) => row[index] ?? '').join(' | ')} |` + + const [head, ...body] = rows + + return [ + line(head ?? []), + line(Array.from({ length: width }, () => '---')), + ...body.map(line) + ].join('\n') } const flat = (node: Node): Node[] => (node instanceof HTMLElement && node.tagName === 'DIV' && !skipped(node) ? [...node.childNodes].flatMap(flat) : [node]) const list = (element: HTMLElement, depth: number): string => { - const ordered = element.tagName === 'OL' - let index = Number.parseInt(element.getAttribute('start') ?? '1', 10) || 1 - const indent = ' '.repeat(depth) - const lines: string[] = [] - for (const item of element.children) { - if (!(item instanceof HTMLElement) || item.tagName !== 'LI' || skipped(item)) continue - const own: string[] = [] - const nested: string[] = [] - for (const child of [...item.childNodes].flatMap(flat)) { - if (child instanceof HTMLElement && (child.tagName === 'UL' || child.tagName === 'OL')) nested.push(list(child, depth + 1)) - else if (child instanceof HTMLElement && child.tagName === 'PRE') nested.push(fence(child).split('\n').map((line) => `${indent} ${line}`).join('\n')) - else own.push(child instanceof HTMLElement && child.tagName === 'P' ? `${inline(child)} ` : inline(child)) - } - lines.push(`${indent}${ordered ? `${index++}.` : '-'} ${own.join('').trim()}`, ...nested.filter(Boolean)) - } - return lines.join('\n') + const ordered = element.tagName === 'OL' + let index = Number.parseInt(element.getAttribute('start') ?? '1', 10) || 1 + const indent = ' '.repeat(depth) + const lines: string[] = [] + for (const item of element.children) { + if (!(item instanceof HTMLElement) || item.tagName !== 'LI' || skipped(item)) continue + const own: string[] = [] + const nested: string[] = [] + for (const child of [...item.childNodes].flatMap(flat)) { + if (child instanceof HTMLElement && (child.tagName === 'UL' || child.tagName === 'OL')) nested.push(list(child, depth + 1)) + else if (child instanceof HTMLElement && child.tagName === 'PRE') nested.push(fence(child).split('\n').map((line) => `${indent} ${line}`).join('\n')) + else own.push(child instanceof HTMLElement && child.tagName === 'P' ? `${inline(child)} ` : inline(child)) + } + lines.push(`${indent}${ordered ? `${index++}.` : '-'} ${own.join('').trim()}`, ...nested.filter(Boolean)) + } + return lines.join('\n') } const block = (element: HTMLElement): string => { - if (skipped(element)) return '' - const heading = HEADING.exec(element.tagName) - if (heading) return `${'#'.repeat(Number(heading[1]))} ${[...element.childNodes].map(inline).join('').trim()}` - switch (element.tagName) { - case 'P': - return [...element.childNodes].map(inline).join('').trim() - case 'UL': - case 'OL': - return list(element, 0) - case 'PRE': - return fence(element) - case 'HR': - return '---' - case 'TABLE': - return table(element) - case 'BLOCKQUOTE': - return children(element).split('\n').map((line) => `> ${line}`).join('\n') - default: - return children(element) - } + if (skipped(element)) return '' + const heading = HEADING.exec(element.tagName) + if (heading) return `${'#'.repeat(Number(heading[1]))} ${[...element.childNodes].map(inline).join('').trim()}` + switch (element.tagName) { + case 'P': + return [...element.childNodes].map(inline).join('').trim() + case 'UL': + case 'OL': + return list(element, 0) + case 'PRE': + return fence(element) + case 'HR': + return '---' + case 'TABLE': + return table(element) + case 'BLOCKQUOTE': + return children(element).split('\n').map((line) => `> ${line}`).join('\n') + default: + return children(element) + } } const children = (element: HTMLElement): string => { - const parts: string[] = [] - let run = '' - const flush = (): void => { - if (run.trim()) parts.push(run.trim()) - run = '' - } - for (const child of element.childNodes) { - if (child instanceof HTMLElement && BLOCKS.has(child.tagName)) { - flush() - const value = block(child) - if (value) parts.push(value) - } else { - run += inline(child) - } - } - flush() - return parts.join('\n\n') + const parts: string[] = [] + let run = '' + const flush = (): void => { + if (run.trim()) parts.push(run.trim()) + run = '' + } + for (const child of element.childNodes) { + if (child instanceof HTMLElement && BLOCKS.has(child.tagName)) { + flush() + const value = block(child) + if (value) parts.push(value) + } else { + run += inline(child) + } + } + flush() + return parts.join('\n\n') } export const toMarkdown = (root: HTMLElement): string => children(root).replace(/\n{3,}/g, '\n\n').trim() diff --git a/ai/perplexity/CHANGELOG.md b/ai/perplexity/CHANGELOG.md index ef93b8c..506f3e7 100644 --- a/ai/perplexity/CHANGELOG.md +++ b/ai/perplexity/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to Better Perplexity are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/). +## [1.1.2] - 2026-11-10 + +### Fixed + +- Fixed language detection crashing when code has no recognized language signals. +- Replaced the placeholder regular expression with a single-pass delimiter scanner. +- Added exporter request timeouts, transport fallback and response validation. +- Export errors now identify the endpoint, HTTP status and transport. +- Preserved thread-UUID recovery when the first export identifier is rejected. +- Fixed shared export parsing of escaped table cells. + ## [1.1.1] - 2026-11-10 ### Export and reliability @@ -158,3 +169,4 @@ First public release. [1.0.5-hotfix.1]: https://github.com/thenolle/userscripts/releases/tag/better-perplexity-v1.0.5-hotfix.1 [1.1.0]: https://github.com/thenolle/userscripts/releases/tag/better-perplexity-v1.1.0 [1.1.1]: https://github.com/thenolle/userscripts/releases/tag/better-perplexity-v1.1.1 +[1.1.2]: https://github.com/thenolle/userscripts/releases/tag/better-perplexity-v1.1.2 diff --git a/ai/perplexity/docs/code-and-export.md b/ai/perplexity/docs/code-and-export.md index 35fc2ca..193e93c 100644 --- a/ai/perplexity/docs/code-and-export.md +++ b/ai/perplexity/docs/code-and-export.md @@ -37,3 +37,13 @@ Choose how sources appear: parenthesized links, inline links, domain names, a nu ## PDF workflow PDF saving uses the browser print dialog. The preview contains selectable text, linked contents, styled sections, code panels and tables. See [PDF reports](pdf-reports.md) for colors, page numbers and limitations. + +## Export failures + +If export fails, the dialog reports the request and transport error. The browser console also logs “Conversation export failed”. + +The exporter tries its existing session API with sandbox fetch, then page fetch where available. Requests have a timeout. If the first identifier fails, a thread-detail lookup may supply the thread UUID. + +Raw JSON is a separate thread request, not a guaranteed offline fallback. If both requests fail, include their status messages in a bug report. Do not include tokens or private thread contents. + +The exporter does not disable CORS, CSP, subresource integrity, browser tracking protection or site access restrictions. diff --git a/ai/perplexity/package.json b/ai/perplexity/package.json index e4d3ab0..5bf85a7 100644 --- a/ai/perplexity/package.json +++ b/ai/perplexity/package.json @@ -1,6 +1,6 @@ { "name": "@nolly/ai-perplexity", - "version": "1.1.1", + "version": "1.1.2", "private": true, "type": "module", "license": "PolyForm-Strict-1.0.0", diff --git a/ai/perplexity/src/export-source.ts b/ai/perplexity/src/export-source.ts index 873b856..998928c 100644 --- a/ai/perplexity/src/export-source.ts +++ b/ai/perplexity/src/export-source.ts @@ -1,89 +1,213 @@ import type { ExportSource } from '@nolly/ai' +import { getPageWindow } from '@nolly/core' import { storage } from './core/storage' import { readCookieAccountId } from './modules/usage-status/account-cookie' -interface ExportedFile { - filename: string - bytes: Uint8Array -} - -const NON_THREAD_ROOTS = new Set(['discover', 'library', 'spaces', 'settings', 'account', 'finance', 'computer', 'pages', 'collections']) const ACCOUNT_KEY = 'usage-status:account-id' +const TIMEOUT = 20_000 const BASE_HEADERS = { 'x-app-apiclient': 'default', 'x-app-apiversion': '2.18' } +const NON_THREAD_ROOTS = new Set(['discover', 'library', 'spaces', 'settings', 'account', 'finance', 'computer', 'pages', 'collections']) const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i -const request = (path: string, init: RequestInit = {}): Promise => { - const account = readCookieAccountId() ?? storage.get(ACCOUNT_KEY, null) - const extra = (init.headers ?? {}) as Record - return fetch(`${location.origin}${path}`, { - credentials: 'include', - ...init, - headers: { ...BASE_HEADERS, ...(account ? { 'x-pplx-account': account } : {}), ...extra } - }) -} +type JsonObject = Record +type Transport = 'sandbox' | 'page' + +const record = (value: unknown): JsonObject | null => value !== null && typeof value === 'object' && !Array.isArray(value) ? value as JsonObject : null + +const messageOf = (error: unknown): string => error instanceof Error ? error.message : String(error) const currentThreadSlug = (): string | null => { - const segments = location.pathname.split('/').filter(Boolean) - return segments.length >= 2 && !NON_THREAD_ROOTS.has(segments[0]!) ? (segments.at(-1) ?? null) : null + const segments = location.pathname.split('/').filter(Boolean) + if (segments.length < 2 || NON_THREAD_ROOTS.has(segments[0]!)) return null + + try { + return decodeURIComponent(segments.at(-1)!) + } catch { + return null + } } -const deepFind = (value: unknown, keys: readonly string[], depth = 0): string | null => { - if (!value || typeof value !== 'object' || depth > 6) return null - for (const [key, nested] of Object.entries(value)) { - if (keys.includes(key) && typeof nested === 'string' && UUID.test(nested)) return nested - } - for (const nested of Object.values(value)) { - const found = deepFind(nested, keys, depth + 1) - if (found) return found - } - return null +const headersFor = (init: RequestInit): Record => { + const account = readCookieAccountId() ?? storage.get(ACCOUNT_KEY, null) + const headers = new Headers(BASE_HEADERS) + + if (account) headers.set('x-pplx-account', account) + new Headers(init.headers).forEach((value, key) => headers.set(key, value)) + + return Object.fromEntries(headers.entries()) } -const postExport = async (id: string, format: string, filename: string): Promise => { - const response = await request('/rest/thread/export', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ thread_uuid: id, format, filename }) }) - if (!response.ok) throw new Error(`Export request returned HTTP ${response.status}`) - const data = (await response.json()) as { file_content_64?: string; filename?: string } - if (!data.file_content_64) throw new Error('The export response had no file content.') - return { filename: data.filename ?? `${filename}.${format}`, bytes: Uint8Array.from(atob(data.file_content_64), (char) => char.charCodeAt(0)) } +const fetchWithTimeout = async (transport: Transport, path: string, init: RequestInit): Promise => { + const controller = new AbortController() + let cancel: (() => void) | undefined + + const timeout = new Promise((_resolve, reject) => { + const timer = window.setTimeout(() => { + controller.abort() + reject(new Error(`${transport}: request timed out after ${TIMEOUT / 1000}s`)) + }, TIMEOUT) + cancel = () => window.clearTimeout(timer) + }) + + const url = `${location.origin}${path}` + + try { + let request: Promise + + if (transport === 'page') { + const page = getPageWindow() + if (!page?.fetch) throw new Error('Page fetch is unavailable') + + const options: RequestInit = { + ...init, + credentials: 'include', + headers: headersFor(init) + } + + const pageOptions = typeof cloneInto === 'function' ? cloneInto(options, page) : options + + request = page.fetch(url, pageOptions) + } else { + request = fetch(url, { + ...init, + credentials: 'include', + headers: headersFor(init), + signal: controller.signal + }) + } + + return await Promise.race([request, timeout]) + } finally { + cancel?.() + } } -const fetchThread = async (slug: string): Promise => { - const response = await request(`/rest/thread/${encodeURIComponent(slug)}`) - if (!response.ok) throw new Error(`Thread request returned HTTP ${response.status}`) - return response.json() +const requestJson = async (path: string, init: RequestInit = {}): Promise => { + const errors: string[] = [] + const transports: Transport[] = getPageWindow()?.fetch ? ['sandbox', 'page'] : ['sandbox'] + + for (const transport of transports) { + try { + const response = await fetchWithTimeout(transport, path, init) + const contentType = response.headers.get('content-type') ?? 'unknown' + + if (!response.ok) throw new Error(`HTTP ${response.status}; content-type ${contentType}`) + + const raw = await response.text() + if (!raw.trim()) throw new Error('Empty response') + + try { + return JSON.parse(raw) as unknown + } catch { + throw new Error(`Expected JSON; content-type ${contentType}`) + } + } catch (error) { + errors.push(`${transport}: ${messageOf(error)}`) + } + } + + throw new Error(`${init.method ?? 'GET'} ${path}: ${errors.join(' | ')}`) } -const exportThread = async (format: 'md' | 'docx' | 'pdf', filename: string): Promise => { - const slug = currentThreadSlug() - if (!slug) throw new Error('Open a thread first.') - try { - return await postExport(slug, format, filename) - } catch (first) { - const uuid = deepFind(await fetchThread(slug).catch(() => null), ['thread_uuid', 'uuid']) - if (!uuid || uuid === slug) throw first - return postExport(uuid, format, filename) - } +const findThreadId = (value: unknown): string | null => { + const visited = new WeakSet() + const queue: { value: unknown; depth: number }[] = [{ value, depth: 0 }] + let cursor = 0 + + while (cursor < queue.length && cursor < 5_000) { + const entry = queue[cursor++]! + if (!entry.value || typeof entry.value !== 'object' || entry.depth > 6) continue + if (visited.has(entry.value)) continue + visited.add(entry.value) + + const object = record(entry.value) + if (object) { + for (const key of ['thread_uuid', 'uuid']) { + const candidate = object[key] + if (typeof candidate === 'string' && UUID.test(candidate)) return candidate + } + } + + for (const nested of Object.values(entry.value)) { + if (nested && typeof nested === 'object') { + queue.push({ value: nested, depth: entry.depth + 1 }) + } + } + } + + return null } -const fetchThreadJson = async (): Promise => { - const slug = currentThreadSlug() - if (!slug) throw new Error('Open a thread first.') - return JSON.stringify(await fetchThread(slug), null, 2) +const fetchThread = async (slug: string): Promise => requestJson(`/rest/thread/${encodeURIComponent(slug)}`) + +const postMarkdown = async (id: string, filename: string): Promise => { + const data = record(await requestJson('/rest/thread/export', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ + thread_uuid: id, + format: 'md', + filename + }) + })) + + const encoded = data?.['file_content_64'] + if (typeof encoded !== 'string' || !encoded.trim()) throw new Error('The export endpoint did not return file_content_64') + + let binary: string + try { + binary = atob(encoded) + } catch { + throw new Error('The export endpoint returned invalid base64') + } + + const markdown = new TextDecoder().decode(Uint8Array.from(binary, (character) => character.charCodeAt(0))) + + if (!markdown.trim()) throw new Error('The exported Markdown was empty') + return markdown } -const fileBase = (): string => (currentThreadSlug() ?? 'perplexity-thread').replace(/[^\w-]+/g, '-').slice(0, 60) || 'perplexity-thread' +const markdown = async (): Promise => { + const slug = currentThreadSlug() + if (!slug) throw new Error('Open a conversation before exporting.') + + const filename = slug.replace(/[^\w-]+/g, '-').slice(0, 80) || 'perplexity-thread' + let firstError: unknown + + try { + return await postMarkdown(slug, filename) + } catch (error) { + firstError = error + } + + let thread: unknown + try { + thread = await fetchThread(slug) + } catch (error) { + throw new Error(`Export failed: ${messageOf(firstError)}. Thread lookup also failed: ${messageOf(error)}`) + } + + const id = findThreadId(thread) + if (!id || id === slug) throw new Error(`Export failed: ${messageOf(firstError)}. Thread lookup did not provide a different thread UUID.`) + + return postMarkdown(id, filename) +} export const exportSource: ExportSource = { - name: 'Perplexity', - citations: true, - slug: currentThreadSlug, - markdown: async () => new TextDecoder().decode((await exportThread('md', fileBase())).bytes), - json: fetchThreadJson, - place: (button) => { - const indicator = document.querySelector('.bp-usage-indicator') - if (!indicator) return false - const gear = indicator.querySelector('.bp-gear') - if (button.parentElement !== indicator || (gear && button.nextElementSibling !== gear)) indicator.insertBefore(button, gear) - return true - } + name: 'Perplexity', + citations: true, + slug: currentThreadSlug, + markdown, + json: async () => { + const slug = currentThreadSlug() + if (!slug) throw new Error('Open a conversation before exporting.') + return JSON.stringify(await fetchThread(slug), null, 2) + }, + place: (button) => { + const indicator = document.querySelector('.bp-usage-indicator') + if (!indicator) return false + const gear = indicator.querySelector('.bp-gear') + if (button.parentElement !== indicator || (gear && button.nextElementSibling !== gear)) indicator.insertBefore(button, gear) + return true + } } diff --git a/bun.lock b/bun.lock index 39c51dc..a2787a2 100644 --- a/bun.lock +++ b/bun.lock @@ -7,7 +7,7 @@ }, "ai/chatgpt": { "name": "@nolly/ai-chatgpt", - "version": "1.1.1", + "version": "1.1.2", "dependencies": { "@nolly/ai": "workspace:*", "@nolly/core": "workspace:*", @@ -19,7 +19,7 @@ }, "ai/perplexity": { "name": "@nolly/ai-perplexity", - "version": "1.1.1", + "version": "1.1.2", "dependencies": { "@nolly/ai": "workspace:*", "@nolly/core": "workspace:*", @@ -39,7 +39,7 @@ }, "core/ai": { "name": "@nolly/ai", - "version": "0.2.0", + "version": "0.2.1", "dependencies": { "@nolly/core": "workspace:*", }, diff --git a/core/ai/CHANGELOG.md b/core/ai/CHANGELOG.md index 4a9b2f2..55d30c2 100644 --- a/core/ai/CHANGELOG.md +++ b/core/ai/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.2.1] - 2026-11-10 + +- Replaced placeholder matching with a non-backtracking scanner and Set-based field deduplication. +- Fixed language detection for an empty score map. +- Fixed escaped table-cell parsing and added export error logging. + ## [0.2.0] - 2026-11-10 ### Export and reliability diff --git a/core/ai/package.json b/core/ai/package.json index 84cb6c0..cfb2ecc 100644 --- a/core/ai/package.json +++ b/core/ai/package.json @@ -1,6 +1,6 @@ { "name": "@nolly/ai", - "version": "0.2.0", + "version": "0.2.1", "private": true, "type": "module", "license": "PolyForm-Strict-1.0.0", diff --git a/core/ai/src/features/code-blocks/guess.ts b/core/ai/src/features/code-blocks/guess.ts index a1fbd1a..de0dd1e 100644 --- a/core/ai/src/features/code-blocks/guess.ts +++ b/core/ai/src/features/code-blocks/guess.ts @@ -50,6 +50,6 @@ export const guessLanguage = (text: string): string | null => { const typed = TYPESCRIPT_SIGNALS.filter((pattern) => pattern.test(sample)).length const javascript = scores.get('javascript') ?? 0 if (typed >= 1 && javascript + typed * 3 >= MIN_SCORE) scores.set('typescript', javascript + typed * 3) - const [[bestLanguage, bestScore]] = [...scores.entries()].sort(([, a], [, b]) => b - a) - return bestLanguage && bestScore >= MIN_SCORE ? bestLanguage : null + const best = [...scores.entries()].sort(([, a], [, b]) => b - a)[0] + return best && best[1] >= MIN_SCORE ? best[0] : null } \ No newline at end of file diff --git a/core/ai/src/features/exporter/dialog.ts b/core/ai/src/features/exporter/dialog.ts index 8d1edf8..3477984 100644 --- a/core/ai/src/features/exporter/dialog.ts +++ b/core/ai/src/features/exporter/dialog.ts @@ -1,5 +1,5 @@ import { createElement } from '@nolly/core' -import { platform } from '../../platform' +import { logger, platform } from '../../platform' import { createButton, createSelectField, createSwitch } from '../../ui/controls' import { createDialog } from '../../ui/dialog' import type { CitationStyle } from './citations' @@ -58,6 +58,7 @@ export const toggleExportDialog = (settings: ExportSettings, onSettings: (settin try { status.textContent = await runExport(state, action) } catch (error) { + logger.error('Conversation export failed.', error) status.textContent = error instanceof Error ? error.message : 'Export failed.' } finally { copyButton.disabled = false diff --git a/core/ai/src/features/exporter/markdown.ts b/core/ai/src/features/exporter/markdown.ts index 38f5899..860628b 100644 --- a/core/ai/src/features/exporter/markdown.ts +++ b/core/ai/src/features/exporter/markdown.ts @@ -1,19 +1,19 @@ export interface Inline { - text: string - bold?: boolean - italic?: boolean - code?: boolean - link?: string + text: string + bold?: boolean + italic?: boolean + code?: boolean + link?: string } export type Block = - | { type: 'heading'; level: number; inlines: Inline[] } - | { type: 'paragraph'; inlines: Inline[] } - | { type: 'code'; language: string; text: string } - | { type: 'item'; ordered: boolean; number: number; depth: number; inlines: Inline[] } - | { type: 'quote'; inlines: Inline[] } - | { type: 'rule' } - | { type: 'table'; header: Inline[][]; rows: Inline[][][] } + | { type: 'heading'; level: number; inlines: Inline[] } + | { type: 'paragraph'; inlines: Inline[] } + | { type: 'code'; language: string; text: string } + | { type: 'item'; ordered: boolean; number: number; depth: number; inlines: Inline[] } + | { type: 'quote'; inlines: Inline[] } + | { type: 'rule' } + | { type: 'table'; header: Inline[][]; rows: Inline[][][] } type Style = Omit @@ -26,125 +26,149 @@ const QUOTE = /^\s*>\s?(.*)$/ const SEPARATOR = /^\s*\|?\s*:?-{2,}:?\s*(?:\|\s*:?-{2,}:?\s*)*\|?\s*$/ export const parseInlines = (text: string, base: Style = {}): Inline[] => { - const result: Inline[] = [] - let last = 0 - for (const match of text.matchAll(INLINE)) { - const index = match.index ?? 0 - const g = match.groups ?? {} - if (index > last) result.push({ ...base, text: text.slice(last, index) }) - if (g['code'] !== undefined) result.push({ ...base, text: g['code'], code: true }) - else if (g['bold'] !== undefined || g['bold2'] !== undefined) result.push(...parseInlines(g['bold'] ?? g['bold2'] ?? '', { ...base, bold: true })) - else if (g['label'] !== undefined && g['url'] !== undefined) result.push(...parseInlines(g['label'] || g['url'], { ...base, link: g['url'] })) - else if (g['italic'] !== undefined || g['italic2'] !== undefined) result.push(...parseInlines(g['italic'] ?? g['italic2'] ?? '', { ...base, italic: true })) - else if (g['note'] !== undefined) result.push({ ...base, text: `[${g['note']}]` }) - last = index + match[0].length - } - if (last < text.length) result.push({ ...base, text: text.slice(last) }) - return result + const result: Inline[] = [] + let last = 0 + for (const match of text.matchAll(INLINE)) { + const index = match.index ?? 0 + const g = match.groups ?? {} + if (index > last) result.push({ ...base, text: text.slice(last, index) }) + if (g['code'] !== undefined) result.push({ ...base, text: g['code'], code: true }) + else if (g['bold'] !== undefined || g['bold2'] !== undefined) result.push(...parseInlines(g['bold'] ?? g['bold2'] ?? '', { ...base, bold: true })) + else if (g['label'] !== undefined && g['url'] !== undefined) result.push(...parseInlines(g['label'] || g['url'], { ...base, link: g['url'] })) + else if (g['italic'] !== undefined || g['italic2'] !== undefined) result.push(...parseInlines(g['italic'] ?? g['italic2'] ?? '', { ...base, italic: true })) + else if (g['note'] !== undefined) result.push({ ...base, text: `[${g['note']}]` }) + last = index + match[0].length + } + if (last < text.length) result.push({ ...base, text: text.slice(last) }) + return result } -const splitRow = (line: string): string[] => line.trim().replace(/^\|/, '').replace(/\|$/, '').split('|').map((cell) => cell.trim()) +const splitRow = (line: string): string[] => { + const source = line.trim() + const cells: string[] = [] + let cell = '' + + for (let index = 0; index < source.length; index++) { + const character = source[index]! + const next = source[index + 1] + + if (character === '\\' && (next === '\\' || next === '|')) { + cell += next + index++ + } else if (character === '|') { + cells.push(cell.trim()) + cell = '' + } else { + cell += character + } + } + + cells.push(cell.trim()) + if (source.startsWith('|')) cells.shift() + if (source.endsWith('|') && cells.at(-1) === '') cells.pop() + return cells +} export const parseBlocks = (markdown: string): Block[] => { - const lines = markdown.replace(/\r\n?/g, '\n').split('\n') - const blocks: Block[] = [] - let paragraph: string[] = [] - const flush = (): void => { - if (paragraph.length) blocks.push({ type: 'paragraph', inlines: parseInlines(paragraph.join(' ')) }) - paragraph = [] - } - for (let index = 0; index < lines.length; index++) { - const line = lines[index]! - const fence = FENCE.exec(line) - if (fence) { - flush() - const marker = fence[1]! - const body: string[] = [] - for (index++; index < lines.length; index++) { - const trimmed = lines[index]!.trim() - if (trimmed.startsWith(marker) && /^[`~]+$/.test(trimmed)) break - body.push(lines[index]!) - } - blocks.push({ type: 'code', language: fence[2] ?? '', text: body.join('\n') }) - continue - } - if (!line.trim()) { - flush() - continue - } - const heading = HEADING.exec(line) - if (heading) { - flush() - blocks.push({ type: 'heading', level: heading[1]!.length, inlines: parseInlines(heading[2] ?? '') }) - continue - } - if (RULE.test(line)) { - flush() - blocks.push({ type: 'rule' }) - continue - } - if (line.includes('|') && SEPARATOR.test(lines[index + 1] ?? '')) { - flush() - const header = splitRow(line).map((cell) => parseInlines(cell)) - const rows: Inline[][][] = [] - for (index += 2; index < lines.length && lines[index]!.includes('|') && lines[index]!.trim(); index++) rows.push(splitRow(lines[index]!).map((cell) => parseInlines(cell))) - index-- - blocks.push({ type: 'table', header, rows }) - continue - } - const item = ITEM.exec(line) - if (item) { - flush() - blocks.push({ type: 'item', ordered: /\d/.test(item[2]!), number: Number.parseInt(item[2]!, 10) || 1, depth: Math.min(4, Math.floor((item[1] ?? '').length / 2)), inlines: parseInlines(item[3] ?? '') }) - continue - } - const quote = QUOTE.exec(line) - if (quote) { - flush() - const previous = blocks.at(-1) - if (previous?.type === 'quote') previous.inlines.push({ text: ' ' }, ...parseInlines(quote[1] ?? '')) - else blocks.push({ type: 'quote', inlines: parseInlines(quote[1] ?? '') }) - continue - } - const previous = blocks.at(-1) - if (!paragraph.length && previous?.type === 'item' && /^\s{2,}\S/.test(line)) previous.inlines.push({ text: ' ' }, ...parseInlines(line.trim())) - else paragraph.push(line.trim()) - } - flush() - return blocks + const lines = markdown.replace(/\r\n?/g, '\n').split('\n') + const blocks: Block[] = [] + let paragraph: string[] = [] + const flush = (): void => { + if (paragraph.length) blocks.push({ type: 'paragraph', inlines: parseInlines(paragraph.join(' ')) }) + paragraph = [] + } + for (let index = 0; index < lines.length; index++) { + const line = lines[index]! + const fence = FENCE.exec(line) + if (fence) { + flush() + const marker = fence[1]! + const body: string[] = [] + for (index++; index < lines.length; index++) { + const trimmed = lines[index]!.trim() + if (trimmed.startsWith(marker) && /^[`~]+$/.test(trimmed)) break + body.push(lines[index]!) + } + blocks.push({ type: 'code', language: fence[2] ?? '', text: body.join('\n') }) + continue + } + if (!line.trim()) { + flush() + continue + } + const heading = HEADING.exec(line) + if (heading) { + flush() + blocks.push({ type: 'heading', level: heading[1]!.length, inlines: parseInlines(heading[2] ?? '') }) + continue + } + if (RULE.test(line)) { + flush() + blocks.push({ type: 'rule' }) + continue + } + if (line.includes('|') && SEPARATOR.test(lines[index + 1] ?? '')) { + flush() + const header = splitRow(line).map((cell) => parseInlines(cell)) + const rows: Inline[][][] = [] + for (index += 2; index < lines.length && lines[index]!.includes('|') && lines[index]!.trim(); index++) rows.push(splitRow(lines[index]!).map((cell) => parseInlines(cell))) + index-- + blocks.push({ type: 'table', header, rows }) + continue + } + const item = ITEM.exec(line) + if (item) { + flush() + blocks.push({ type: 'item', ordered: /\d/.test(item[2]!), number: Number.parseInt(item[2]!, 10) || 1, depth: Math.min(4, Math.floor((item[1] ?? '').length / 2)), inlines: parseInlines(item[3] ?? '') }) + continue + } + const quote = QUOTE.exec(line) + if (quote) { + flush() + const previous = blocks.at(-1) + if (previous?.type === 'quote') previous.inlines.push({ text: ' ' }, ...parseInlines(quote[1] ?? '')) + else blocks.push({ type: 'quote', inlines: parseInlines(quote[1] ?? '') }) + continue + } + const previous = blocks.at(-1) + if (!paragraph.length && previous?.type === 'item' && /^\s{2,}\S/.test(line)) previous.inlines.push({ text: ' ' }, ...parseInlines(line.trim())) + else paragraph.push(line.trim()) + } + flush() + return blocks } const BLOCK_START = /^(#{1,6}\s|---+\s*$|\s*(?:[-*+]|\d+[.)])\s|\s*\|)/ export const formatMarkdown = (markdown: string, meta: { title: string; url: string; source: string }): string => { - const cleaned: string[] = [] - let fenced = false - for (const raw of markdown.replace(/\r\n?/g, '\n').split('\n')) { - if (/^\s*(```|~~~)/.test(raw)) { - fenced = !fenced - cleaned.push(raw.trimEnd()) - continue - } - if (fenced) { - cleaned.push(raw) - continue - } - cleaned.push(raw.replace(/]*>\s*⁂\s*<\/div>/g, '---').replace(/]*>/gi, '').replace(/<\/?(?:div|span|br|p|center)[^>]*>/gi, '').trimEnd()) - } - const spaced: string[] = [] - fenced = false - for (const line of cleaned) { - const fence = /^\s*(```|~~~)/.test(line) - const opening = fence && !fenced - if (fence) fenced = !fenced - const previous = spaced.at(-1) - const needsBreak = !fenced && !fence && previous !== undefined && previous.trim() !== '' && line.trim() !== '' && BLOCK_START.test(line) && !BLOCK_START.test(previous) - if ((opening || needsBreak) && previous !== undefined && previous.trim() !== '') spaced.push('') - if (fenced || fence || line.trim() !== '' || spaced.at(-1)?.trim() !== '') spaced.push(line) - } - const body = spaced.join('\n').replace(/\n{3,}/g, '\n\n').trim() - const stamp = `_Exported from ${meta.source} on ${new Date().toLocaleString()} · ${meta.url}_` - const firstLine = body.split('\n', 1)[0] ?? '' - if (firstLine.startsWith('# ')) return `${firstLine}\n\n${stamp}\n\n${body.slice(firstLine.length).trim()}\n` - return `# ${meta.title}\n\n${stamp}\n\n---\n\n${body}\n` -} \ No newline at end of file + const cleaned: string[] = [] + let fenced = false + for (const raw of markdown.replace(/\r\n?/g, '\n').split('\n')) { + if (/^\s*(```|~~~)/.test(raw)) { + fenced = !fenced + cleaned.push(raw.trimEnd()) + continue + } + if (fenced) { + cleaned.push(raw) + continue + } + cleaned.push(raw.replace(/]*>\s*⁂\s*<\/div>/g, '---').replace(/]*>/gi, '').replace(/<\/?(?:div|span|br|p|center)[^>]*>/gi, '').trimEnd()) + } + const spaced: string[] = [] + fenced = false + for (const line of cleaned) { + const fence = /^\s*(```|~~~)/.test(line) + const opening = fence && !fenced + if (fence) fenced = !fenced + const previous = spaced.at(-1) + const needsBreak = !fenced && !fence && previous !== undefined && previous.trim() !== '' && line.trim() !== '' && BLOCK_START.test(line) && !BLOCK_START.test(previous) + if ((opening || needsBreak) && previous !== undefined && previous.trim() !== '') spaced.push('') + if (fenced || fence || line.trim() !== '' || spaced.at(-1)?.trim() !== '') spaced.push(line) + } + const body = spaced.join('\n').replace(/\n{3,}/g, '\n\n').trim() + const stamp = `_Exported from ${meta.source} on ${new Date().toLocaleString()} · ${meta.url}_` + const firstLine = body.split('\n', 1)[0] ?? '' + if (firstLine.startsWith('# ')) return `${firstLine}\n\n${stamp}\n\n${body.slice(firstLine.length).trim()}\n` + return `# ${meta.title}\n\n${stamp}\n\n---\n\n${body}\n` +} diff --git a/core/ai/src/placeholders.ts b/core/ai/src/placeholders.ts index 800ad5b..d8c77b0 100644 --- a/core/ai/src/placeholders.ts +++ b/core/ai/src/placeholders.ts @@ -5,7 +5,12 @@ export interface Field { value?: string } -const TOKEN = /\{\{\s*([^{}]+?)\s*\}\}/g +interface Token { + readonly start: number + readonly end: number + readonly key: string +} + const LONG_HINT = /text|body|content|list|items?|description|code|paste|notes|context|article/i const labelOf = (key: string): string => { @@ -13,14 +18,53 @@ const labelOf = (key: string): string => { return raw.charAt(0).toUpperCase() + raw.slice(1) } +const scanTokens = (text: string, visit: (token: Token) => void): void => { + let start = -1 + + for (let index = 0; index < text.length; index++) { + const character = text[index] + + if (character === '{') { + start = index > 0 && text[index - 1] === '{' ? index - 1 : -1 + continue + } + + if (character !== '}') continue + + if (start >= 0 && text[index + 1] === '}') { + const key = text.slice(start + 2, index).trim() + if (key) visit({ start, end: index + 2, key }) + start = -1 + index++ + } else { + start = -1 + } + } +} + export const findFields = (text: string, isResolved: (key: string) => boolean): Field[] => { const fields: Field[] = [] - for (const match of text.matchAll(TOKEN)) { - const key = match[1] ?? '' - if (!key || isResolved(key) || fields.some((field) => field.key === key)) continue - fields.push({ key, label: labelOf(key), long: LONG_HINT.test(key) }) - } + const seen = new Set() + + scanTokens(text, ({ key }) => { + if (seen.has(key)) return + seen.add(key) + if (!isResolved(key)) fields.push({ key, label: labelOf(key), long: LONG_HINT.test(key) }) + }) + return fields } -export const applyValues = (text: string, values: Readonly>): string => text.replace(TOKEN, (match, key: string) => Object.prototype.hasOwnProperty.call(values, key) ? values[key] ?? '' : match) +export const applyValues = (text: string, values: Readonly>): string => { + const parts: string[] = [] + let cursor = 0 + + scanTokens(text, ({ start, end, key }) => { + parts.push(text.slice(cursor, start)) + parts.push(Object.prototype.hasOwnProperty.call(values, key) ? values[key] ?? '' : text.slice(start, end)) + cursor = end + }) + + parts.push(text.slice(cursor)) + return parts.join('') +}