Skip to content

Commit

Permalink
jim will fix it for you, and you and you and bababa! please no...
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Sep 19, 2024
1 parent f73c2f5 commit 771e94a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
59 changes: 39 additions & 20 deletions src/extension/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,21 +560,23 @@ export class ChatService {

private async loadFileContents(files: FileItem[]): Promise<string> {
if (!files?.length) return ''
let fileContents = '';
let fileContents = ''

for (const file of files) {
try {
const content = await fs.readFile(file.path, 'utf-8');
fileContents += `File: ${file.name}\n\n${content}\n\n`;
const content = await fs.readFile(file.path, 'utf-8')
fileContents += `File: ${file.name}\n\n${content}\n\n`
} catch (error) {
console.error(`Error reading file ${file.path}:`, error);
console.error(`Error reading file ${file.path}:`, error)
}
}
return fileContents.trim();
return fileContents.trim()
}


public async streamChatCompletion(messages: Message[], filePaths: FileItem[]) {
public async streamChatCompletion(
messages: Message[],
filePaths: FileItem[]
) {
this._completion = ''
this.sendEditorLanguage()
const editor = window.activeTextEditor
Expand Down Expand Up @@ -604,27 +606,37 @@ export class ChatService {
additionalContext += `Additional Context:\n${ragContext}\n\n`
}

const fileContents = await this.loadFileContents(filePaths);
const fileContents = await this.loadFileContents(filePaths)
if (fileContents) {
additionalContext += `File Contents:\n${fileContents}\n\n`;
additionalContext += `File Contents:\n${fileContents}\n\n`
}

const updatedMessages = [systemMessage, ...messages.slice(0, -1)]
const provider = this.getProvider()

if (!provider) return

const conversation = []

conversation.push(...messages.slice(0, -1))

if (!provider.modelName.includes('claude')) {
conversation.unshift(systemMessage)
}

if (additionalContext) {
const lastMessageContent = `${cleanedText}\n\n${additionalContext.trim()}`
updatedMessages.push({
conversation.push({
role: USER,
content: lastMessageContent
})
} else {
updatedMessages.push({
conversation.push({
...lastMessage,
content: cleanedText
})
}
updateLoadingMessage(this._webView, 'Thinking')
const request = this.buildStreamRequest(updatedMessages)
const request = this.buildStreamRequest(conversation)
if (!request) return
const { requestBody, requestOptions } = request
return this.streamResponse({ requestBody, requestOptions })
Expand Down Expand Up @@ -678,13 +690,20 @@ export class ChatService {
? `${prompt}\n\nAdditional Context:\n${ragContext}`
: prompt

const conversation: Message[] = [
systemMessage,
{
role: USER,
content: userContent
}
]
const provider = this.getProvider()

if (!provider) return []

const conversation = []

conversation.push({
role: USER,
content: userContent
})

if (!provider.modelName.includes('claude')) {
conversation.push(systemMessage)
}

return conversation
}
Expand Down
17 changes: 13 additions & 4 deletions src/extension/conversation-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {
EXTENSION_SESSION_NAME,
SYMMETRY_EMITTER_KEY,
SYMMETRY_DATA_MESSAGE,
TITLE_GENERATION_PROMPT_MESAGE
TITLE_GENERATION_PROMPT_MESAGE,
USER,
ASSISTANT
} from '../common/constants'
import { SessionManager } from './session-manager'
import { SymmetryService } from './symmetry-service'
Expand Down Expand Up @@ -157,14 +159,21 @@ export class ConversationHistory {

const requestOptions = this.getRequestOptions(provider)

if (messages.length === 1 && messages[0].role === ASSISTANT) {
messages.unshift({
role: USER,
content: 'Request to review code.'
})
}

const requestBody = createStreamRequestBody(provider.provider, {
model: provider.modelName,
numPredictChat: this.config.numPredictChat,
temperature: this.temperature,
messages: [
...messages,
{
role: 'user',
role: USER,
content: TITLE_GENERATION_PROMPT_MESAGE
}
],
Expand Down Expand Up @@ -236,7 +245,7 @@ export class ConversationHistory {
conversation
)
this.webView?.postMessage({
type: CONVERSATION_EVENT_NAME.getActiveConversation,
type: CONVERSATION_EVENT_NAME.setActiveConversation,
value: {
data: conversation
}
Expand Down Expand Up @@ -287,7 +296,7 @@ export class ConversationHistory {
messages: [
...conversation.messages,
{
role: 'user',
role: USER,
content: TITLE_GENERATION_PROMPT_MESAGE
}
],
Expand Down
8 changes: 2 additions & 6 deletions src/extension/review-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EVENT_NAME,
EXTENSION_CONTEXT_NAME,
GITHUB_EVENT_NAME,
USER,
WEBUI_TABS
} from '../common/constants'
import { StreamResponse } from 'symmetry-core'
Expand Down Expand Up @@ -134,7 +135,7 @@ export class GithubService extends ConversationHistory {

const messages = [
{
role: 'user',
role: USER,
content: prompt
}
]
Expand Down Expand Up @@ -181,11 +182,6 @@ export class GithubService extends ConversationHistory {
return streamResponse({
body: requestBody,
options: requestOptions,
onStart: (controller: AbortController) => {
this.webView?.onDidReceiveMessage(() => {
controller?.abort()
})
},
onData: (streamResponse) => {
const provider = this.getProvider()
if (!provider) return
Expand Down
3 changes: 3 additions & 0 deletions src/webview/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ export const useConversationHistory = () => {
if (message?.type === CONVERSATION_EVENT_NAME.getConversations) {
setConversations(message.value.data)
}
if (message?.type === CONVERSATION_EVENT_NAME.setActiveConversation) {
setConversation(message.value.data)
}
}
}

Expand Down

0 comments on commit 771e94a

Please sign in to comment.