polyapi-python 0.3.19.dev4__tar.gz → 0.3.20.dev0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/PKG-INFO +1 -1
  2. polyapi_python-0.3.20.dev0/polyapi/ai_function.py +96 -0
  3. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/generate.py +12 -1
  4. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/typedefs.py +1 -1
  5. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi_python.egg-info/PKG-INFO +1 -1
  6. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi_python.egg-info/SOURCES.txt +1 -0
  7. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/pyproject.toml +1 -1
  8. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/LICENSE +0 -0
  9. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/README.md +0 -0
  10. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/__init__.py +0 -0
  11. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/__main__.py +0 -0
  12. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/api.py +0 -0
  13. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/auth.py +0 -0
  14. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/cli.py +0 -0
  15. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/cli_constants.py +0 -0
  16. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/client.py +0 -0
  17. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/config.py +0 -0
  18. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/constants.py +0 -0
  19. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/deployables.py +0 -0
  20. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/error_handler.py +0 -0
  21. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/exceptions.py +0 -0
  22. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/execute.py +0 -0
  23. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/function_cli.py +0 -0
  24. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/http_client.py +0 -0
  25. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/parser.py +0 -0
  26. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/poly_schemas.py +0 -0
  27. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/poly_tables.py +0 -0
  28. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/prepare.py +0 -0
  29. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/py.typed +0 -0
  30. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/rendered_spec.py +0 -0
  31. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/schema.py +0 -0
  32. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/server.py +0 -0
  33. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/sync.py +0 -0
  34. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/utils.py +0 -0
  35. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/variables.py +0 -0
  36. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi/webhook.py +0 -0
  37. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi_python.egg-info/dependency_links.txt +0 -0
  38. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi_python.egg-info/requires.txt +0 -0
  39. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/polyapi_python.egg-info/top_level.txt +0 -0
  40. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/setup.cfg +0 -0
  41. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_api.py +0 -0
  42. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_async_proof.py +0 -0
  43. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_auth.py +0 -0
  44. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_cli.py +0 -0
  45. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_dependency_pins.py +0 -0
  46. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_deployables.py +0 -0
  47. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_generate.py +0 -0
  48. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_parser.py +0 -0
  49. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_poly_custom.py +0 -0
  50. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_poly_schemas.py +0 -0
  51. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_rendered_spec.py +0 -0
  52. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_schema.py +0 -0
  53. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_server.py +0 -0
  54. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_tabi.py +0 -0
  55. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_utils.py +0 -0
  56. {polyapi_python-0.3.19.dev4 → polyapi_python-0.3.20.dev0}/tests/test_variables.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.19.dev4
3
+ Version: 0.3.20.dev0
4
4
  Summary: The Python SDK for the integration platform PolyAPI
5
5
  Author-email: PolyAPI <support@polyapi.io>
6
6
  License: MIT License
@@ -0,0 +1,96 @@
1
+ from typing import Any, Dict, List, Tuple, cast
2
+
3
+ from polyapi.typedefs import PropertySpecification, PropertyType
4
+ from polyapi.utils import add_type_import_path, parse_arguments, get_type_and_def, return_type_already_defined_in_args, rewrite_arg_name
5
+
6
+ AI_DEFS_TEMPLATE = """
7
+ from typing import List, Dict, Any, TypedDict, Callable
8
+ {args_def}
9
+ {return_type_def}
10
+ """
11
+
12
+ AI_FUNCTION_TEMPLATE = """
13
+ def {function_name}(
14
+ {args}
15
+ ) -> {return_type_name}:
16
+ \"""{function_description}
17
+
18
+ Function ID: {function_id}
19
+ \"""
20
+ resp = execute("{function_type}", "{function_id}", {data})
21
+ try:
22
+ return {return_action}
23
+ except:
24
+ return resp.text # type: ignore # fallback for debugging
25
+
26
+
27
+ async def {function_name}_async(
28
+ {args}
29
+ ) -> {return_type_name}:
30
+ \"""{function_description}
31
+
32
+ Function ID: {function_id}
33
+ \"""
34
+ resp = await execute_async("{function_type}", "{function_id}", {data})
35
+ try:
36
+ return {return_action}
37
+ except:
38
+ return resp.text # type: ignore # fallback for debugging
39
+
40
+
41
+ """
42
+
43
+
44
+ def render_ai_function(
45
+ function_type: str,
46
+ function_name: str,
47
+ function_id: str,
48
+ function_description: str,
49
+ arguments: List[PropertySpecification],
50
+ return_type: Dict[str, Any],
51
+ ) -> Tuple[str, str]:
52
+ arg_names = [a["name"] for a in arguments]
53
+ args, args_def = parse_arguments(function_name, arguments)
54
+ return_type_name, return_type_def = get_type_and_def(cast(PropertyType, return_type), "ReturnType")
55
+
56
+ if return_type_def and return_type_already_defined_in_args(return_type_name, args_def):
57
+ return_type_def = ""
58
+
59
+ data = "{" + ", ".join([f"'{arg}': {rewrite_arg_name(arg)}" for arg in arg_names]) + "}"
60
+ func_type_defs = AI_DEFS_TEMPLATE.format(
61
+ args_def=args_def,
62
+ return_type_def=return_type_def,
63
+ )
64
+ func_str = AI_FUNCTION_TEMPLATE.format(
65
+ return_type_name=_normalize_return_type_for_annotation(function_name, return_type_name),
66
+ function_type="ai",
67
+ function_name=function_name,
68
+ function_id=function_id,
69
+ function_description=function_description.replace('"', "'"),
70
+ args=args,
71
+ return_action=_get_ai_return_action(return_type_name),
72
+ data=data,
73
+ )
74
+ return func_str, func_type_defs
75
+
76
+
77
+ def _normalize_return_type_for_annotation(function_name: str, return_type_name: str) -> str:
78
+ if return_type_name == "ReturnType":
79
+ return "ReturnType"
80
+ return add_type_import_path(function_name, return_type_name)
81
+
82
+
83
+ def _get_ai_return_action(return_type_name: str) -> str:
84
+ normalized_type = return_type_name.replace(" ", "")
85
+
86
+ if normalized_type in {"str", "Optional[str]"}:
87
+ return_action = "resp.text"
88
+ elif "|" in normalized_type:
89
+ union_parts = {part for part in normalized_type.split("|") if part}
90
+ if union_parts == {"str", "None"}:
91
+ return_action = "resp.text"
92
+ else:
93
+ return_action = "resp.json()"
94
+ else:
95
+ return_action = "resp.json()"
96
+ return return_action
@@ -17,6 +17,7 @@ from .webhook import render_webhook_handle
17
17
  from .typedefs import PropertySpecification, SchemaSpecDto, SpecificationDto, VariableSpecDto, TableSpecDto
