Skip to content

Commit

Permalink
Merge branch 'main' into kevinlu1248-patch-8
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlu1248 authored May 11, 2024
2 parents c59ee51 + 91041c1 commit f033bba
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 36 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies = [
"scipy==1.12.0",
"jira==3.8.0",
"slack-sdk==3.27.1",
"jsonpatch==1.33",
]

[tool.isort]
Expand Down
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements.txt
# uv pip compile --output-file requirements.txt pyproject.toml
aiohttp==3.9.3
# via voyageai
aiolimiter==1.1.0
Expand Down Expand Up @@ -218,8 +218,11 @@ jmespath==1.0.1
# botocore
json5==0.9.24
# via jupyterlab-server
jsonpatch==1.33
jsonpointer==2.4
# via jsonschema
# via
# jsonpatch
# jsonschema
jsonschema==4.21.1
# via
# jupyter-events
Expand Down
90 changes: 74 additions & 16 deletions sweep_chat/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { Session } from "next-auth";
import { PostHogProvider, usePostHog } from "posthog-js/react";
import posthog from "posthog-js";
import Survey from "./Survey";
import * as jsonpatch from 'fast-json-patch';


if (typeof window !== 'undefined') {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!)
Expand Down Expand Up @@ -238,6 +240,16 @@ const getLastLine = (content: string) => {
return splitContent[splitContent.length - 1];
}

const getLastLineEndingWithBracket = (content: string) => {
const splitContent = content.trim().split("\n");
for (let i = splitContent.length - 1; i >= 0; i--) {
if (splitContent[i].trim().endsWith("]")) {
return splitContent[i];
}
}
return null;
}

const defaultMessage = `I'm Sweep and I'm here to help you answer questions about your codebase!`;

