Handle cancelling browser session

This commit is contained in:
Saoud Rizwan
2024-10-27 17:09:12 -04:00
parent 78a3666f68
commit cf2ec27a44
3 changed files with 29 additions and 2 deletions

View File

@@ -614,7 +614,7 @@ export class Cline {
newUserContent.push({ newUserContent.push({
type: "text", type: "text",
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 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." ? "\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."
: "" : ""

View File

@@ -37,8 +37,19 @@ const BrowserSessionRow = memo((props: BrowserSessionRowProps) => {
const [maxActionHeight, setMaxActionHeight] = useState(0) const [maxActionHeight, setMaxActionHeight] = useState(0)
const [consoleLogsExpanded, setConsoleLogsExpanded] = useState(false) 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(() => { 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]) }, [isLast, messages])
// Organize messages into pages with current state and next action // Organize messages into pages with current state and next action

View File

@@ -473,6 +473,22 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
isInBrowserSession = true isInBrowserSession = true
currentGroup.push(message) currentGroup.push(message)
} else if (isInBrowserSession) { } 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)) { if (isBrowserSessionMessage(message)) {
currentGroup.push(message) currentGroup.push(message)