Trying to fix non-streaming

This commit is contained in:
Cyber MacGeddon 2026-01-12 21:17:57 +00:00
parent 37b7137832
commit abbbe533b9
2 changed files with 39 additions and 38 deletions

View file

@ -118,32 +118,20 @@ class SocketClient:
async with websockets.connect(ws_url, ping_interval=20, ping_timeout=self.timeout) as websocket: async with websockets.connect(ws_url, ping_interval=20, ping_timeout=self.timeout) as websocket:
await websocket.send(json.dumps(message)) await websocket.send(json.dumps(message))
# Some services (like agent) send multiple messages even in non-streaming mode # Wait for single response
# Collect all messages until complete=True raw_message = await websocket.recv()
messages = [] response = json.loads(raw_message)
while True: if response.get("id") != request_id:
raw_message = await websocket.recv() raise ProtocolException(f"Response ID mismatch")
response = json.loads(raw_message)
if response.get("id") != request_id: if "error" in response:
raise ProtocolException(f"Response ID mismatch") raise_from_error_dict(response["error"])
if "error" in response: if "response" not in response:
raise_from_error_dict(response["error"]) raise ProtocolException(f"Missing response in message")
if "response" not in response: return response["response"]
raise ProtocolException(f"Missing response in message")
messages.append(response["response"])
# Check if this is the final message
if response.get("complete", False):
break
# For single-message responses, return the dict directly
# For multi-message responses (like agent), return a list
return messages[0] if len(messages) == 1 else messages
async def _send_request_async_streaming( async def _send_request_async_streaming(
self, self,
@ -226,6 +214,23 @@ class SocketClient:
content=resp.get("content", ""), content=resp.get("content", ""),
end_of_message=resp.get("end_of_message", False) end_of_message=resp.get("end_of_message", False)
) )
# Non-streaming agent format: chunk_type is empty but has thought/observation/answer fields
elif resp.get("thought"):
return AgentThought(
content=resp.get("thought", ""),
end_of_message=resp.get("end_of_message", False)
)
elif resp.get("observation"):
return AgentObservation(
content=resp.get("observation", ""),
end_of_message=resp.get("end_of_message", False)
)
elif resp.get("answer"):
return AgentAnswer(
content=resp.get("answer", ""),
end_of_message=resp.get("end_of_message", False),
end_of_dialog=resp.get("end_of_dialog", False)
)
else: else:
# RAG-style chunk (or generic chunk) # RAG-style chunk (or generic chunk)
# Text-completion uses "response" field, RAG uses "chunk" field, Prompt uses "text" field # Text-completion uses "response" field, RAG uses "chunk" field, Prompt uses "text" field
@ -273,7 +278,9 @@ class SocketFlowInstance:
request["history"] = history request["history"] = history
request.update(kwargs) request.update(kwargs)
return self.client._send_request_sync("agent", self.flow_id, request, streaming) # Agents always use multipart messaging (multiple complete messages)
# regardless of streaming flag, so always use the streaming code path
return self.client._send_request_sync("agent", self.flow_id, request, streaming=True)
def text_completion(self, system: str, prompt: str, streaming: bool = False, **kwargs) -> Union[str, Iterator[str]]: def text_completion(self, system: str, prompt: str, streaming: bool = False, **kwargs) -> Union[str, Iterator[str]]:
"""Text completion with optional streaming""" """Text completion with optional streaming"""

View file

@ -178,28 +178,22 @@ def question(
print() print()
else: else:
# Non-streaming response # Non-streaming response - but agents use multipart messaging
# Response may be a list of messages (thought/observation/answer) or a single dict # so we iterate through the chunks (which are complete messages, not text chunks)
messages = response if isinstance(response, list) else [response] for chunk in response:
for msg in messages:
# Display thoughts if verbose # Display thoughts if verbose
if "thought" in msg and msg["thought"] and verbose: if chunk.chunk_type == "thought" and verbose:
output(wrap(msg["thought"]), "\U0001f914 ") output(wrap(chunk.content), "\U0001f914 ")
print() print()
# Display observations if verbose # Display observations if verbose
if "observation" in msg and msg["observation"] and verbose: elif chunk.chunk_type == "observation" and verbose:
output(wrap(msg["observation"]), "\U0001f4a1 ") output(wrap(chunk.content), "\U0001f4a1 ")
print() print()
# Display answer # Display answer
if "answer" in msg and msg["answer"]: elif chunk.chunk_type == "final-answer" or chunk.chunk_type == "answer":
print(msg["answer"]) print(chunk.content)
# Handle errors
if "error" in msg:
raise RuntimeError(msg["error"])
finally: finally:
# Clean up socket connection # Clean up socket connection