Skip to content

Commit f8b2ec5

Browse files
committed
fix typings
1 parent 8ffee0e commit f8b2ec5

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

src/lib/buildPrompt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { downloadImgFile } from "./server/files/downloadFile";
44
import type { Conversation } from "./types/Conversation";
55
import RAGs from "./server/rag/rag";
66
import type { RagContext } from "./types/rag";
7+
import type { RagContextWebSearch } from "./types/WebSearch";
78

89
export type BuildPromptMessage = Pick<Message, "from" | "content" | "files">;
910

@@ -26,7 +27,7 @@ export async function buildPrompt({
2627
}: buildPromptOptions): Promise<string> {
2728
if (ragContext) {
2829
const { type: ragType } = ragContext;
29-
messages = RAGs[ragType].buildPrompt(messages, ragContext);
30+
messages = RAGs[ragType].buildPrompt(messages, ragContext as RagContextWebSearch);
3031
}
3132

3233
// section to handle potential files input

src/lib/components/chat/ChatMessage.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import type { Model } from "$lib/types/Model";
1818
1919
import OpenRAGResults from "../OpenRAGResults.svelte";
20-
import type { RAGUpdate } from "$lib/types/MessageUpdate";
20+
import type { RAGUpdate, WebSearchUpdate } from "$lib/types/MessageUpdate";
2121
import { ragTypes } from "$lib/types/rag";
2222
2323
function sanitizeMd(md: string) {
@@ -117,8 +117,10 @@
117117
$: downloadLink =
118118
message.from === "user" ? `${$page.url.pathname}/message/${message.id}/prompt` : undefined;
119119
120-
$: webSearchSources =
121-
ragUpdates && ragUpdates?.filter(({ messageType }) => messageType === "sources")?.[0]?.sources;
120+
$: webSearchSources = (
121+
ragUpdates &&
122+
(ragUpdates?.filter(({ messageType }) => messageType === "sources")?.[0] as WebSearchUpdate)
123+
)?.sources;
122124
123125
$: if (isCopied) {
124126
setTimeout(() => {

src/lib/server/endpoints/openai/endpointOai.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { buildPrompt } from "$lib/buildPrompt";
55
import { OPENAI_API_KEY } from "$env/static/private";
66
import type { Endpoint } from "../endpoints";
77
import { format } from "date-fns";
8+
import type { RagContextWebSearch } from "$lib/types/WebSearch";
89

910
export const endpointOAIParametersSchema = z.object({
1011
weight: z.number().int().positive().default(1),
@@ -56,9 +57,10 @@ export async function endpointOai(
5657
} else if (completion === "chat_completions") {
5758
return async ({ conversation }) => {
5859
let messages = conversation.messages;
59-
const webSearch = conversation.messages[conversation.messages.length - 1].webSearch;
60+
const ragContext = conversation.messages[conversation.messages.length - 1].ragContext;
6061

61-
if (webSearch && webSearch.context) {
62+
if (ragContext && ragContext.type === "webSearch") {
63+
const webSearchContext = ragContext as RagContextWebSearch;
6264
const lastMsg = messages.slice(-1)[0];
6365
const messagesWithoutLastUsrMsg = messages.slice(0, -1);
6466
const previousUserMessages = messages.filter((el) => el.from === "user").slice(0, -1);
@@ -74,9 +76,9 @@ export async function endpointOai(
7476
...messagesWithoutLastUsrMsg,
7577
{
7678
from: "user",
77-
content: `I searched the web using the query: ${webSearch.searchQuery}. Today is ${currentDate} and here are the results:
79+
content: `I searched the web using the query: ${webSearchContext.searchQuery}. Today is ${currentDate} and here are the results:
7880
=====================
79-
${webSearch.context}
81+
${webSearchContext.context}
8082
=====================
8183
${previousQuestions}
8284
Answer the question: ${lastMsg.content}

src/lib/types/MessageUpdate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface RAGUpdate {
2626
}
2727

2828
export interface WebSearchUpdate extends RAGUpdate {
29-
type: "websearch";
29+
type: "webSearch";
3030
messageType: RAGUpdate["messageType"] | "sources";
3131
sources?: WebSearchSource[];
3232
}
@@ -51,6 +51,7 @@ export type MessageUpdate =
5151
| FinalAnswer
5252
| TextStreamUpdate
5353
| AgentUpdate
54-
| RAGUpdate
54+
| WebSearchUpdate
55+
| PdfSearchUpdate
5556
| StatusUpdate
5657
| ErrorUpdate;

src/routes/conversation/[id]/+page.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import type { MessageUpdate, RAGUpdate } from "$lib/types/MessageUpdate";
1717
import titleUpdate from "$lib/stores/titleUpdate";
1818
import file2base64 from "$lib/utils/file2base64";
19-
import { PdfUploadStatus, type PdfUpload } from "$lib/types/PdfChat.js";
20-
import { ragTypes } from "$lib/types/rag.js";
19+
import { PdfUploadStatus, type PdfUpload } from "$lib/types/PdfChat";
2120
export let data;
2221
2322
let messages = data.messages;
@@ -197,7 +196,7 @@
197196
lastMessage.content += update.token;
198197
messages = [...messages];
199198
}
200-
} else if (ragTypes.includes(update.type)) {
199+
} else if (update.type === "webSearch" || update.type === "pdfChat") {
201200
RAGMessages = [...RAGMessages, update];
202201
} else if (update.type === "status") {
203202
if (update.status === "title" && update.message) {

src/routes/conversation/[id]/+server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { summarize } from "$lib/server/summarize";
1414
import { uploadImgFile } from "$lib/server/files/uploadFile";
1515
import sizeof from "image-size";
1616
import RAGs from "$lib/server/rag/rag";
17+
import type { RagContext } from "$lib/types/rag";
1718

1819
export async function POST({ request, locals, params, getClientAddress }) {
1920
const id = z.string().parse(params.id);
@@ -235,12 +236,16 @@ export async function POST({ request, locals, params, getClientAddress }) {
235236
let webSearchResults: RagContextWebSearch | undefined;
236237

237238
if (webSearch) {
238-
webSearchResults = await RAGs["webSearch"].retrieveRagContext(conv, newPrompt, update);
239+
webSearchResults = (await RAGs["webSearch"].retrieveRagContext(
240+
conv,
241+
newPrompt,
242+
update
243+
)) as RagContextWebSearch;
239244
}
240245

241246
messages[messages.length - 1].ragContext = webSearchResults;
242247

243-
let pdfSearchResults: PdfSearch | undefined;
248+
let pdfSearchResults: RagContext | undefined;
244249
const pdfSearch = await collections.files.findOne({ filename: `${convId.toString()}-pdf` });
245250
if (pdfSearch) {
246251
pdfSearchResults = await RAGs["pdfChat"].retrieveRagContext(conv, newPrompt, update);
@@ -274,7 +279,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
274279
{
275280
from: "assistant",
276281
content: output.token.text.trimStart(),
277-
webSearch: webSearchResults,
282+
ragContext: webSearchResults,
278283
updates: updates,
279284
id: (responseId as Message["id"]) || crypto.randomUUID(),
280285
createdAt: new Date(),

0 commit comments

Comments
 (0)