Skip to content

Commit 06d2864

Browse files
committed
check for ongoing task first
1 parent 625e478 commit 06d2864

1 file changed

Lines changed: 58 additions & 52 deletions

File tree

src/bro/conversation.py

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,12 @@ def _process_response_output(self, output: Any) -> None:
232232
_logger.error(f"Message can't be parsed. Received data: {msg_data}")
233233
# TODO rerunning inference using Tenacity
234234

235-
# If we had function calls, call model again to process the results
236235
if had_function_calls:
237236
_logger.info("Function calls were processed, requesting follow-up inference...")
238237
conversation_response = self._request_inference(self._context)
239238
follow_up_output = conversation_response["output"]
240239
if follow_up_output:
241-
self._process_response_output(follow_up_output) # Recursive call
240+
self._process_response_output(follow_up_output)
242241

243242
def _on_task_completed_cb(self, message: str) -> None:
244243
_logger.warning("🏁 " * 40 + "\n" + message)
@@ -258,6 +257,8 @@ def _on_task_completed_cb(self, message: str) -> None:
258257
"content": input_data,
259258
}
260259
]
260+
261+
self._current_task = None
261262
_logger.info("Requesting conversation response after receiving reasoner response...")
262263
conversation_response = self._request_inference(self._context)
263264
output = conversation_response["output"]
@@ -280,57 +281,62 @@ def _process(self, item: dict[str, Any]) -> str | None:
280281
args = json.loads(arguments)
281282
_logger.debug(f"Received function call arguments: {args}")
282283
result = None
283-
match name, args:
284-
case ("task_reasoner", {"prompt": prompt, "channel": channel}):
285-
# TODO: add a way to interrupt the current task.
286-
_logger.info("Tasking the reasoner...")
287-
_logger.debug(f"Prompt for the reasoner: {prompt}")
288-
if self._reasoner.task(Context(prompt=prompt, files=[])):
289-
self._current_task = Task(summary=prompt, channel=Channel(name=channel))
290-
result = "Successfully tasked the reasoner."
291-
case ("get_reasoner_status", {}):
292-
_logger.info("Calling legilimens for task progress...")
293-
result = self._reasoner.legilimens()
294-
if not self._current_task:
295-
_logger.error(
296-
f"Missing current task context. Cannot route message to any channel. Message "
297-
f"content: {result}"
298-
)
299-
else:
300-
self._msgs.append(
301-
ReceivedMessage(
302-
via=self._current_task.channel,
303-
user=User(name="Bro"),
304-
text=f"Send message to the user: {result}",
305-
attachments=[],
284+
285+
if self._current_task and name != "get_reasoner_status":
286+
result = f"Cannot execute {name}. Another task is ongoing. Please try again later."
287+
else:
288+
match name, args:
289+
case ("task_reasoner", {"prompt": prompt, "channel": channel}):
290+
_logger.info("Tasking the reasoner...")
291+
_logger.debug(f"Prompt for the reasoner: {prompt}")
292+
if self._reasoner.task(Context(prompt=prompt, files=[])):
293+
self._current_task = Task(summary=prompt, channel=Channel(name=channel))
294+
result = "Successfully tasked the reasoner."
295+
else:
296+
result = "Failed to task the reasoner."
297+
case ("get_reasoner_status", {}):
298+
_logger.info("Calling legilimens for task progress...")
299+
result = self._reasoner.legilimens()
300+
if not self._current_task:
301+
_logger.error(
302+
f"Missing current task context. Cannot route message to any channel. Message "
303+
f"content: {result}"
306304
)
307-
)
308-
case ("recall", {"query": query, "sectors": sectors}):
309-
result = self._memory.recall(query, sectors)
310-
311-
case ("remember", {"text": text, "tags": tags}):
312-
result = self._memory.remember(text, tags)
313-
314-
case ("wiki_search", {"query": query}):
315-
if self._wiki:
316-
result = self._wiki.search(query)
317-
else:
318-
result = "Wiki client not available. Set BRO_WIKI_API_TOKEN environment variable."
319-
320-
case ("wiki_list_pages", _):
321-
if self._wiki:
322-
result = self._wiki.list_pages()
323-
else:
324-
result = "Wiki client not available. Set BRO_WIKI_API_TOKEN environment variable."
325-
326-
case ("wiki_fetch_page", {"path": path}):
327-
if self._wiki:
328-
result = self._wiki.fetch_page(path)
329-
else:
330-
result = "Wiki client not available. Set BRO_WIKI_API_TOKEN environment variable."
331-
332-
case _:
333-
_logger.error(f"Unrecognized function call: {name!r}({args})")
305+
else:
306+
self._msgs.append(
307+
ReceivedMessage(
308+
via=self._current_task.channel,
309+
user=User(name="Bro"),
310+
text=f"Send message to the user: {result}",
311+
attachments=[],
312+
)
313+
)
314+
case ("recall", {"query": query, "sectors": sectors}):
315+
result = self._memory.recall(query, sectors)
316+
317+
case ("remember", {"text": text, "tags": tags}):
318+
result = self._memory.remember(text, tags)
319+
320+
case ("wiki_search", {"query": query}):
321+
if self._wiki:
322+
result = self._wiki.search(query)
323+
else:
324+
result = "Wiki client not available. Set BRO_WIKI_API_TOKEN environment variable."
325+
326+
case ("wiki_list_pages", _):
327+
if self._wiki:
328+
result = self._wiki.list_pages()
329+
else:
330+
result = "Wiki client not available. Set BRO_WIKI_API_TOKEN environment variable."
331+
332+
case ("wiki_fetch_page", {"path": path}):
333+
if self._wiki:
334+
result = self._wiki.fetch_page(path)
335+
else:
336+
result = "Wiki client not available. Set BRO_WIKI_API_TOKEN environment variable."
337+
338+
case _:
339+
_logger.error(f"Unrecognized function call: {name!r}({args})")
334340

335341
if result:
336342
_logger.info(f"Function call result: {result}")

0 commit comments

Comments
 (0)