Skip to content

Commit ae80104

Browse files
chore: speedup initial import
1 parent fa1bf74 commit ae80104

File tree

1 file changed

+105
-28
lines changed

1 file changed

+105
-28
lines changed

src/agentbase/_client.py

Lines changed: 105 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import Any, Mapping, Iterable
6+
from typing import TYPE_CHECKING, Any, Mapping, Iterable
77
from typing_extensions import Self, Literal, override
88

99
import httpx
@@ -31,14 +31,14 @@
3131
get_async_library,
3232
async_maybe_transform,
3333
)
34+
from ._compat import cached_property
3435
from ._version import __version__
3536
from ._response import (
3637
to_raw_response_wrapper,
3738
to_streamed_response_wrapper,
3839
async_to_raw_response_wrapper,
3940
async_to_streamed_response_wrapper,
4041
)
41-
from .resources import agent, messages
4242
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
4343
from ._exceptions import AgentbaseError, APIStatusError
4444
from ._base_client import (
@@ -49,6 +49,11 @@
4949
)
5050
from .types.run_agent_response import RunAgentResponse
5151

52+
if TYPE_CHECKING:
53+
from .resources import agent, messages
54+
from .resources.agent import AgentResource, AsyncAgentResource
55+
from .resources.messages import MessagesResource, AsyncMessagesResource
56+
5257
__all__ = [
5358
"Timeout",
5459
"Transport",
@@ -62,11 +67,6 @@
6267

6368

6469
class Agentbase(SyncAPIClient):
65-
agent: agent.AgentResource
66-
messages: messages.MessagesResource
67-
with_raw_response: AgentbaseWithRawResponse
68-
with_streaming_response: AgentbaseWithStreamedResponse
69-
7070
# client options
7171
api_key: str
7272

@@ -121,10 +121,25 @@ def __init__(
121121
_strict_response_validation=_strict_response_validation,
122122
)
123123

124-
self.agent = agent.AgentResource(self)
125-
self.messages = messages.MessagesResource(self)
126-
self.with_raw_response = AgentbaseWithRawResponse(self)
127-
self.with_streaming_response = AgentbaseWithStreamedResponse(self)
124+
@cached_property
125+
def agent(self) -> AgentResource:
126+
from .resources.agent import AgentResource
127+
128+
return AgentResource(self)
129+
130+
@cached_property
131+
def messages(self) -> MessagesResource:
132+
from .resources.messages import MessagesResource
133+
134+
return MessagesResource(self)
135+
136+
@cached_property
137+
def with_raw_response(self) -> AgentbaseWithRawResponse:
138+
return AgentbaseWithRawResponse(self)
139+
140+
@cached_property
141+
def with_streaming_response(self) -> AgentbaseWithStreamedResponse:
142+
return AgentbaseWithStreamedResponse(self)
128143

129144
@property
130145
@override
@@ -349,11 +364,6 @@ def _make_status_error(
349364

350365

351366
class AsyncAgentbase(AsyncAPIClient):
352-
agent: agent.AsyncAgentResource
353-
messages: messages.AsyncMessagesResource
354-
with_raw_response: AsyncAgentbaseWithRawResponse
355-
with_streaming_response: AsyncAgentbaseWithStreamedResponse
356-
357367
# client options
358368
api_key: str
359369

@@ -408,10 +418,25 @@ def __init__(
408418
_strict_response_validation=_strict_response_validation,
409419
)
410420

411-
self.agent = agent.AsyncAgentResource(self)
412-
self.messages = messages.AsyncMessagesResource(self)
413-
self.with_raw_response = AsyncAgentbaseWithRawResponse(self)
414-
self.with_streaming_response = AsyncAgentbaseWithStreamedResponse(self)
421+
@cached_property
422+
def agent(self) -> AsyncAgentResource:
423+
from .resources.agent import AsyncAgentResource
424+
425+
return AsyncAgentResource(self)
426+
427+
@cached_property
428+
def messages(self) -> AsyncMessagesResource:
429+
from .resources.messages import AsyncMessagesResource
430+
431+
return AsyncMessagesResource(self)
432+
433+
@cached_property
434+
def with_raw_response(self) -> AsyncAgentbaseWithRawResponse:
435+
return AsyncAgentbaseWithRawResponse(self)
436+
437+
@cached_property
438+
def with_streaming_response(self) -> AsyncAgentbaseWithStreamedResponse:
439+
return AsyncAgentbaseWithStreamedResponse(self)
415440

416441
@property
417442
@override
@@ -636,44 +661,96 @@ def _make_status_error(
636661

637662

638663
class AgentbaseWithRawResponse:
664+
_client: Agentbase
665+
639666
def __init__(self, client: Agentbase) -> None:
640-
self.agent = agent.AgentResourceWithRawResponse(client.agent)
641-
self.messages = messages.MessagesResourceWithRawResponse(client.messages)
667+
self._client = client
642668

643669
self.run_agent = to_raw_response_wrapper(
644670
client.run_agent,
645671
)
646672

673+
@cached_property
674+
def agent(self) -> agent.AgentResourceWithRawResponse:
675+
from .resources.agent import AgentResourceWithRawResponse
676+
677+
return AgentResourceWithRawResponse(self._client.agent)
678+
679+
@cached_property
680+
def messages(self) -> messages.MessagesResourceWithRawResponse:
681+
from .resources.messages import MessagesResourceWithRawResponse
682+
683+
return MessagesResourceWithRawResponse(self._client.messages)
684+
647685

648686
class AsyncAgentbaseWithRawResponse:
687+
_client: AsyncAgentbase
688+
649689
def __init__(self, client: AsyncAgentbase) -> None:
650-
self.agent = agent.AsyncAgentResourceWithRawResponse(client.agent)
651-
self.messages = messages.AsyncMessagesResourceWithRawResponse(client.messages)
690+
self._client = client
652691

653692
self.run_agent = async_to_raw_response_wrapper(
654693
client.run_agent,
655694
)
656695

696+
@cached_property
697+
def agent(self) -> agent.AsyncAgentResourceWithRawResponse:
698+
from .resources.agent import AsyncAgentResourceWithRawResponse
699+
700+
return AsyncAgentResourceWithRawResponse(self._client.agent)
701+
702+
@cached_property
703+
def messages(self) -> messages.AsyncMessagesResourceWithRawResponse:
704+
from .resources.messages import AsyncMessagesResourceWithRawResponse
705+
706+
return AsyncMessagesResourceWithRawResponse(self._client.messages)
707+
657708

658709
class AgentbaseWithStreamedResponse:
710+
_client: Agentbase
711+
659712
def __init__(self, client: Agentbase) -> None:
660-
self.agent = agent.AgentResourceWithStreamingResponse(client.agent)
661-
self.messages = messages.MessagesResourceWithStreamingResponse(client.messages)
713+
self._client = client
662714

663715
self.run_agent = to_streamed_response_wrapper(
664716
client.run_agent,
665717
)
666718

719+
@cached_property
720+
def agent(self) -> agent.AgentResourceWithStreamingResponse:
721+
from .resources.agent import AgentResourceWithStreamingResponse
722+
723+
return AgentResourceWithStreamingResponse(self._client.agent)
724+
725+
@cached_property
726+
def messages(self) -> messages.MessagesResourceWithStreamingResponse:
727+
from .resources.messages import MessagesResourceWithStreamingResponse
728+
729+
return MessagesResourceWithStreamingResponse(self._client.messages)
730+
667731

668732
class AsyncAgentbaseWithStreamedResponse:
733+
_client: AsyncAgentbase
734+
669735
def __init__(self, client: AsyncAgentbase) -> None:
670-
self.agent = agent.AsyncAgentResourceWithStreamingResponse(client.agent)
671-
self.messages = messages.AsyncMessagesResourceWithStreamingResponse(client.messages)
736+
self._client = client
672737

673738
self.run_agent = async_to_streamed_response_wrapper(
674739
client.run_agent,
675740
)
676741

742+
@cached_property
743+
def agent(self) -> agent.AsyncAgentResourceWithStreamingResponse:
744+
from .resources.agent import AsyncAgentResourceWithStreamingResponse
745+
746+
return AsyncAgentResourceWithStreamingResponse(self._client.agent)
747+
748+
@cached_property
749+
def messages(self) -> messages.AsyncMessagesResourceWithStreamingResponse:
750+
from .resources.messages import AsyncMessagesResourceWithStreamingResponse
751+
752+
return AsyncMessagesResourceWithStreamingResponse(self._client.messages)
753+
677754

678755
Client = Agentbase
679756

0 commit comments

Comments
 (0)