Skip to content

Commit a422f90

Browse files
author
steven
committed
1. /stop command created.
1 parent 7abd4d6 commit a422f90

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

ailice/common/AExceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class AExceptionStop(Exception):
2+
pass

ailice/core/AInterpreter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ast
55
import traceback
66
from typing import Any
7+
from ailice.common.AExceptions import AExceptionStop
78
from ailice.common.ADataType import typeInfo, ToJson, FromJson
89
from ailice.prompts.ARegex import GenerateRE4FunctionCalling, GenerateRE4ObjectExpr, ARegexMap, VAR_DEF, EXPR_OBJ
910

@@ -142,6 +143,8 @@ def EvalEntries(self, txt: str) -> str:
142143
resp += f"EXCEPTION: {str(e)}\n{traceback.format_exc()}\n"
143144
if "unterminated string literal" in str(e):
144145
resp += "Please check if there are any issues with your string syntax. For instance, are you using a newline within a single-quoted string? Or should you use triple quotes to avoid error-prone escape sequences?"
146+
except AExceptionStop as e:
147+
raise e
145148
except Exception as e:
146149
resp += f"EXCEPTION: {str(e)}\n{e.tb if hasattr(e, 'tb') else traceback.format_exc()}"
147150
return resp

ailice/core/AProcessor.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import random
99
import json
1010
from ailice.common.AConfig import config
11+
from ailice.common.AExceptions import AExceptionStop
1112
from ailice.common.utils.ALogger import ALoggerSection, ALoggerMsg
1213
from ailice.core.AConversation import AConversations
1314
from ailice.core.AInterpreter import AInterpreter
@@ -133,20 +134,30 @@ def __call__(self, txt: str) -> str:
133134
self.SaveMsg(role="ASSISTANT", msg=ret, storeMsg=ret)
134135
self.result = ret
135136

136-
msg = self.messenger.GetPreviousMsg()
137-
if msg != None:
138-
resp = f"Interruption. Reminder from super user: {msg}"
139-
self.SaveMsg(role="SYSTEM", msg=resp, storeMsg=resp, loggerMsg=resp, logger=loggerSection)
140-
continue
141-
142-
resp = self.interpreter.EvalEntries(ret)
143-
144-
if "" != resp:
145-
self.interpreter.EvalVar(varName="returned_content_in_last_function_call", content=resp)
146-
m = "This is a system-generated message. Since the function call in your previous message has returned information, the response to this message will be handled by the backend system instead of the user. Meanwhile, your previous message has been marked as private and has not been sent to the user. Function returned: {" + resp + "}\n\nThe returned text has been automatically saved to variable 'returned_content_in_last_function_call' for quick reference."
147-
self.SaveMsg(role="SYSTEM", msg=m, storeMsg="Function returned: {" + resp + "}", logMsg=resp, logger=loggerSection)
148-
else:
149-
return self.result
137+
try:
138+
msg = self.messenger.GetPreviousMsg()
139+
if (str == type(msg)) and ("/stop" == msg.strip()):
140+
raise AExceptionStop()
141+
elif msg != None:
142+
resp = f"Interruption. Reminder from super user: {msg}"
143+
self.SaveMsg(role="SYSTEM", msg=resp, storeMsg=resp, loggerMsg=resp, logger=loggerSection)
144+
continue
145+
146+
resp = self.interpreter.EvalEntries(ret)
147+
148+
if "" != resp:
149+
self.interpreter.EvalVar(varName="returned_content_in_last_function_call", content=resp)
150+
m = "This is a system-generated message. Since the function call in your previous message has returned information, the response to this message will be handled by the backend system instead of the user. Meanwhile, your previous message has been marked as private and has not been sent to the user. Function returned: {" + resp + "}\n\nThe returned text has been automatically saved to variable 'returned_content_in_last_function_call' for quick reference."
151+
self.SaveMsg(role="SYSTEM", msg=m, storeMsg="Function returned: {" + resp + "}", logMsg=resp, logger=loggerSection)
152+
else:
153+
return self.result
154+
except AExceptionStop as e:
155+
resp = "Interruption. The task was terminated by the superuser."
156+
self.SaveMsg(role="SYSTEM", msg=resp, storeMsg=resp, logMsg=resp, logger=loggerSection)
157+
158+
resp = "I will stop here due to the superuser's request to terminate the task."
159+
self.SaveMsg(role="ASSISTANT", msg=resp, storeMsg=resp, logMsg=resp, logger=loggerSection)
160+
raise e
150161

151162
def EvalCall(self, agentType: str, agentName: str, msg: str) -> str:
152163
if agentType not in self.promptsManager:

ailice/ui/templates/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,12 @@
926926

927927
function interrupt() {
928928
isInterrupted = true;
929-
document.getElementById('text-input').disabled = false;
929+
const textInput = document.getElementById('text-input');
930+
textInput.disabled = false;
930931
document.getElementById('interrupt-button').textContent = 'Send';
932+
933+
textInput.placeholder = "Send a message to the currently active agent. You can also send the command \"/stop\" to terminate the current task.";
934+
931935
fetch('/interrupt', {
932936
method: 'POST'
933937
}).catch(error => {
@@ -954,6 +958,7 @@
954958
document.getElementById('text-input').disabled = true;
955959
}
956960
document.getElementById('interrupt-button').textContent = '⏹️';
961+
document.getElementById('text-input').placeholder = "Type a message... (Markdown/LaTeX/code highlighting Supported. Shift+Enter for new line)";
957962
}).catch(error => {
958963
console.error('Error:', error);
959964
});

0 commit comments

Comments
 (0)