Skip to content

Commit 524aa01

Browse files
committed
SimpleChatTC:AutoToolCalls: Track and clear related timers
also cleanup the existing toolResponseTimeout timer to be in the same structure and have similar flow convention.
1 parent a128f83 commit 524aa01

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,29 @@ class MultiChatUI {
691691
/** @type {string} */
692692
this.curChatId = "";
693693

694+
this.TimePeriods = {
695+
ToolCallResponseTimeout: 10000,
696+
ToolCallAutoTimeUnit: 1000
697+
}
698+
699+
this.timers = {
700+
/**
701+
* Used to identify Delay with getting response from a tool call.
702+
* @type {number | undefined}
703+
*/
704+
toolcallResponseTimeout: undefined,
705+
/**
706+
* Used to auto trigger tool call, after a set time, if enabled.
707+
* @type {number | undefined}
708+
*/
709+
toolcallTriggerClick: undefined,
710+
/**
711+
* Used to auto submit tool call response, after a set time, if enabled.
712+
* @type {number | undefined}
713+
*/
714+
toolcallResponseSubmitClick: undefined
715+
}
716+
694717
// the ui elements
695718
this.elInSystem = /** @type{HTMLInputElement} */(document.getElementById("system-in"));
696719
this.elDivChat = /** @type{HTMLDivElement} */(document.getElementById("chat-div"));
@@ -742,9 +765,9 @@ class MultiChatUI {
742765
this.elInToolArgs.value = ar.ns.tool_calls[0].function.arguments
743766
this.elBtnTool.disabled = false
744767
if (gMe.tools.auto > 0) {
745-
setTimeout(()=>{
768+
this.timers.toolcallTriggerClick = setTimeout(()=>{
746769
this.elBtnTool.click()
747-
}, gMe.tools.auto*1000)
770+
}, gMe.tools.auto*this.TimePeriods.ToolCallAutoTimeUnit)
748771
}
749772
} else {
750773
this.elDivTool.hidden = true
@@ -791,6 +814,8 @@ class MultiChatUI {
791814
});
792815

793816
this.elBtnUser.addEventListener("click", (ev)=>{
817+
clearTimeout(this.timers.toolcallResponseSubmitClick)
818+
this.timers.toolcallResponseSubmitClick = undefined
794819
if (this.elInUser.disabled) {
795820
return;
796821
}
@@ -803,20 +828,24 @@ class MultiChatUI {
803828
});
804829

805830
this.elBtnTool.addEventListener("click", (ev)=>{
831+
clearTimeout(this.timers.toolcallTriggerClick)
832+
this.timers.toolcallTriggerClick = undefined
806833
if (this.elDivTool.hidden) {
807834
return;
808835
}
809836
this.handle_tool_run(this.curChatId);
810837
})
811838

839+
// Handle messages from Tools web worker
812840
tools.setup((id, name, data)=>{
813-
clearTimeout(this.idTimeOut)
841+
clearTimeout(this.timers.toolcallResponseTimeout)
842+
this.timers.toolcallResponseTimeout = undefined
814843
this.elInUser.value = ChatMessageEx.createToolCallResultAllInOne(id, name, data);
815844
this.ui_reset_userinput(false)
816845
if (gMe.tools.auto > 0) {
817-
setTimeout(()=>{
846+
this.timers.toolcallResponseSubmitClick = setTimeout(()=>{
818847
this.elBtnUser.click()
819-
}, gMe.tools.auto*1000)
848+
}, gMe.tools.auto*this.TimePeriods.ToolCallAutoTimeUnit)
820849
}
821850
})
822851

@@ -946,10 +975,10 @@ class MultiChatUI {
946975
this.elInUser.value = ChatMessageEx.createToolCallResultAllInOne(toolCallId, toolname, toolResult);
947976
this.ui_reset_userinput(false)
948977
} else {
949-
this.idTimeOut = setTimeout(() => {
978+
this.timers.toolcallResponseTimeout = setTimeout(() => {
950979
this.elInUser.value = ChatMessageEx.createToolCallResultAllInOne(toolCallId, toolname, `Tool/Function call ${toolname} taking too much time, aborting...`);
951980
this.ui_reset_userinput(false)
952-
}, 10000)
981+
}, this.TimePeriods.ToolCallResponseTimeout)
953982
}
954983
}
955984

@@ -1044,6 +1073,10 @@ class Me {
10441073
enabled: false,
10451074
fetchProxyUrl: "http://127.0.0.1:3128",
10461075
toolNames: /** @type {Array<string>} */([]),
1076+
/**
1077+
* Control how many seconds to wait before auto triggering tool call or its response submission.
1078+
* A value of 0 is treated as auto triggering disable.
1079+
*/
10471080
auto: 0
10481081
};
10491082
this.chatProps = {

0 commit comments

Comments
 (0)