mirror of https://github.com/kortix-ai/suna.git
feat(kortix): enhance streaming capabilities and add utility functions
- Introduced a new `stream.py` module for processing streamed data, including classes for handling different message types. - Added a local key-value store for managing agent and thread IDs. - Implemented a real-time stream processor with callbacks for handling various events. - Created example scripts for testing the streaming functionality and integrated weather tool. - Updated import statements for better organization and clarity.
This commit is contained in:
parent
53fb20aa6a
commit
aa75f47ac3
|
@ -0,0 +1,22 @@
|
|||
from typing import AsyncGenerator
|
||||
import httpx
|
||||
|
||||
|
||||
async def stream_from_url(url: str, **kwargs) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Helper function that takes a URL and returns an async generator yielding lines.
|
||||
|
||||
Args:
|
||||
url: The URL to stream from
|
||||
**kwargs: Additional arguments to pass to httpx.AsyncClient.stream()
|
||||
|
||||
Yields:
|
||||
str: Each line from the streaming response
|
||||
"""
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with client.stream("GET", url, **kwargs) as response:
|
||||
response.raise_for_status()
|
||||
|
||||
async for line in response.aiter_lines():
|
||||
if line.strip(): # Only yield non-empty lines
|
||||
yield line.strip()
|
|
@ -1,6 +1,7 @@
|
|||
from .api import agents, threads
|
||||
from .agent import KortixAgent
|
||||
from .thread import KortixThread
|
||||
from .tools import AgentPressTools, KortixMCP
|
||||
|
||||
|
||||
class Kortix:
|
||||
|
|
|
@ -6,11 +6,11 @@ from dotenv import load_dotenv
|
|||
from fastmcp import FastMCP
|
||||
|
||||
|
||||
from .kortix import Kortix
|
||||
from .tools import AgentPressTools, KortixMCP
|
||||
from kortix.kortix import Kortix
|
||||
from kortix.tools import AgentPressTools, KortixMCP
|
||||
from .stream import RealtimeStreamProcessor, RealtimeCallbacks
|
||||
|
||||
load_dotenv("../.env")
|
||||
load_dotenv("../../.env")
|
||||
|
||||
|
||||
# Local key-value store for storing agent and thread IDs
|
|
@ -20,26 +20,6 @@ def try_parse_json(json_str: str) -> Optional[Any]:
|
|||
return None
|
||||
|
||||
|
||||
async def stream_from_url(url: str, **kwargs) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Helper function that takes a URL and returns an async generator yielding lines.
|
||||
|
||||
Args:
|
||||
url: The URL to stream from
|
||||
**kwargs: Additional arguments to pass to httpx.AsyncClient.stream()
|
||||
|
||||
Yields:
|
||||
str: Each line from the streaming response
|
||||
"""
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with client.stream("GET", url, **kwargs) as response:
|
||||
response.raise_for_status()
|
||||
|
||||
async for line in response.aiter_lines():
|
||||
if line.strip(): # Only yield non-empty lines
|
||||
yield line.strip()
|
||||
|
||||
|
||||
@dataclass
|
||||
class BaseStreamEvent:
|
||||
"""The base structure for any event coming from the stream."""
|
|
@ -1,7 +1,7 @@
|
|||
from typing import AsyncGenerator
|
||||
|
||||
from .api.threads import ThreadsClient
|
||||
from .stream import stream_from_url
|
||||
from .api.utils import stream_from_url
|
||||
|
||||
|
||||
class Thread:
|
||||
|
|
Loading…
Reference in New Issue