ivcap-lambda 0.8.10__tar.gz → 0.8.11__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ivcap-lambda
3
- Version: 0.8.10
3
+ Version: 0.8.11
4
4
  Summary: Helper functions for building lambda-style services on the IVCAP platform
5
5
  License-File: AUTHORS.md
6
6
  License-File: LICENSE
@@ -4,6 +4,8 @@
4
4
  # found in the LICENSE file. See the AUTHORS file for names of contributors.
5
5
  #
6
6
  import argparse
7
+ import asyncio
8
+ import json
7
9
  import os
8
10
  import sys
9
11
  from collections.abc import Callable
@@ -15,6 +17,8 @@ from typing import Any
15
17
  import uvicorn
16
18
  from fastapi import FastAPI, Request, Response
17
19
  from ivcap_service import (
20
+ ExecutionError,
21
+ IvcapResult,
18
22
  Service,
19
23
  SidecarReporter,
20
24
  getLogger,
@@ -24,14 +28,16 @@ from ivcap_service import (
24
28
  service_log_config,
25
29
  set_context,
26
30
  set_event_reporter_factory,
31
+ set_sidecar_delivery_disabled,
27
32
  )
28
33
  from ivcap_service import (
29
34
  get_version as get_service_version,
30
35
  )
31
36
  from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
37
+ from starlette.requests import Request as StarletteRequest
32
38
 
33
39
  # from .context import set_context, otel_instrument
34
- from .builder import tools
40
+ from .builder import ToolDescription, tools
35
41
  from .executor import Executor, get_job_context
36
42
  from .health_handler import add_health_route
37
43
  from .version import get_version, resolve_service_version
@@ -53,6 +59,58 @@ def get_fast_app() -> FastAPI:
53
59
  return _app
54
60
 
55
61
 
62
+ def run_test_file(path: str, tool: ToolDescription) -> None:
63
+ """Run `tool` once on a job file in the ivcap-service batch format and print the result.
64
+
65
+ The job file has the same shape as the ones used by ivcap-service's
66
+ `--test-file`: `id`, `in-content-type` and `in-content`.
67
+
68
+ Args:
69
+ path (str): path to the job JSON file
70
+ tool (ToolDescription): the registered tool to execute
71
+ """
72
+ with open(path) as f:
73
+ job = json.load(f)
74
+ job_id = job.get("id", "unknown_job_id")
75
+ ct = job["in-content-type"]
76
+ if ct != "application/json":
77
+ raise ValueError(f"cannot handle content-type '{ct}'")
78
+ input_model, _ = tool.input
79
+ if input_model is None:
80
+ raise ValueError(f"tool '{tool.name}' has no input model")
81
+ param = input_model(**job["in-content"])
82
+
83
+ scope = {
84
+ "type": "http",
85
+ "method": "POST",
86
+ "scheme": "http",
87
+ "server": ("localhost", 8090),
88
+ "path": "/",
89
+ "query_string": b"",
90
+ "headers": [],
91
+ }
92
+
93
+ async def _execute():
94
+ queue = await tool.executor.execute(
95
+ param, job_id, StarletteRequest(scope), report_result=False
96
+ )
97
+ return await queue.get()
98
+
99
+ res = asyncio.run(_execute())
100
+ if isinstance(res, IvcapResult):
101
+ content = res.content
102
+ try:
103
+ if not isinstance(content, (bytes, str)):
104
+ raise ValueError("content is not in-memory")
105
+ print(json.dumps(json.loads(content), indent=2))
106
+ except ValueError:
107
+ print(content)
108
+ else:
109
+ print(res.model_dump_json(indent=2))
110
+ if isinstance(res, ExecutionError):
111
+ sys.exit(1)
112
+
113
+
56
114
  def start_lambda_server(
57
115
  service: Service,
58
116
  *,
@@ -152,6 +210,25 @@ def start_lambda_server(
152
210
  help=f"Print tool description to stdout [{','.join(tool_names)}]",
153
211
  )
154
212
 
213
+ parser.add_argument(
214
+ "--test-file",
215
+ type=str,
216
+ help=(
217
+ "Run the first tool on a job file (ivcap-service format with 'id', "
218
+ "'in-content-type' and 'in-content') and print the result, then exit"
219
+ ),
220
+ )
221
+ parser.add_argument(
222
+ "--test-without-sidecar",
223
+ action="store_true",
224
+ help=(
225
+ "Silently drop any attempt to deliver events or results to the "
226
+ "sidecar (no HTTP calls, no retries, no warnings). Useful for "
227
+ "local testing (typically together with --test-file) without a "
228
+ "reachable IVCAP_BASE_URL/sidecar."
229
+ ),
230
+ )
231
+
155
232
  if custom_args is not None:
156
233
  args = custom_args(parser)
157
234
  else:
@@ -218,6 +295,13 @@ def start_lambda_server(
218
295
  elif getattr(args, "with_telemetry", False):
219
296
  effective_with_telemetry = True
220
297
 
298
+ if args.test_without_sidecar:
299
+ logger.info(
300
+ "--test-without-sidecar set - events/results will not be delivered "
301
+ "to the sidecar"
302
+ )
303
+ set_sidecar_delivery_disabled(True)
304
+
221
305
  set_event_reporter_factory(SidecarReporter) # type: ignore[arg-type]
222
306
 
223
307
  def get_context():
@@ -230,6 +314,10 @@ def start_lambda_server(
230
314
 
231
315
  set_context(get_context)
232
316
 
317
+ if args.test_file is not None:
318
+ run_test_file(args.test_file, tools[0])
319
+ return
320
+
233
321
  if args.with_mcp_stdio:
234
322
  # Run as a stdio MCP server instead of the HTTP/uvicorn server: no
235
323
  # FastAPI app involved, so there is nothing to instrument there, but
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ivcap-lambda"
3
- version = "0.8.10"
3
+ version = "0.8.11"
4
4
  description = "Helper functions for building lambda-style services on the IVCAP platform"
5
5
 
6
6
  authors = ["Max Ott <max.ott@csiro.au>"]
File without changes
File without changes
File without changes