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:
mykonos-ibiza 2025-07-29 21:23:37 +05:30
parent 53fb20aa6a
commit aa75f47ac3
7 changed files with 27 additions and 24 deletions

22
sdk/kortix/api/utils.py Normal file
View File

@ -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()

View File

@ -1,6 +1,7 @@
from .api import agents, threads
from .agent import KortixAgent
from .thread import KortixThread
from .tools import AgentPressTools, KortixMCP
class Kortix:

View File

@ -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

View File

@ -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."""

View File

@ -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: