codekingpro/portable-devtools
114k
1"""Base class for Gmail tools."""2 3from __future__ import annotations4 5from typing import TYPE_CHECKING6 7from langchain_core.tools import BaseTool8from pydantic import Field9 10from langchain_community.tools.gmail.utils import build_resource_service11 12if TYPE_CHECKING:13 # This is for linting and IDE typehints14 from googleapiclient.discovery import Resource15else:16 try:17 # We do this so pydantic can resolve the types when instantiating18 from googleapiclient.discovery import Resource19 except ImportError:20 pass21 22 23class GmailBaseTool(BaseTool):24 """Base class for Gmail tools."""25 26 api_resource: Resource = Field(default_factory=build_resource_service)27 28 @classmethod29 def from_api_resource(cls, api_resource: Resource) -> "GmailBaseTool":30 """Create a tool from an api resource.31 32 Args:33 api_resource: The api resource to use.34 35 Returns:36 A tool.37 """38 return cls(service=api_resource) # type: ignore[call-arg]39 