function App() {
Expand Down Expand Up @@ -299,12 +311,14 @@ function App() {
<Toaster />
{showSurvey && process.env.NEXT_PUBLIC_SURVEY_ID && (
<Survey
onClose={() => {
onClose={(didSubmit) => {
setShowSurvey(false)
toast({
title: "Thanks for your feedback!",
description: "We'll reach back out shortly.",
})
if (didSubmit) {
toast({
title: "Thanks for your feedback!",
description: "We'll reach back out shortly.",
})
}
}}
/>
)}
Expand All @@ -327,6 +341,7 @@ function App() {
</div>
<div className={`w-full flex items-center ${repoNameValid ? "" : "grow"}`}>
<Input
data-ph-capture-attribute-repo-name={repoName}
className="mb-4"
value={repoName}
onChange={(e) => setRepoName(e.target.value)}
Expand Down Expand Up @@ -409,8 +424,15 @@ function App() {
Restart
</Button>
<Input
data-ph-capture-attribute-current-message={currentMessage}
onKeyUp={(e) => {
if (e.key === "Enter") {
posthog.capture("chat submitted", {
repoName,
snippets,
messages,
currentMessage,
});
(async () => {
if (currentMessage !== "") {
const newMessages: Message[] = [...messages, { content: currentMessage, role: "user" }];
Expand All @@ -437,6 +459,13 @@ function App() {
variant: "destructive"
});
setIsLoading(false);
posthog.capture("chat errored", {
repoName,
snippets,
messages,
currentMessage,
error: e.message
});
throw e;
}
}
Expand All @@ -451,30 +480,46 @@ function App() {
repo_name: repoName,
messages: newMessages,
snippets: currentSnippets,
use_patch: true
})
});

// Stream
const reader = chatResponse.body?.getReader();
let done = false;
let chat = "";
let buffer = "";
var streamedMessages: Message[] = []
var respondedMessages: Message[] = [...newMessages, { content: "", role: "assistant" }]
setMessages(respondedMessages);
try {
while (!done) {
const { value, done: done_ } = await reader!.read();
if (value) {
const decodedValue = new TextDecoder().decode(value);
chat += decodedValue;
const lastLine = getLastLine(chat);
if (lastLine !== "") {
try {
const addedMessages = JSON.parse(lastLine);
respondedMessages = [...newMessages, ...addedMessages]
setMessages(respondedMessages);
} catch (e: any) { }
chat = lastLine
buffer += decodedValue;
buffer = buffer.replace("][{", "]\n[{")
var newBuffer = "";
const bufferLines = buffer.trim().split("\n")

for (var i = 0; i < bufferLines.length; i += 1) {
const line = bufferLines[i];
if (line !== "") {
try {
const patch = JSON.parse(line)
streamedMessages = jsonpatch.applyPatch(streamedMessages, patch).newDocument
} catch (e: any) {
if (i == bufferLines.length - 1) {
newBuffer = line
} else {
console.log(e.message)
console.log(buffer)
}
}
}
}
setMessages([...newMessages, ...streamedMessages])

buffer = newBuffer
}
done = done_;
}
Expand All @@ -484,8 +529,15 @@ function App() {
description: e.message,
variant: "destructive"
});
console.log(chat)
console.log(buffer)
setIsLoading(false);
posthog.capture("chat errored", {
repoName,
snippets,
messages,
currentMessage,
error: e.message
});
throw e;
}

Expand All @@ -494,6 +546,12 @@ function App() {
setShowSurvey(true);
}
setIsLoading(false);
posthog.capture("chat succeeded", {
repoName,
snippets,
messages,
currentMessage,
});
}
})()
}
Expand Down
8 changes: 4 additions & 4 deletions sweep_chat/components/Survey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useEffect, useState } from 'react';
export default function Feedback({
onClose
}: {
onClose: () => void
onClose: (didSubmit: boolean) => void
}) {
const posthog = usePostHog();
const [feedback, setFeedback] = useState("")
Expand All @@ -29,7 +29,7 @@ export default function Feedback({
$survey_id: surveyID,
});
localStorage.setItem(`hasInteractedWithSurvey_${surveyID}`, 'true');
onClose();
onClose(false);
}

const handleFeedbackSubmit = (e: any) => {
Expand All @@ -40,11 +40,11 @@ export default function Feedback({
$survey_response: feedback
});
localStorage.setItem(`hasInteractedWithSurvey_${surveyID}`, 'true');
onClose();
onClose(true);
}

return (
<Card className="w-[500px] fixed right-4 bottom-4">
<Card className="w-[500px] fixed right-4 bottom-4 z-10">
<CardHeader>
<CardTitle>Give us Feedback</CardTitle>
<CardDescription>Sweep Chat is new so we&apos;re actively trying to improve it for developers like you.</CardDescription>
Expand Down
12 changes: 9 additions & 3 deletions sweep_chat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sweep_chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@radix-ui/react-toast": "^1.1.5",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"fast-json-patch": "^3.1.1",
"lucide-react": "^0.378.0",
"next": "14.2.3",
"next-auth": "^4.24.7",
Expand Down
29 changes: 21 additions & 8 deletions sweepai/chat/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jsonpatch
from copy import deepcopy
import json
import os
Expand Down Expand Up @@ -130,7 +131,7 @@ def search_codebase_endpoint(
## Updated answer
Determine if you have sufficient information to answer the user's question. If not, determine the information you need to answer the question completely by making `search_codebase` tool calls.
If so, rewrite your previous response with the new information and any invalidated beliefs or assumptions. Make sure this answer is complete and helpful. Provide code examples, explanations and excerpts wherever possible to provide concrete explanations. When suggesting code changes, write out all the code changes required in the unified diff format.
If so, rewrite your previous response with the new information and any invalidated beliefs or assumptions. Make sure this answer is complete and helpful. Provide code examples, explanations and excerpts wherever possible to provide concrete explanations. When explaining how to add new code, always write out the new code. When suggesting code changes, write out all the code changes required in the unified diff format.
</user_response>
# 2. Self-Critique
Expand Down Expand Up @@ -163,7 +164,7 @@ def search_codebase_endpoint(
## Answer
Determine if you have sufficient information to answer the user's question. If not, determine the information you need to answer the question completely by making `search_codebase` tool calls.
If so, write a complete helpful response to the user's question oin detail. Make sure this answer is complete and helpful. Provide code examples, explanations and excerpts wherever possible to provide concrete explanations. When suggesting code changes, write out all the code changes required in the unified diff format.
If so, write a complete helpful response to the user's question in full detail. Make sure this answer is complete and helpful. Provide code examples, explanations and excerpts wherever possible to provide concrete explanations. When explaining how to add new code, always write out the new code. When suggesting code changes, write out all the code changes required in the unified diff format.
</user_response>
# 2. Self-Critique
Expand Down Expand Up @@ -260,6 +261,7 @@ def chat_codebase(
repo_name: str = Body(...),
messages: list[Message] = Body(...),
snippets: list[Snippet] = Body(...),
use_patch: bool = Body(False)
):
if len(messages) == 0:
raise ValueError("At least one message is required.")
Expand Down Expand Up @@ -427,14 +429,25 @@ def stream_state(initial_user_message: str, snippets: list[Snippet], messages: l
break
yield new_messages

def postprocessed_stream(*args, **kwargs):
def postprocessed_stream(*args, use_patch=False, **kwargs):
previous_state = []
for messages in stream_state(*args, **kwargs):
yield json.dumps([
message.model_dump()
for message in messages
]) + "\n"
if not use_patch:
yield json.dumps([
message.model_dump()
for message in messages
]) + "\n"
else:
current_state = [
message.model_dump()
for message in messages
]
patch = jsonpatch.JsonPatch.from_diff(previous_state, current_state)
if patch:
yield patch.to_string() + "\n"
previous_state = current_state

return StreamingResponse(postprocessed_stream(messages[-1].content + "\n\n" + format_message, snippets, messages))
return StreamingResponse(postprocessed_stream(messages[-1].content + "\n\n" + format_message, snippets, messages, use_patch=use_patch))

def handle_function_call(function_call: AnthropicFunctionCall, repo_name: str, snippets: list[Snippet]):
NUM_SNIPPETS = 5
Expand Down
2 changes: 1 addition & 1 deletion sweepai/core/sweep_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def remove_line_numbers(s: str) -> str:

def parse_filenames(text):
# Regular expression pattern to match file names
pattern = r'\b(?:\w+/)*\w+(?:\.\w+)+\b|\b(?:\w+/)+\w+\b'
pattern = r'\b(?:[\w-]+/)*[\w-]+(?:[.:]\w+)+\b|\b(?:[\w-]+/)+[\w-]+\b'

# Find all occurrences of file names in the text
filenames = re.findall(pattern, text)
Expand Down
2 changes: 1 addition & 1 deletion sweepai/handlers/on_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def edit_comment(new_comment: str) -> None:

sweep_bot.comment_pr_diff_str = pr_diff_string
sweep_bot.comment_pr_files_modified = pr_files_modified
modify_files_dict, changes_made, _ = handle_file_change_requests(
modify_files_dict, changes_made, _, file_change_requests = handle_file_change_requests(
file_change_requests=file_change_requests,
request=file_comment,
branch_name=branch_name,
Expand Down
2 changes: 1 addition & 1 deletion sweepai/handlers/on_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def edit_sweep_comment(
logger.warning(f"Validation error: {error_message}")
edit_sweep_comment(
(
f"The issue was rejected with the following response:\n\n{blockquote(error_message)}"
f"The issue was rejected with the following response:\n\n**{error_message}**"
),
-1,
)
Expand Down

0 comments on commit f033bba

Please sign in to comment.