Skip to content

Commit

Permalink
Emit Bug Fix [explanation required] (#409)
Browse files Browse the repository at this point in the history
This is a fix for a bug where the first message to a web bot works, but
subsequent calls do not.

The issue appears to be that SOMEHOW, the emit_func from the previous
run is still present in memory. This does not make any sense to me, nor
does the "existing emit functions" logging support that.

AND YET - The other logging, of which emit function is being used,
supports the existing-functions theory. Before the change to overwrite
the list, if you sent a message to the Web client, then a message to
Telegram, the telegram call would attempt to use the web client's emit.
Also, changing the code to always overwrite the emit function list fixes
the issue.
  • Loading branch information
dkolas authored Jun 2, 2023
1 parent c608530 commit f6e87e8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/steamship/agents/schema/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ def get_or_create(
context.chat_history = history
context.client = client
context.completed_steps = []
context.emit_funcs = []
return context
7 changes: 7 additions & 0 deletions src/steamship/agents/service/agent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,12 @@ def run_agent(self, agent: Agent, context: AgentContext):
)

context.completed_steps.append(action)
output_text_length = 0
if action.output is not None:
output_text_length = sum([len(block.text or "") for block in action.output])
logging.info(
f"Completed agent run. Result: {len(action.output or [])} blocks. {output_text_length} total text length. Emitting on {len(context.emit_funcs)} functions."
)
for func in context.emit_funcs:
logging.info(f"Emitting via function: {func.__name__}")
func(action.output, context.metadata)
4 changes: 2 additions & 2 deletions src/steamship/experimental/package_starters/telegram_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def respond(self, **kwargs) -> InvocableResponse[str]:
if incoming_message is not None:
context = AgentContext.get_or_create(self.client, context_keys={"chat_id": chat_id})
context.chat_history.append_user_message(text=incoming_message.text)
if len(context.emit_funcs) == 0:
context.emit_funcs.append(self.build_emit_func(chat_id=chat_id))
logging.info(f"Existing emit functions: {len(context.emit_funcs)}")
context.emit_funcs = [self.build_emit_func(chat_id=chat_id)]

response = self.run_agent(self.incoming_message_agent, context)
if response is not None:
Expand Down
6 changes: 4 additions & 2 deletions src/steamship/experimental/package_starters/web_agent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from abc import ABC
from typing import List, Optional

Expand Down Expand Up @@ -39,8 +40,8 @@ def answer(self, **payload) -> List[Block]:
self.client, context_keys={"chat_id": incoming_message.chat_id}
)
context.chat_history.append_user_message(text=incoming_message.text)
if len(context.emit_funcs) == 0:
context.emit_funcs.append(self.save_for_emit)
logging.info(f"Existing emit functions: {len(context.emit_funcs)}")
context.emit_funcs = [self.save_for_emit]
try:
self.run_agent(self.incoming_message_agent, context)
except Exception as e:
Expand All @@ -50,4 +51,5 @@ def answer(self, **payload) -> List[Block]:
return self.message_output

def save_for_emit(self, blocks: List[Block], metadata: Metadata):
logging.info(f"Emitting by saving to self of type {type(self)}")
self.message_output = blocks

0 comments on commit f6e87e8

Please sign in to comment.