Skip to content

Commit

Permalink
Fix bug of messages not being added
Browse files Browse the repository at this point in the history
  • Loading branch information
WonderPG committed Jan 10, 2025
1 parent 8a8bcf4 commit 1533ce4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/neuroagent/app/routers/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
get_context_variables,
get_session,
get_starting_agent,
get_user_id,
)
from neuroagent.new_types import (
Agent,
Expand Down Expand Up @@ -101,14 +100,13 @@ async def stream_chat_agent(
agents_routine: Annotated[AgentsRoutine, Depends(get_agents_routine)],
agent: Annotated[Agent, Depends(get_starting_agent)],
context_variables: Annotated[dict[str, Any], Depends(get_context_variables)],
session: Annotated[AsyncSession, Depends(get_session)],
user_id: Annotated[str, Depends(get_user_id)],
thread: Annotated[Threads, Depends(get_thread)],
) -> StreamingResponse:
"""Run a single agent query in a streamed fashion."""
# Temporary solution
context_variables["vlab_id"] = thread.vlab_id
context_variables["project_id"] = thread.project_id

messages: list[Messages] = await thread.awaitable_attrs.messages
if not messages or messages[-1].entity != Entity.AI_TOOL:
messages.append(
Expand Down
9 changes: 6 additions & 3 deletions src/neuroagent/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ async def stream_agent_response(
# Restore the session
engine = request.app.state.engine
session = AsyncSession(engine)
# Need to rebind every single message
for message in messages:
session.add(message)
# Need to rebind the messages to the session
session.add_all(messages)

iterator = connected_agents_routine.astream(agent, messages, context_variables)
async for chunk in iterator:
Expand All @@ -58,5 +57,9 @@ async def stream_agent_response(

# Save the new messages in DB
thread.update_date = utc_now()

# For some weird reason need to re-add messages, but only post validation ones
session.add_all(messages)

await session.commit()
await session.close()

0 comments on commit 1533ce4

Please sign in to comment.