Skip to content

Commit

Permalink
Rename function and variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on committed Oct 23, 2024
1 parent 696bb8d commit 02743ea
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/components/chat-view/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const getNewInputMessage = (app: App): ChatUserMessage => {
return {
role: 'user',
content: null,
parsedContent: null,
promptContent: null,
id: uuidv4(),
mentionables: [
{
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useChatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const serializeChatMessage = (message: ChatMessage): SerializedChatMessage => {
return {
role: 'user',
content: message.content,
parsedContent: message.parsedContent,
promptContent: message.promptContent,
id: message.id,
mentionables: message.mentionables.map(serializeMentionable),
}
Expand All @@ -45,7 +45,7 @@ const deserializeChatMessage = (
return {
role: 'user',
content: message.content,
parsedContent: message.parsedContent,
promptContent: message.promptContent,
id: message.id,
mentionables: message.mentionables
.map((m) => deserializeMentionable(m, app))
Expand Down
4 changes: 2 additions & 2 deletions src/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Mentionable, SerializedMentionable } from './mentionable'
export type ChatUserMessage = {
role: 'user'
content: SerializedEditorState | null
parsedContent: string | null
promptContent: string | null
id: string
mentionables: Mentionable[]
}
Expand All @@ -19,7 +19,7 @@ export type ChatMessage = ChatUserMessage | ChatAssistantMessage
export type SerializedChatUserMessage = {
role: 'user'
content: SerializedEditorState | null
parsedContent: string | null
promptContent: string | null
id: string
mentionables: SerializedMentionable[]
}
Expand Down
32 changes: 18 additions & 14 deletions src/utils/promptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,31 @@ export class PromptGenerator {
throw new Error('Last message is not a user message')
}

const { parsedContent, shouldUseRAG } = await this.parseUserMessage({
message: lastUserMessage,
useVaultSearch,
onQueryProgressChange: onQueryProgressChange,
})
const { promptContent, shouldUseRAG } = await this.compileUserMessagePrompt(
{
message: lastUserMessage,
useVaultSearch,
onQueryProgressChange: onQueryProgressChange,
},
)
let parsedMessages = [
...messages.slice(0, -1),
{
...lastUserMessage,
parsedContent: parsedContent,
promptContent: promptContent,
},
]

// Safeguard: ensure all user messages have parsed content
parsedMessages = await Promise.all(
parsedMessages.map(async (message) => {
if (message.role === 'user' && !message.parsedContent) {
const { parsedContent } = await this.parseUserMessage({ message })
if (message.role === 'user' && !message.promptContent) {
const { promptContent } = await this.compileUserMessagePrompt({
message,
})
return {
...message,
parsedContent: parsedContent,
promptContent: promptContent,
}
}
return message
Expand All @@ -94,7 +98,7 @@ export class PromptGenerator {
if (message.role === 'user') {
return {
role: 'user',
content: message.parsedContent ?? '',
content: message.promptContent ?? '',
}
} else {
return {
Expand All @@ -112,7 +116,7 @@ export class PromptGenerator {
}
}

private async parseUserMessage({
private async compileUserMessagePrompt({
message,
useVaultSearch,
onQueryProgressChange,
Expand All @@ -121,15 +125,15 @@ export class PromptGenerator {
useVaultSearch?: boolean
onQueryProgressChange?: (queryProgress: QueryProgressState) => void
}): Promise<{
parsedContent: string
promptContent: string
shouldUseRAG: boolean
}> {
if (!this.ragEngine) {
throw new Error('RAG engine not initialized')
}
if (!message.content) {
return {
parsedContent: '',
promptContent: '',
shouldUseRAG: false,
}
}
Expand Down Expand Up @@ -214,7 +218,7 @@ ${results
.join('')

return {
parsedContent: `${filePrompt}${blockPrompt}\n\n${query}\n\n`,
promptContent: `${filePrompt}${blockPrompt}\n\n${query}\n\n`,
shouldUseRAG,
}
}
Expand Down

0 comments on commit 02743ea

Please sign in to comment.