()
| 318 | |
| 319 | |
| 320 | def main(): |
| 321 | if len(sys.argv) < 3: |
| 322 | print( |
| 323 | "Usage: persistent_wrapper.py <script_path> <function_name>", |
| 324 | file=sys.stderr, |
| 325 | ) |
| 326 | sys.exit(1) |
| 327 | |
| 328 | script_path = sys.argv[1] |
| 329 | function_name = sys.argv[2] |
| 330 | |
| 331 | # Load user module once |
| 332 | try: |
| 333 | user_module = load_user_module(script_path) |
| 334 | # Note: We don't validate the default function exists at initialization. |
| 335 | # With the persistent worker protocol supporting dynamic function calls per request, |
| 336 | # users may only define specific functions (e.g., call_embedding_api for embeddings-only). |
| 337 | # Functions are validated when actually called in handle_call(), providing clear |
| 338 | # error messages with available functions and suggestions. |
| 339 | except Exception as e: |
| 340 | print(f"ERROR: Failed to load module: {e}", file=sys.stderr, flush=True) |
| 341 | print(traceback.format_exc(), file=sys.stderr, flush=True) |
| 342 | sys.exit(1) |
| 343 | |
| 344 | # Signal ready |
| 345 | print("READY", flush=True) |
| 346 | |
| 347 | # Main loop - wait for commands |
| 348 | while True: |
| 349 | try: |
| 350 | line = sys.stdin.readline() |
| 351 | if not line: |
| 352 | # stdin closed, exit gracefully |
| 353 | break |
| 354 | |
| 355 | line = line.strip() |
| 356 | |
| 357 | if line.startswith("SHUTDOWN"): |
| 358 | break |
| 359 | elif line.startswith("CALL|"): |
| 360 | handle_call(line, user_module, function_name) |
| 361 | else: |
| 362 | print(f"ERROR: Unknown command: {line}", file=sys.stderr, flush=True) |
| 363 | |
| 364 | except KeyboardInterrupt: |
| 365 | break |
| 366 | except Exception as e: |
| 367 | print(f"ERROR in main loop: {e}", file=sys.stderr, flush=True) |
| 368 | print(traceback.format_exc(), file=sys.stderr, flush=True) |
| 369 | |
| 370 | |
| 371 | def handle_call(command_line, user_module, default_function_name): |
no test coverage detected
searching dependent graphs…