Skip to content

Commit

Permalink
Fix issues related to 0 messages in state
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Nov 20, 2023
1 parent 7757f92 commit d38fafe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 4 additions & 3 deletions backend/app/api/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ async def consume_astream() -> None:
await streamer.send_stream.send(chunk)
# hack: function messages aren't generated by chat model
# so the callback handler doesn't know about them
message = chunk["messages"][-1]
if isinstance(message, FunctionMessage):
streamer.output[uuid4()] = ChatGeneration(message=message)
if chunk["messages"]:
message = chunk["messages"][-1]
if isinstance(message, FunctionMessage):
streamer.output[uuid4()] = ChatGeneration(message=message)
except Exception as e:
await streamer.send_stream.send(e)
finally:
Expand Down
6 changes: 5 additions & 1 deletion backend/packages/agent-executor/agent_executor/permchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def get_agent_executor(
agent_chain = agent | _create_agent_message | Channel.write_to("messages")

def route_last_message(input: dict[str, bool | Sequence[AnyMessage]]) -> Runnable:
if not input["messages"]:
# no messages, do nothing
return RunnablePassthrough()

message: AnyMessage = input["messages"][-1]
if isinstance(message.additional_kwargs.get("agent"), AgentFinish):
# finished, do nothing
Expand All @@ -137,7 +141,7 @@ def route_last_message(input: dict[str, bool | Sequence[AnyMessage]]) -> Runnabl
return agent_chain

executor = (
Channel.subscribe_to(["messages", ReservedChannels.is_last_step])
Channel.subscribe_to(["messages"]).join([ReservedChannels.is_last_step])
| route_last_message
)

Expand Down
12 changes: 7 additions & 5 deletions frontend/src/hooks/useStreamState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Message } from "./useChatList";

export interface StreamState {
status: "inflight" | "error" | "done";
messages: Message[];
messages?: Message[];
run_id?: string;
merge?: boolean;
}
Expand Down Expand Up @@ -50,30 +50,32 @@ export function useStreamState(): StreamStateProps {
const { run_id } = JSON.parse(msg.data);
setCurrent((current) => ({
status: "inflight",
messages: current?.messages ?? [],
messages: current?.messages,
run_id: run_id,
}));
} else if (msg.event === "error") {
setCurrent((current) => ({
status: "error",
messages: current?.messages ?? [],
messages: current?.messages,
run_id: current?.run_id,
}));
}
},
onclose() {
setCurrent((current) => ({
status: current?.status === "error" ? current.status : "done",
messages: current?.messages ?? [],
messages: current?.messages,
run_id: current?.run_id,
merge: current?.merge,
}));
setController(null);
},
onerror(error) {
setCurrent((current) => ({
status: "error",
messages: current?.messages ?? [],
messages: current?.messages,
run_id: current?.run_id,
merge: current?.merge,
}));
setController(null);
throw error;
Expand Down

0 comments on commit d38fafe

Please sign in to comment.