|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import asyncio |
| 17 | +from typing import ( |
| 18 | + Any, |
| 19 | + Awaitable, |
| 20 | + Callable, |
| 21 | + Iterable, |
| 22 | + Mapping, |
| 23 | + Sequence, |
| 24 | + Type, |
| 25 | + Union, |
| 26 | + cast, |
| 27 | +) |
| 28 | + |
| 29 | +from pydantic import BaseModel, Field, create_model |
| 30 | + |
| 31 | +from toolbox_core.protocol import ParameterSchema |
| 32 | + |
| 33 | + |
| 34 | +def create_docstring(description: str, params: Sequence[ParameterSchema]) -> str: |
| 35 | + """Convert tool description and params into its function docstring""" |
| 36 | + docstring = description |
| 37 | + if not params: |
| 38 | + return docstring |
| 39 | + docstring += "\n\nArgs:" |
| 40 | + for p in params: |
| 41 | + docstring += ( |
| 42 | + f"\n {p.name} ({p.to_param().annotation.__name__}): {p.description}" |
| 43 | + ) |
| 44 | + return docstring |
| 45 | + |
| 46 | + |
| 47 | +def identify_required_authn_params( |
| 48 | + req_authn_params: Mapping[str, list[str]], auth_service_names: Iterable[str] |
| 49 | +) -> dict[str, list[str]]: |
| 50 | + """ |
| 51 | + Identifies authentication parameters that are still required; because they |
| 52 | + not covered by the provided `auth_service_names`. |
| 53 | +
|
| 54 | + Args: |
| 55 | + req_authn_params: A mapping of parameter names to sets of required |
| 56 | + authentication services. |
| 57 | + auth_service_names: An iterable of authentication service names for which |
| 58 | + token getters are available. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + A new dictionary representing the subset of required authentication parameters |
| 62 | + that are not covered by the provided `auth_services`. |
| 63 | + """ |
| 64 | + required_params = {} # params that are still required with provided auth_services |
| 65 | + for param, services in req_authn_params.items(): |
| 66 | + # if we don't have a token_getter for any of the services required by the param, |
| 67 | + # the param is still required |
| 68 | + required = not any(s in services for s in auth_service_names) |
| 69 | + if required: |
| 70 | + required_params[param] = services |
| 71 | + return required_params |
| 72 | + |
| 73 | + |
| 74 | +def params_to_pydantic_model( |
| 75 | + tool_name: str, params: Sequence[ParameterSchema] |
| 76 | +) -> Type[BaseModel]: |
| 77 | + """Converts the given parameters to a Pydantic BaseModel class.""" |
| 78 | + field_definitions = {} |
| 79 | + for field in params: |
| 80 | + field_definitions[field.name] = cast( |
| 81 | + Any, |
| 82 | + ( |
| 83 | + field.to_param().annotation, |
| 84 | + Field(description=field.description), |
| 85 | + ), |
| 86 | + ) |
| 87 | + return create_model(tool_name, **field_definitions) |
| 88 | + |
| 89 | + |
| 90 | +async def resolve_value( |
| 91 | + source: Union[Callable[[], Awaitable[Any]], Callable[[], Any], Any], |
| 92 | +) -> Any: |
| 93 | + """ |
| 94 | + Asynchronously or synchronously resolves a given source to its value. |
| 95 | +
|
| 96 | + If the `source` is a coroutine function, it will be awaited. |
| 97 | + If the `source` is a regular callable, it will be called. |
| 98 | + Otherwise (if it's not a callable), the `source` itself is returned directly. |
| 99 | +
|
| 100 | + Args: |
| 101 | + source: The value, a callable returning a value, or a callable |
| 102 | + returning an awaitable value. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + The resolved value. |
| 106 | + """ |
| 107 | + |
| 108 | + if asyncio.iscoroutinefunction(source): |
| 109 | + return await source() |
| 110 | + elif callable(source): |
| 111 | + return source() |
| 112 | + return source |
0 commit comments