18
18
  from .api import render_api_function
19
19
  from .server import render_server_function
20
+ from .ai_function import render_ai_function
20
21
  from .utils import add_import_to_init, get_auth_headers, init_the_init, print_green, to_func_namespace, to_type_module_alias
21
22
  from .variables import generate_variables
22
23
  from .poly_tables import generate_tables
@@ -29,6 +30,7 @@ SUPPORTED_FUNCTION_TYPES = {
29
30
  "customFunction", # client function - this is badly named in /specs atm
30
31
  "serverFunction",
31
32
  "webhookHandle",
33
+ "aiFunction",
32
34
  }
33
35
 
34
36
  SUPPORTED_TYPES = SUPPORTED_FUNCTION_TYPES | {"serverVariable", "schema", "snippet", "table"}
@@ -98,7 +100,7 @@ def resolve_poly_refs(obj, schema_index):
98
100
  def replace_poly_refs_in_functions(specs: List[SpecificationDto], schema_index):
99
101
  spec_idxs_to_remove = []
100
102
  for idx, spec in enumerate(specs):
101
- if spec.get("type") in ("apiFunction", "customFunction", "serverFunction"):
103
+ if spec.get("type") in ("apiFunction", "customFunction", "serverFunction", "aiFunction"):
102
104
  func = spec.get("function")
103
105
  if func:
104
106
  try:
@@ -501,6 +503,15 @@ def render_spec(spec: SpecificationDto) -> Tuple[str, str]:
501
503
  arguments,
502
504
  return_type,
503
505
  )
506
+ elif function_type == "aiFunction":
507
+ func_str, func_type_defs = render_ai_function(
508
+ function_type,
509
+ function_name,
510
+ function_id,
511
+ function_description,
512
+ arguments,
513
+ return_type,
514
+ )
504
515
 
505
516
  if X_POLY_REF_WARNING in func_type_defs:
506
517
  # this indicates that jsonschema_gentypes has detected an x-poly-ref
@@ -35,7 +35,7 @@ class SpecificationDto(TypedDict):
35
35
  description: str
36
36
  # function is none (or function key not present) if this is actually VariableSpecDto
37
37
  function: NotRequired[FunctionSpecification | None]
38
- type: Literal['apiFunction', 'customFunction', 'serverFunction', 'authFunction', 'webhookHandle', 'serverVariable', 'table']
38
+ type: Literal['apiFunction', 'customFunction', 'serverFunction', 'authFunction', 'webhookHandle', 'serverVariable', 'table', 'aiFunction']
39
39
  code: NotRequired[str]
40
40
  language: str
41
41
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.19.dev4
3
+ Version: 0.3.20.dev0
4
4
  Summary: The Python SDK for the integration platform PolyAPI
5
5
  Author-email: PolyAPI <support@polyapi.io>
6
6
  License: MIT License
@@ -3,6 +3,7 @@ README.md
3
3
  pyproject.toml
4
4
  polyapi/__init__.py
5
5
  polyapi/__main__.py
6
+ polyapi/ai_function.py
6
7
  polyapi/api.py
7
8
  polyapi/auth.py
8
9
  polyapi/cli.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "polyapi-python"
7
- version = "0.3.19.dev4"
7
+ version = "0.3.20.dev0"
8
8
  description = "The Python SDK for the integration platform PolyAPI"
9
9
  authors = [{ name = "PolyAPI", email = "support@polyapi.io" }]
10
10
  dependencies = [