hypha-rpc 0.21.35 → 0.21.37

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.
package/README.md CHANGED
@@ -24,4 +24,75 @@ hyphaWebsocketClient.connectToServer({
24
24
  }
25
25
  )
26
26
  })
27
- ```
27
+ ```
28
+
29
+ ### Keyword Arguments (kwargs)
30
+
31
+ Hypha RPC supports Python-style keyword arguments when calling JavaScript services. This works seamlessly across Python→JS and JS→JS calls.
32
+
33
+ #### Calling from Python to JavaScript
34
+
35
+ When a Python client calls a JS service using keyword arguments, they are automatically unpacked into the matching positional parameters:
36
+
37
+ ```python
38
+ # Python caller
39
+ svc = await server.get_service("my-js-service")
40
+ result = await svc.greet(name="Alice", greeting="Hello")
41
+ ```
42
+
43
+ ```javascript
44
+ // JavaScript service — params are matched by name
45
+ await api.registerService({
46
+ id: "my-js-service",
47
+ greet(name, greeting) {
48
+ return `${greeting}, ${name}!`; // "Hello, Alice!"
49
+ }
50
+ });
51
+ ```
52
+
53
+ #### Calling from JavaScript to JavaScript
54
+
55
+ Use the `_rkwargs: true` flag to send keyword arguments from JS:
56
+
57
+ ```javascript
58
+ const svc = await api.getService("my-js-service");
59
+
60
+ // Positional (traditional)
61
+ await svc.greet("Alice", "Hello");
62
+
63
+ // Keyword arguments — params matched by name, order doesn't matter
64
+ await svc.greet({ greeting: "Hello", name: "Alice", _rkwargs: true });
65
+ ```
66
+
67
+ #### With `require_context`
68
+
69
+ When a service uses `require_context: true`, the `context` parameter is automatically injected by the server and cannot be overridden via kwargs:
70
+
71
+ ```javascript
72
+ await api.registerService({
73
+ id: "secure-svc",
74
+ config: { require_context: true },
75
+ greet(name, greeting, context) {
76
+ console.log("Called by:", context.user);
77
+ return `${greeting}, ${name}!`;
78
+ }
79
+ });
80
+
81
+ // Caller — context is injected automatically, not passed by the caller
82
+ await svc.greet({ name: "Alice", greeting: "Hello", _rkwargs: true });
83
+ ```
84
+
85
+ #### With `kwargs_expansion`
86
+
87
+ For convenience when calling server APIs, connect with `kwargs_expansion: true` to automatically convert the last object argument to kwargs:
88
+
89
+ ```javascript
90
+ const api = await connectToServer({
91
+ server_url: "https://ai.imjoy.io",
92
+ kwargs_expansion: true,
93
+ });
94
+ // The last object arg is automatically sent as kwargs
95
+ const token = await api.generateToken({ config: { workspace: "my-ws" } });
96
+ ```
97
+
98
+ > **Note:** Kwargs unpacking relies on parsing function parameter names from source code. This works with regular functions, arrow functions, and async functions, but will not work with minified/bundled code where parameter names are mangled.
@@ -86,7 +86,7 @@
86
86
  <div class='footer quiet pad2 space-top1 center small'>
87
87
  Code coverage generated by
88
88
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
89
- at 2026-03-10T23:48:04.149Z
89
+ at 2026-03-19T04:11:10.099Z
90
90
  </div>
91
91
  <script src="prettify.js"></script>
92
92
  <script>
@@ -3439,7 +3439,8 @@ __webpack_require__.r(__webpack_exports__);
3439
3439
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
3440
3440
  /* harmony export */ API_VERSION: () => (/* binding */ API_VERSION),
3441
3441
  /* harmony export */ RPC: () => (/* binding */ RPC),
3442
- /* harmony export */ _applyEncryptionKeyToService: () => (/* binding */ _applyEncryptionKeyToService)
3442
+ /* harmony export */ _applyEncryptionKeyToService: () => (/* binding */ _applyEncryptionKeyToService),
3443
+ /* harmony export */ getParamNames: () => (/* binding */ getParamNames)
3443
3444
  /* harmony export */ });
3444
3445
  /* harmony import */ var _utils_index_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/index.js */ "./src/utils/index.js");
3445
3446
  /* harmony import */ var _utils_schema_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/schema.js */ "./src/utils/schema.js");
@@ -3456,6 +3457,44 @@ __webpack_require__.r(__webpack_exports__);
3456
3457
 
3457
3458
 
3458
3459
 
3460
+ /**
3461
+ * Extract parameter names from a function's source text.
3462
+ * Works with regular functions, arrow functions, async functions,
3463
+ * destructured params, default values, and rest params.
3464
+ * Note: will not work with minified/bundled code where param names are mangled.
3465
+ * @param {Function} fn - The function to inspect.
3466
+ * @returns {string[]} Array of parameter names.
3467
+ */
3468
+ function getParamNames(fn) {
3469
+ const src = fn.toString();
3470
+ // Match these forms (with optional leading async):
3471
+ // function(a, b) – anonymous function
3472
+ // function name(a, b) – named function
3473
+ // (a, b) => – arrow function
3474
+ // a => – single-param arrow (no parens)
3475
+ // name(a, b) { – shorthand method (object literal / class)
3476
+ const match = src.match(
3477
+ /^(?:async\s+)?(?:function\s*\w*)?\s*\(([^)]*)\)|^(?:async\s+)?(\w+)\s*=>|^(?:async\s+)?\w+\s*\(([^)]*)\)/,
3478
+ );
3479
+ if (!match) return [];
3480
+ const paramStr =
3481
+ match[1] !== undefined
3482
+ ? match[1]
3483
+ : match[2] !== undefined
3484
+ ? match[2]
3485
+ : match[3];
3486
+ if (!paramStr || !paramStr.trim()) return [];
3487
+ return paramStr
3488
+ .split(",")
3489
+ .map((p) =>
3490
+ p
3491
+ .trim()
3492
+ .replace(/\s*=.*$/, "") // strip default values
3493
+ .replace(/^\.\.\.\s*/, ""), // strip rest operator
3494
+ )
3495
+ .filter(Boolean);
3496
+ }
3497
+
3459
3498
  /**
3460
3499
  * Apply an out-of-band encryption public key to all remote methods in a service.
3461
3500
  * @param {Object} svc - The decoded service object.
@@ -6404,6 +6443,24 @@ class RPC extends _utils_index_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
6404
6443
  } else {
6405
6444
  args = [];
6406
6445
  }
6446
+
6447
+ // Unpack kwargs into positional arguments when with_kwargs is set.
6448
+ // This mirrors Python's _handle_method which pops the last arg as
6449
+ // a kwargs dict and passes it via **kwargs. Since JS doesn't have
6450
+ // **kwargs, we map the dict keys to the function's parameter names.
6451
+ if (data.with_kwargs && args.length > 0) {
6452
+ const kwargs = args.pop();
6453
+ if (typeof kwargs === "object" && kwargs !== null) {
6454
+ const paramNames = getParamNames(method);
6455
+ // Filter out 'context' — it's handled separately by require_context
6456
+ const mappableParams = paramNames.filter((n) => n !== "context");
6457
+ args = mappableParams.map((name) => kwargs[name]);
6458
+ } else {
6459
+ // Not a dict — push it back (shouldn't happen, but be safe)
6460
+ args.push(kwargs);
6461
+ }
6462
+ }
6463
+
6407
6464
  if (
6408
6465
  this._method_annotations.has(method) &&
6409
6466
  this._method_annotations.get(method).require_context