Skip to content

Commit

Permalink
Handle cancelling browser session
Browse files Browse the repository at this point in the history
  • Loading branch information
saoudrizwan committed Oct 27, 2024
1 parent 78a3666 commit cf2ec27
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/Cline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ export class Cline {
newUserContent.push({
type: "text",
text:
`[TASK RESUMPTION] This task was interrupted ${agoText}. It may or may not be complete, so please reassess the task context. Be aware that the project state may have changed since then. The current working directory is now '${cwd.toPosix()}'. If the task has not been completed, retry the last step before interruption and proceed with completing the task.\n\nNote: If you previously attempted a tool use that the user did not provide a result for, you should assume the tool use was not successful and assess whether you should retry.${
`[TASK RESUMPTION] This task was interrupted ${agoText}. It may or may not be complete, so please reassess the task context. Be aware that the project state may have changed since then. The current working directory is now '${cwd.toPosix()}'. If the task has not been completed, retry the last step before interruption and proceed with completing the task.\n\nNote: If you previously attempted a tool use that the user did not provide a result for, you should assume the tool use was not successful and assess whether you should retry. If the last tool was a browser_action, the browser has been closed and you must launch a new browser if needed.${
wasRecent
? "\n\nIMPORTANT: If the last tool use was a write_to_file that was interrupted, the file was reverted back to its original state before the interrupted edit, and you do NOT need to re-read the file as you already have its up-to-date contents."
: ""
Expand Down
13 changes: 12 additions & 1 deletion webview-ui/src/components/chat/BrowserSessionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ const BrowserSessionRow = memo((props: BrowserSessionRowProps) => {
const [maxActionHeight, setMaxActionHeight] = useState(0)
const [consoleLogsExpanded, setConsoleLogsExpanded] = useState(false)

// const isLastApiReqInterrupted = useMemo(() => {
// // Check if last api_req_started is cancelled
// const lastApiReqStarted = [...messages].reverse().find((m) => m.say === "api_req_started")
// if (lastApiReqStarted?.text != null) {
// const info = JSON.parse(lastApiReqStarted.text)
// return info.cancelReason != null
// }
// const lastApiReqFailed = isLast && lastModifiedMessage?.ask === "api_req_failed"
// return lastApiReqFailed
// }, [messages, lastModifiedMessage, isLast])

const isBrowsing = useMemo(() => {
return isLast && messages.some((m) => m.say === "browser_action_result") // after user approves, browser_action_result with "" is sent to indicate that the session has started
return isLast && messages.some((m) => m.say === "browser_action_result") //&& !isLastApiReqInterrupted // after user approves, browser_action_result with "" is sent to indicate that the session has started
}, [isLast, messages])

// Organize messages into pages with current state and next action
Expand Down
16 changes: 16 additions & 0 deletions webview-ui/src/components/chat/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,22 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
isInBrowserSession = true
currentGroup.push(message)
} else if (isInBrowserSession) {
// end session if api_req_started is cancelled

if (message.say === "api_req_started") {
// get last api_req_started in currentGroup to check if it's cancelled. If it is then this api req is not part of the current browser session
const lastApiReqStarted = [...currentGroup].reverse().find((m) => m.say === "api_req_started")
if (lastApiReqStarted?.text != null) {
const info = JSON.parse(lastApiReqStarted.text)
const isCancelled = info.cancelReason != null
if (isCancelled) {
endBrowserSession()
result.push(message)
return
}
}
}

if (isBrowserSessionMessage(message)) {
currentGroup.push(message)

Expand Down

0 comments on commit cf2ec27

Please sign in to comment.