From 8264c4b63edb4fb1b832b265bbfb0aecc8412eb7 Mon Sep 17 00:00:00 2001 From: Federico Berto Date: Sat, 21 Jan 2023 19:24:14 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20dynamic=20reference=20links;?= =?UTF-8?q?=20update=20to=20latest=20YouChat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background/index.ts | 68 ++++++++++++++++++++++------- src/content-script/ChatGPTQuery.tsx | 13 ++++++ 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index 8c2a7eb..c36aa1b 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,5 +1,6 @@ +import { string } from "prop-types"; import Browser from "webextension-polyfill"; -import { Answer } from "../messaging.js"; +import { Answer, Link} from "../messaging.js"; import { sendMessageFeedback, setConversationProperty } from "./chatgpt.js"; import { fetchSSE } from "./fetch-sse.js"; @@ -20,37 +21,72 @@ async function generateAnswers(port: Browser.Runtime.Port, question: string) { let messages = []; await fetchSSE( - "https://you.com/api/youchatStreaming?question=" + question + "&chat=[]", + // "https://you.com/api/youchatStreaming?question=" + question + "&chat=[]", + "https://you.com/api/streamingSearch?q=" + question + "&domain=youchat", + { method: "GET", signal: controller.signal, - // NOTE: possibly this boy is not the best, better look for "done" or "end" or something - onMessage(message: string) { + // We parse the full event here. + // NOTE: [Future work] We could even parse the full internal search results! + onMessage(message: any) { + console.debug("sse message", message); - if (message === "I'm Mr. Meeseeks. Look at me.") { - port.postMessage({ event: "DONE" }); - deleteConversation(); - return; - } messages.push(message); let text = String(""); + let serpResults = []; + let urlCount = 1; + let links = []; for (const message of messages) { - const obj = JSON.parse(message); - const token = obj.token; + + const obj = JSON.parse(message.data); - //if undefined, remove - if (token === undefined) { - continue; + let new_text = String(""); + + // add to text key youChatToken and its value if it exists + if (obj.hasOwnProperty('youChatToken')) { + new_text = obj.youChatToken; } - text += token; - } + // Internal results + if (obj.hasOwnProperty('youChatSerpResults')) { + serpResults = obj.youChatSerpResults; + } + + // if serpResults is not empty, detect if the text contains a url. if so, get the urlCount index + // and replace it with the name of the url + // for example: [8][https//google.com] -> [1][https//google.com] + if (serpResults.length > 0) { + for (const result of serpResults) { + if (new_text.includes(result.url)) { + + let linkIndex = 0; + if (!links.some((link) => link.url === result.url)) { + links.push({index: urlCount, name: result.name, url: result.url} as Link); + linkIndex = urlCount; + urlCount++; + } + else { + linkIndex = links.find((link) => link.url === result.url).index; + } + text = text.replace(result.url, `[[${linkIndex}]](${result.url})`); + new_text = `[[${linkIndex}]](${result.url})`; + } + } + } + text += new_text; + } + if (text) { port.postMessage({ text, + links: links, + status: 'done', + messageId: 'dummy', + conversationId: 'dummy', } as Answer); } }, diff --git a/src/content-script/ChatGPTQuery.tsx b/src/content-script/ChatGPTQuery.tsx index e625232..119e006 100644 --- a/src/content-script/ChatGPTQuery.tsx +++ b/src/content-script/ChatGPTQuery.tsx @@ -86,6 +86,19 @@ function ChatGPTQuery(props: Props) { > {answer.text} + {answer.links && + answer.links.map((link) => ( +
openInNewTab(link.url)}> +
+ + [{link.index}] {link.name} + +
+
+ {link.url} +
+
+ ))} ); }