11import typing as t
22
33from llama_index .core .workflow import Context
4- from llama_index .core .workflow .handler import WorkflowHandler
54from oso_semantic .definition import SemanticQuery
65from pydantic import BaseModel , Field
6+ from workflows .handler import WorkflowHandler
77
88from .sql_query import SqlQuery
99
1010
1111class ErrorResponse (BaseModel ):
1212 type : t .Literal ["error" ] = "error"
1313
14- message : str = Field (
15- description = "Error message from the agent."
16- )
14+ message : str = Field (description = "Error message from the agent." )
1715
1816 details : str = Field (
19- default = "" ,
20- description = "Optional details about the error, if available."
17+ default = "" , description = "Optional details about the error, if available."
2118 )
2219
2320 def __str__ (self ) -> str :
2421 """Return the string representation of the error response."""
25- return f"Error: { self .message } | Details: { self .details } " if self .details else f"Error: { self .message } "
22+ return (
23+ f"Error: { self .message } | Details: { self .details } "
24+ if self .details
25+ else f"Error: { self .message } "
26+ )
27+
2628
2729class StrResponse (BaseModel ):
2830 type : t .Literal ["str" ] = "str"
@@ -35,6 +37,7 @@ def __str__(self) -> str:
3537 """Return the string representation of the response."""
3638 return self .blob
3739
40+
3841class AnyResponse (BaseModel ):
3942 type : t .Literal ["any" ] = "any"
4043
@@ -45,14 +48,16 @@ class AnyResponse(BaseModel):
4548 def __str__ (self ):
4649 return str (self .raw )
4750
51+
4852class SemanticResponse (BaseModel ):
4953 type : t .Literal ["semantic" ] = "semantic"
5054
5155 query : SemanticQuery
5256
5357 def __str__ (self ):
5458 return self .query .model_dump_json ()
55-
59+
60+
5661class SqlResponse (BaseModel ):
5762 type : t .Literal ["sql" ] = "sql"
5863
@@ -61,16 +66,15 @@ class SqlResponse(BaseModel):
6166 def __str__ (self ):
6267 return self .query .query
6368
69+
6470ResponseType = t .Union [
65- StrResponse ,
66- SemanticResponse ,
67- SqlResponse ,
68- ErrorResponse ,
69- AnyResponse
71+ StrResponse , SemanticResponse , SqlResponse , ErrorResponse , AnyResponse
7072]
7173
74+
7275class WrappedResponse :
7376 """A wrapper for the response from an agent"""
77+
7478 _response : ResponseType
7579 _handler : WorkflowHandler | None
7680
@@ -84,7 +88,7 @@ def ctx(self) -> Context:
8488 assert self ._handler is not None , "Workflow handler is not set."
8589 assert self ._handler .ctx is not None , "Workflow handler context is not set."
8690 return self ._handler .ctx
87-
91+
8892 @property
8993 def response (self ) -> ResponseType :
9094 """Get the response from the agent."""
0 commit comments