@adhd/apigen-python-env 0.2.3 โ 0.2.5
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/CHANGELOG.md +16 -0
- package/index.js +4 -4
- package/index.mjs +3 -3
- package/lib/python-env.d.ts +21 -11
- package/package.json +12 -2
- package/python/apigen_python/flask_server.py +30 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
## 0.2.5 (2026-09-24)
|
|
2
|
+
|
|
3
|
+
This was a version bump only for apigen-python-env to align it with other projects, there were no code changes.
|
|
4
|
+
|
|
5
|
+
## 0.2.4 (2026-08-08)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### ๐ฉน Fixes
|
|
9
|
+
|
|
10
|
+
- **apigen-python-env:** probe monorepo-live python source before co-located dist copy (DEBT-APIGEN-010)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### โค๏ธ Thank You
|
|
14
|
+
|
|
15
|
+
- pseudosky
|
|
16
|
+
|
|
1
17
|
## 0.2.3 (2026-08-07)
|
|
2
18
|
|
|
3
19
|
|
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const D=require("node:fs"),I=require("node:os"),q=require("node:path"),F=require("node:crypto"),h=require("node:child_process");function P(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const
|
|
2
|
-
${j.stderr}`);const x=o.join(e,"build");
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const D=require("node:fs"),I=require("node:os"),q=require("node:path"),F=require("node:crypto"),h=require("node:child_process");function P(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const n in t)if(n!=="default"){const i=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,i.get?i:{enumerable:!0,get:()=>t[n]})}}return e.default=t,Object.freeze(e)}const r=P(D),b=P(I),o=P(q);function k(t){const e=t??__dirname,n=o.resolve(e,"..","..","..","python");if(r.existsSync(o.join(n,"apigen_python")))return n;const i=o.join(e,"python");if(r.existsSync(o.join(i,"apigen_python")))return i;let a=e;for(let c=0;c<20;c++){const p=o.join(a,"packages","apigen","python");if(r.existsSync(o.join(p,"apigen_python")))return p;const u=o.dirname(a);if(u===a)break;a=u}return n}const H=["python3.13","python3.12","python3.11","python3"],m=[3,11];function J(t){const e=h.spawnSync(t,["-c",'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")'],{encoding:"utf8",timeout:15e3});if(e.status!==0||!e.stdout)return;const[n,i]=e.stdout.trim().split(".").map(Number);if(!(!Number.isFinite(n)||!Number.isFinite(i)))return[n,i]}function M(){for(const t of H){const e=J(t);if(e&&(e[0]>m[0]||e[0]===m[0]&&e[1]>=m[1]))return t}throw new Error(`apigen-python-env: no Python >= ${m.join(".")} found on PATH (tried: ${H.join(", ")}). Install Python ${m.join(".")}+ or set APIGEN_PYTHON to a provisioned interpreter.`)}function Y(t){return process.platform==="win32"?o.join(t,"Scripts","python.exe"):o.join(t,"bin","python3")}function G(t){const e=r.readFileSync(o.join(t,"pyproject.toml"));return F.createHash("sha256").update(e).digest("hex")}function O(t){try{const e=JSON.parse(r.readFileSync(t,"utf8"));if(typeof e.pyprojectHash=="string"&&Array.isArray(e.extras))return e}catch{}}function B(t,e,n){const i=Date.now()+e;for(;;)try{r.mkdirSync(t,{recursive:!1});break}catch{if(Date.now()>i){try{r.rmdirSync(t)}catch{}continue}h.spawnSync(process.execPath,["-e","setTimeout(()=>{}, 250)"],{timeout:5e3})}try{return n()}finally{try{r.rmdirSync(t)}catch{}}}function V(t={}){const e=k(),n=process.env.APIGEN_PYTHON;if(n){if(!r.existsSync(n))throw new Error(`apigen-python-env: APIGEN_PYTHON points to a missing file: ${n}`);return{python:n,venvDir:"",pythonPkgDir:e}}const i=[...new Set(t.extras??[])].sort(),a=t.envHome??process.env.APIGEN_PYENV_HOME??o.join(b.homedir(),".adhd","apigen"),c=o.join(a,"pyvenv"),p=o.join(c,".apigen-stamp"),u=G(e),y=Y(c),l=()=>{if(!r.existsSync(y))return!1;const s=O(p);return!s||s.pyprojectHash!==u?!1:i.every(d=>s.extras.includes(d))};if(l())return{python:y,venvDir:c,pythonPkgDir:e};r.mkdirSync(a,{recursive:!0});const v=o.join(a,"pyvenv.lock"),w=t.lockTimeoutMs??3e5;return B(v,w,()=>{if(l())return{python:y,venvDir:c,pythonPkgDir:e};const s=O(p),d=[...new Set([...(s==null?void 0:s.extras)??[],...i])].sort();r.rmSync(c,{recursive:!0,force:!0});const _=M(),j=h.spawnSync(_,["-m","venv",c],{encoding:"utf8",timeout:12e4});if(j.status!==0)throw new Error(`apigen-python-env: "${_} -m venv" failed (exit ${j.status}):
|
|
2
|
+
${j.stderr}`);const x=o.join(e,"build");r.existsSync(x)&&r.rmSync(x,{recursive:!0,force:!0});for(const f of r.readdirSync(e))f.endsWith(".egg-info")&&r.rmSync(o.join(e,f),{recursive:!0,force:!0});const S=r.mkdtempSync(o.join(b.tmpdir(),"apigen-python-wheel-"));try{const f=h.spawnSync(y,["-m","pip","wheel","--no-deps","--wheel-dir",S,e],{encoding:"utf8",timeout:6e5});if(f.status!==0)throw new Error(`apigen-python-env: pip wheel failed (exit ${f.status}):
|
|
3
3
|
${f.stderr}
|
|
4
4
|
stdout:
|
|
5
|
-
${f.stdout}`);const E=
|
|
5
|
+
${f.stdout}`);const E=r.readdirSync(S).filter(A=>A.endsWith(".whl"));if(E.length===0)throw new Error("apigen-python-env: pip wheel produced no .whl file");const $=o.join(S,E[0]),N=d.length?`${$}[${d.join(",")}]`:$,g=h.spawnSync(y,["-m","pip","install","--quiet","--disable-pip-version-check",N],{encoding:"utf8",timeout:6e5});if(g.status!==0)throw new Error(`apigen-python-env: pip install "${N}" failed (exit ${g.status}):
|
|
6
6
|
${g.stderr}
|
|
7
7
|
stdout:
|
|
8
|
-
${g.stdout}`)}finally{
|
|
8
|
+
${g.stdout}`)}finally{r.rmSync(S,{recursive:!0,force:!0})}const T={pyprojectHash:u,extras:d};return r.writeFileSync(p,JSON.stringify(T)),{python:y,venvDir:c,pythonPkgDir:e}})}function L(t,e,n,i,a){return new Promise((c,p)=>{var v,w;const u=h.spawn(t,["-m","apigen_python.extractor",n,"--namespace",i,"--emit-json"],{cwd:e,env:{...process.env,PYTHONPATH:e},stdio:["ignore","pipe","pipe"]});let y="",l="";(v=u.stdout)==null||v.on("data",s=>{y+=s.toString("utf8")}),(w=u.stderr)==null||w.on("data",s=>{l+=s.toString("utf8")}),u.on("error",p),u.on("exit",s=>{if(s!==0){p(new Error(`${a}: extractor --emit-json exited with code ${s}:
|
|
9
9
|
${l}`));return}try{c(JSON.parse(y))}catch(d){p(new Error(`${a}: extractor --emit-json produced invalid JSON (${d.message}):
|
|
10
10
|
${y}`))}})})}exports.ensurePythonEnv=V;exports.resolvePythonPkgDir=k;exports.runExtractorEmitJson=L;
|
package/index.mjs
CHANGED
|
@@ -4,10 +4,10 @@ import * as r from "node:path";
|
|
|
4
4
|
import { createHash as T } from "node:crypto";
|
|
5
5
|
import { spawnSync as l, spawn as I } from "node:child_process";
|
|
6
6
|
function O(e) {
|
|
7
|
-
const t = e ?? __dirname, o = r.
|
|
7
|
+
const t = e ?? __dirname, o = r.resolve(t, "..", "..", "..", "python");
|
|
8
8
|
if (n.existsSync(r.join(o, "apigen_python")))
|
|
9
9
|
return o;
|
|
10
|
-
const s = r.
|
|
10
|
+
const s = r.join(t, "python");
|
|
11
11
|
if (n.existsSync(r.join(s, "apigen_python")))
|
|
12
12
|
return s;
|
|
13
13
|
let a = t;
|
|
@@ -20,7 +20,7 @@ function O(e) {
|
|
|
20
20
|
break;
|
|
21
21
|
a = u;
|
|
22
22
|
}
|
|
23
|
-
return
|
|
23
|
+
return o;
|
|
24
24
|
}
|
|
25
25
|
const _ = ["python3.13", "python3.12", "python3.11", "python3"], m = [3, 11];
|
|
26
26
|
function D(e) {
|
package/lib/python-env.d.ts
CHANGED
|
@@ -27,17 +27,27 @@ export interface IEnsurePythonEnvOptions {
|
|
|
27
27
|
* Locate the directory containing the `apigen_python` package sources.
|
|
28
28
|
*
|
|
29
29
|
* Probed in order:
|
|
30
|
-
* 1.
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
30
|
+
* 1. Monorepo source (LIVE, preferred when present): `<this file>/../../
|
|
31
|
+
* ../python` (source layout: packages/apigen/python-env/src/lib โ
|
|
32
|
+
* packages/apigen/python; from a build, `dist/lib` โ `packages/apigen/
|
|
33
|
+
* python` by the same three-levels-up relationship). When both this
|
|
34
|
+
* AND the co-located dist copy below exist โ i.e. running inside the
|
|
35
|
+
* monorepo, built or not โ this MUST win. `dist/python` is a
|
|
36
|
+
* point-in-time copy made by this package's build (vite.config.ts
|
|
37
|
+
* `copyPythonPackagePlugin`); it goes stale the moment someone edits
|
|
38
|
+
* `packages/apigen/python` without rebuilding, and code run outside
|
|
39
|
+
* Nx's task graph (`npx vitest run` directly, bypassing the `test`
|
|
40
|
+
* target's `dependsOn: ["build"]`) has no mechanism to refresh it. Prior
|
|
41
|
+
* ordering probed the dist copy FIRST, so any monorepo dev/test
|
|
42
|
+
* invocation that happened to have a `dist/python` lying around from a
|
|
43
|
+
* previous build silently exercised stale Python source with no
|
|
44
|
+
* indication the live edit was never observed โ see
|
|
45
|
+
* DEBT-APIGEN-010. Source must win whenever it's reachable.
|
|
46
|
+
* 2. Co-located (SHIPPED) copy: `<this file's dir>/python`. For a
|
|
47
|
+
* consumer installed from npm OUTSIDE the monorepo (where __dirname is
|
|
48
|
+
* `node_modules/@adhd/apigen-python-env/dist`), step 1's monorepo-
|
|
49
|
+
* relative path doesn't exist, so this bundled-alongside-the-compiled-
|
|
50
|
+
* code copy is the only candidate and is used.
|
|
41
51
|
* 3. Walk up from __dirname looking for `packages/apigen/python` (covers
|
|
42
52
|
* the vite-bundled CLI, where __dirname is the bundle output dir).
|
|
43
53
|
* (Shared by py-grpc / py-flask โ previously duplicated in each plugin.)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adhd/apigen-python-env",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"module": "./index.mjs",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -12,9 +12,19 @@
|
|
|
12
12
|
"provisioning"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@adhd/apigen-core-client": "^0.3.
|
|
15
|
+
"@adhd/apigen-core-client": "^0.3.1"
|
|
16
16
|
},
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/PseudoSky/adhd.git",
|
|
24
|
+
"directory": "packages/apigen/python-env"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/PseudoSky/adhd/tree/main/packages/apigen/python-env#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/PseudoSky/adhd/issues"
|
|
19
29
|
}
|
|
20
30
|
}
|
|
@@ -434,11 +434,12 @@ def _resolve_batch_concurrency(mode: str, requested: Any, item_count: int) -> in
|
|
|
434
434
|
def _dispatch_batch(
|
|
435
435
|
state: _ServerState, body: dict[str, Any], envelope: dict[str, Any]
|
|
436
436
|
) -> list[dict[str, Any]]:
|
|
437
|
-
"""Fan out `body['items']` to the real operation
|
|
438
|
-
the Python host's OWN executor against
|
|
439
|
-
(LEFT column only; this function is
|
|
440
|
-
`invokeBatch`). Implements the
|
|
441
|
-
(`tmp/apigen-batch-python-design.md`
|
|
437
|
+
"""Fan out `body['input']['items']` to the real operation
|
|
438
|
+
`body['input']['operation']` โ the Python host's OWN executor against
|
|
439
|
+
BATCH_0.0.1.md ยง5's wire contract (LEFT column only; this function is
|
|
440
|
+
never a call into the TS `invokeBatch`). Implements the
|
|
441
|
+
architect-reviewed design's binding fixes (`tmp/apigen-batch-python-design.md`
|
|
442
|
+
ยง3.5):
|
|
442
443
|
|
|
443
444
|
- F2: `mode` branching โ `parallel` uses a bounded `ThreadPoolExecutor`;
|
|
444
445
|
`serial`/`chained` use a plain sequential loop; `chained` additionally
|
|
@@ -455,13 +456,32 @@ def _dispatch_batch(
|
|
|
455
456
|
- F5: every rejected result's `reason` is a real `ApiError(...).to_json()`
|
|
456
457
|
using an existing `errors.py` taxonomy code.
|
|
457
458
|
|
|
459
|
+
BUG-APIGEN-CLI-002: every batch control-plane field (`operation`,
|
|
460
|
+
`items`, `concurrency`, `mode`, `onItemError`, `itemTimeoutMs`) now nests
|
|
461
|
+
under one top-level `input` object on the wire โ `batch.ts`'s
|
|
462
|
+
`branchInputSchema` matches every other apigen-mounted operation's
|
|
463
|
+
single-JSON-blob convention. `@adhd/apigen-plugin-batch`'s
|
|
464
|
+
`parseBatchRequest` already unwraps this one extra level (its own doc
|
|
465
|
+
comment: "Unwrap that one extra level here โ the only call site that
|
|
466
|
+
reads this shape."); this Python host is a SECOND call site reading the
|
|
467
|
+
same wire shape and must unwrap identically, never operate on the raw
|
|
468
|
+
top-level `body`.
|
|
469
|
+
|
|
458
470
|
Raises:
|
|
459
|
-
ApiError: for whole-request-level rejections (
|
|
460
|
-
unsupported `itemTimeoutMs`, malformed
|
|
461
|
-
`onItemError`/`concurrency`) โ the caller maps
|
|
462
|
-
HTTP 4xx via `_send_error`, the SAME mechanism
|
|
463
|
-
on this server already uses.
|
|
471
|
+
ApiError: for whole-request-level rejections (missing/malformed
|
|
472
|
+
`input`, bad `operation`, unsupported `itemTimeoutMs`, malformed
|
|
473
|
+
`items`/`mode`/`onItemError`/`concurrency`) โ the caller maps
|
|
474
|
+
this to a real HTTP 4xx via `_send_error`, the SAME mechanism
|
|
475
|
+
every other route on this server already uses.
|
|
464
476
|
"""
|
|
477
|
+
input_raw = body.get("input")
|
|
478
|
+
if not isinstance(input_raw, dict):
|
|
479
|
+
raise ApiError(
|
|
480
|
+
"invalid_argument",
|
|
481
|
+
'"input" must be an object containing {operation, items, ...}',
|
|
482
|
+
)
|
|
483
|
+
body = input_raw
|
|
484
|
+
|
|
465
485
|
# F3 โ reject the WHOLE batch upfront; never a silently-weaker
|
|
466
486
|
# wait-only guarantee under the same field name TS uses for real
|
|
467
487
|
# cancellation.
|