Browse: get page title too

This commit is contained in:
Enrico Ros
2024-06-24 18:44:34 -07:00
parent 82ecfdbd37
commit 7fd359852a
2 changed files with 15 additions and 3 deletions
@@ -125,9 +125,9 @@ export async function attachmentLoadInputAsync(source: Readonly<AttachmentDraftS
try {
const page = await callBrowseFetchPage(source.url);
edit(
page.content.markdown ? { input: { mimeType: 'text/markdown', data: page.content.markdown, dataSize: page.content.markdown.length } }
: page.content.text ? { input: { mimeType: 'text/plain', data: page.content.text, dataSize: page.content.text.length } }
: page.content.html ? { input: { mimeType: 'text/html', data: page.content.html, dataSize: page.content.html.length } }
page.content.markdown ? { input: { mimeType: 'text/markdown', data: page.content.markdown, dataSize: page.content.markdown.length, altData: page.title } }
: page.content.text ? { input: { mimeType: 'text/plain', data: page.content.text, dataSize: page.content.text.length, altData: page.title } }
: page.content.html ? { input: { mimeType: 'text/html', data: page.content.html, dataSize: page.content.html.length, altData: page.title } }
: { inputError: 'No content found at this link' },
);
} catch (error: any) {
+12
View File
@@ -42,6 +42,7 @@ const fetchPageInputSchema = z.object({
const fetchPageWorkerOutputSchema = z.object({
url: z.string(),
title: z.string(),
content: z.record(pageTransformSchema, z.string()),
error: z.string().optional(),
stopReason: z.enum(['end', 'timeout', 'error']),
@@ -77,6 +78,7 @@ export const browseRouter = createTRPCRouter({
? result.value
: {
url: requests[index].url,
title: '',
content: {},
error: result.reason?.message || 'Unknown fetch error',
stopReason: 'error',
@@ -106,6 +108,7 @@ async function workerPuppeteer(
const result: FetchPageWorkerOutputSchema = {
url: targetUrl,
title: '',
content: {},
error: undefined,
stopReason: 'error',
@@ -142,6 +145,15 @@ async function workerPuppeteer(
}
}
// Get the page title after successful navigation
if (result.stopReason !== 'error') {
try {
result.title = await page.title();
} catch (error: any) {
// result.error = '[Puppeteer] ' + (error?.message || error?.toString() || 'Unknown title error');
}
}
// transform the content of the page as text
try {
if (result.stopReason !== 'error') {