pi-ptc-subagents 5.0.0 → 6.0.0

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/dist/worker.js CHANGED
@@ -1,4 +1,158 @@
1
- import{a as e,c as t}from"./protocol-CfOgxz3u.js";import{stripTypeScriptTypes as n}from"node:module";import{parentPort as r,workerData as i}from"node:worker_threads";import{inspect as a}from"node:util";function o(t){let n=t.protocol.hostFrame,r=t.protocol.workerFrame,i=t.protocol.logLevel,a=t.protocol.errorKind,o=`__ptcProgram`,s=`CREATED`,c=0,l,u,d=1,f=0,p=1/0,m=[],h=/* @__PURE__ */ new Map,g,_=()=>globalThis;class v extends Error{toolName;constructor(e,t){super(t),this.name=`ToolCallError`,this.toolName=e}}let y=e=>e instanceof Error?e.message:String(e),b=e=>{if(!(e instanceof Error)||typeof e.stack!=`string`)return;let t=e.stack.split(`
2
- `),n=[t[0]??``];for(let e of t.slice(1))if(n.push(e),n.length>=6)break;return n.join(`
3
- `)},x=(e,t=l)=>{if(!t)return!1;try{return t.postMessage(e),!0}catch{return!1}},S=(e,t,n)=>{x({kind:r.log,level:e,text:t},n)},C=(e,t,n,i)=>{x(n===void 0?{kind:r.error,error:{kind:e,message:t}}:{kind:r.error,error:{kind:e,message:t,stack:n}},i)},w=e=>{let t=/* @__PURE__ */ Error(e===`timeout`?`PTC run timed out`:`PTC run was cancelled`);return t.name=`AbortError`,t},T=()=>{let e=/* @__PURE__ */ Error(`the PTC control channel closed before the call completed`);return e.name=`AbortError`,e},E=async()=>{if(f<p){f+=1;return}await new Promise((e,t)=>{m.push({resolve:e,reject:t})})},D=()=>{let e=m.shift();if(e){e.resolve();return}--f},O=e=>{for(;m.length>0;){let t=m.shift();t&&t.reject(e)}},k=e=>{O(e);for(let[t,n]of h)h.delete(t),n.reject(e)},A=()=>{let e=t.workerData;if(typeof e!=`object`||!e)return;let n=e.env;if(typeof n!=`object`||!n||Array.isArray(n))return;let r=n;for(let e of Object.keys(process.env))e in r||delete process.env[e];for(let e of Object.keys(r)){let t=r[e];typeof t==`string`&&(process.env[e]=t)}},j=t=>{let n=/* @__PURE__ */ new Set,r=(t,i)=>{if(t===null)return{ok:!0,value:null};if(typeof t==`string`||typeof t==`boolean`)return{ok:!0,value:t};if(t===void 0)return{ok:!0,value:void 0};if(typeof t==`number`)return Number.isFinite(t)?{ok:!0,value:t}:{ok:!1,reason:`${i} is ${String(t)}, which is not representable as JSON`};if(typeof t!=`object`)return{ok:!1,reason:`${i} is ${e(t)}; results must be lossless JSON`};if(Array.isArray(t)){if(n.has(t))return{ok:!1,reason:`${i} is a circular reference`};n.add(t);let e=[];for(let n=0;n<t.length;n+=1){let a=r(t[n],`${i}[${n}]`);if(!a.ok)return a;e.push(a.value===void 0?null:a.value)}return n.delete(t),{ok:!0,value:e}}let a=Object.getPrototypeOf(t);if(a!==Object.prototype&&a!==null){let e=t.constructor;return{ok:!1,reason:`${i} is a ${e&&typeof e.name==`string`&&e.name.length>0?e.name:`an object`}; results must be plain JSON objects`}}if(n.has(t))return{ok:!1,reason:`${i} is a circular reference`};n.add(t);let o={};for(let e of Object.keys(t)){let n=r(t[e],`${i}.${e}`);if(!n.ok)return n;n.value!==void 0&&(o[e]=n.value)}return n.delete(t),{ok:!0,value:o}};return r(t,`result`)},M=e=>e.map(e=>typeof e==`string`?e:t.inspect(e,{depth:4,breakLength:1/0,colors:!1})).join(` `),N=e=>{let t=t=>(...n)=>{S(t,M(n),e)};_().console={log:t(i.log),info:t(i.info),warn:t(i.warn),error:t(i.error),debug:t(i.debug)}},P=()=>{typeof process.removeAllListeners==`function`&&typeof process.on==`function`&&(process.removeAllListeners(`warning`),process.on(`warning`,e=>{let t=typeof e==`object`&&e?e:void 0,n=t&&typeof t.name==`string`?t.name:`Warning`;if(n===`ExperimentalWarning`)return;let r=t&&typeof t.message==`string`?t.message:String(e);S(i.warn,`${n}: ${r}`)}))},F=e=>{let n=`async function ${o}() {\n${e}\n}`,r=n;try{r=t.stripTypes(n,{mode:`strip`})}catch{r=n}return Function(`${r}\nreturn ${o}();`)},I=async(e,t,n)=>{try{let t;try{t=F(e)}catch(e){C(a.exception,`program failed to compile: ${y(e)}`,b(e),n);return}try{let e=await t(),i=j(e);if(!i.ok){C(a.invalidOutput,i.reason,void 0,n);return}x(i.value===void 0?{kind:r.result}:{kind:r.result,value:i.value},n)}catch(e){C(u??a.exception,y(e),b(e),n)}}finally{await W(t,n)}},L=(e,t)=>async n=>{if(u)throw w(u);await E();try{if(u)throw w(u);let i=d;d+=1;let a=new Promise((e,t)=>{h.set(i,{resolve:e,reject:t})});if(!x({kind:r.call,callId:i,tool:e,args:n},t))throw h.delete(i),new v(e,`${e}() could not be called: the arguments are not transferable or the run has ended`);return await a}finally{D()}},R=e=>{let t=e.callId;if(typeof t!=`number`)return;let n=h.get(t);if(n){if(h.delete(t),e.ok===!0){n.resolve(e.value);return}n.reject(new v(typeof e.tool==`string`?e.tool:`unknown`,typeof e.message==`string`?e.message:`binding call failed`))}},z=e=>{u=e,k(w(e))},B=(e,t)=>{u=void 0;let n=w(e);C(e,n.message,void 0,t)},V=(e,t,n)=>{if(t>n)throw RangeError(`${e}() received ${t} items, exceeding maxItemsPerCall (${n})`)},H=(t,n,i)=>{let a=_();a.args=t===void 0?null:t,a.log=t=>{if(typeof t!=`string`)throw TypeError(`log(message) expects a string, received ${e(t)}`);x({kind:r.narration,message:t},i)},a.phase=t=>{if(typeof t!=`string`)throw TypeError(`phase(title) expects a string, received ${e(t)}`);x({kind:r.phase,title:t},i)},a.parallel=async t=>{if(!Array.isArray(t))throw TypeError(`parallel(thunks) expects an array of functions, received ${e(t)}`);V(`parallel`,t.length,n);for(let n=0;n<t.length;n+=1)if(typeof t[n]!=`function`)throw TypeError(`parallel(thunks) expects functions; item ${n} is ${e(t[n])}`);return await Promise.all(t.map(async e=>{try{return await e()}catch{return null}}))},a.pipeline=async(t,...r)=>{if(!Array.isArray(t))throw TypeError(`pipeline(items, ...stages) expects an array of items, received ${e(t)}`);if(V(`pipeline`,t.length,n),r.length===0)throw TypeError(`pipeline(items, ...stages) requires at least one stage function`);for(let t=0;t<r.length;t+=1)if(typeof r[t]!=`function`)throw TypeError(`pipeline(items, ...stages) expects stage functions; stage ${t} is ${e(r[t])}`);let i=r;return await Promise.all(t.map(async(e,t)=>{try{let n=e;for(let r of i)n=await r(n,e,t);return n}catch{return null}}))}},U=()=>{if(!g)return;let e=_();for(let t of Object.keys(e))g.has(t)||delete e[t];for(let[t,n]of g){let r=Object.getOwnPropertyDescriptor(e,t);if(r===void 0||r.writable!==!1)try{e[t]=n}catch{}}},W=async(e,t)=>{s===`RUNNING`&&e===c&&(k(T()),f=0,u=void 0,d=1,h.clear(),U(),A(),s=`READY`,x({kind:r.ready},t))},G=(e,t,n)=>{U(),u=void 0,f=0,h.clear(),A(),N(n),p=typeof e.maxPendingCalls==`number`&&Number.isFinite(e.maxPendingCalls)&&e.maxPendingCalls>0?e.maxPendingCalls:1/0;let r={},i=Array.isArray(e.bindings)?e.bindings.filter(e=>typeof e==`string`):[];for(let e of i)r[e]=L(e,n);let a=i.join(`, `)||`(none)`,o=Array.isArray(e.bindingCandidates)?e.bindingCandidates:[];for(let e of o)typeof e!=`string`||r[e]||(r[e]=async()=>{throw new v(e,`no binding named "${e}" in this run; available bindings: ${a}`)});if(_().tools=r,_().ptcBindings=Object.freeze([...i]),e.surface===`workflow`){let t=typeof e.maxItemsPerCall==`number`?e.maxItemsPerCall:0;H(e.args,t,n)}I(typeof e.code==`string`?e.code:``,t,n)},K=(e,t)=>{if(typeof e!=`object`||!e||Array.isArray(e))return;let r=e;if(r.kind===n.init){if(s!==`READY`)return;c+=1;let e=c;s=`RUNNING`,G(r,e,t);return}if(r.kind===n.callResult){R(r);return}if(r.kind===n.cancel){let e=r.reason===`timeout`?`timeout`:`abort`;if(s===`READY`){B(e,t);return}z(e)}};s=`BOOTING`,t.parentPort.on(`message`,e=>{if(typeof e!=`object`||!e)return;let t=e;if(t.kind!==n.connect)return;let i=t.port;if(typeof i!=`object`||!i||typeof i.on!=`function`)return;let a=i;l=a,a.on(`message`,e=>{K(e,a)}),a.on(`close`,()=>{l===a&&k(T())}),P();let o=g===void 0;if(o&&(g=new Map(Object.entries(_()))),s=`READY`,u!==void 0){if(o){B(u,a);return}u=void 0}x({kind:r.ready},a)})}o({parentPort:r,workerData:i,inspect:a,stripTypes:n,protocol:t()});export{};
4
- //# sourceMappingURL=worker.js.map
1
+ import { stripTypeScriptTypes } from "node:module";
2
+ import { parentPort, workerData } from "node:worker_threads";
3
+ import { inspect } from "node:util";
4
+ // ../ptc/src/compile.ts
5
+ function compileProgram(source) {
6
+ const stripped = stripTypeScriptTypes(`async function __ptcProgram() {\n${source}\n}`, { mode: "strip" });
7
+ return new Function(`"use strict";\n${stripped}\nreturn __ptcProgram;`)();
8
+ }
9
+ // ../ptc/src/workflow-helpers.ts
10
+ function createWorkflowHelpers(emit) {
11
+ return {
12
+ log: (...args) => emit(args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ")),
13
+ phase: (name) => emit(`— phase: ${name} —`),
14
+ parallel: (items) => Promise.all(items.map((run) => run())),
15
+ pipeline: (items, step) => {
16
+ const results = [];
17
+ return items.reduce((chain, item, index) => chain.then(async () => {
18
+ results.push(await step(item, index));
19
+ }), Promise.resolve()).then(() => results);
20
+ }
21
+ };
22
+ }
23
+ // ../ptc/src/worker-main.ts
24
+ function workerMain(deps) {
25
+ let tools = {};
26
+ let pending = /* @__PURE__ */ new Map();
27
+ let nextCallId = 1;
28
+ let currentRunId = null;
29
+ let logs = [];
30
+ const globalBaseline = /* @__PURE__ */ new Set();
31
+ const consoleBaseline = { ...console };
32
+ deps.onFrame((frame) => {
33
+ if (frame.kind === "init") {
34
+ for (const name of Object.getOwnPropertyNames(globalThis)) globalBaseline.add(name);
35
+ tools = Object.freeze(Object.fromEntries(frame.bindingNames.map((name) => [name, (args) => {
36
+ if (currentRunId === null) return Promise.reject(/* @__PURE__ */ new Error("run 已结束:reset 后不再接受工具调用"));
37
+ const call = new Promise((resolve, reject) => {
38
+ const callId = nextCallId++;
39
+ pending.set(callId, {
40
+ resolve,
41
+ reject
42
+ });
43
+ deps.post({
44
+ kind: "call",
45
+ runId: currentRunId ?? "",
46
+ callId,
47
+ name,
48
+ args
49
+ });
50
+ });
51
+ call.catch(() => {});
52
+ return call;
53
+ }])));
54
+ defineGlobal("tools", tools);
55
+ if (frame.surface === "workflow") defineGlobal("workflow", createWorkflowHelpers((text) => {
56
+ logs.push(text);
57
+ deps.post({
58
+ kind: "log",
59
+ runId: currentRunId ?? "",
60
+ level: "log",
61
+ text
62
+ });
63
+ }));
64
+ deps.post({ kind: "ready" });
65
+ return;
66
+ }
67
+ if (frame.kind === "reset") {
68
+ currentRunId = null;
69
+ for (const { reject } of pending.values()) reject(/* @__PURE__ */ new Error("run reset:worker 被复用"));
70
+ pending = /* @__PURE__ */ new Map();
71
+ for (const name of Object.getOwnPropertyNames(globalThis)) if (!globalBaseline.has(name)) delete globalThis[name];
72
+ Object.assign(console, consoleBaseline);
73
+ deps.post({ kind: "ready" });
74
+ return;
75
+ }
76
+ if (frame.kind === "result") {
77
+ const entry = pending.get(frame.callId);
78
+ if (!entry) return;
79
+ pending.delete(frame.callId);
80
+ if (frame.ok) entry.resolve(frame.value);
81
+ else entry.reject(new Error(frame.error));
82
+ return;
83
+ }
84
+ if (frame.kind === "cancel") return;
85
+ run(frame.code, frame.id, frame.args);
86
+ });
87
+ function defineGlobal(name, value) {
88
+ Object.defineProperty(globalThis, name, {
89
+ value,
90
+ writable: false,
91
+ configurable: true
92
+ });
93
+ }
94
+ async function run(code, runId, args) {
95
+ currentRunId = runId;
96
+ logs = [];
97
+ defineGlobal("args", args ?? {});
98
+ installConsole((level, text) => {
99
+ logs.push(text);
100
+ deps.post({
101
+ kind: "log",
102
+ runId,
103
+ level,
104
+ text
105
+ });
106
+ });
107
+ const outcome = await execute(code);
108
+ deps.post({
109
+ kind: "settled",
110
+ runId,
111
+ outcome
112
+ });
113
+ }
114
+ async function execute(code) {
115
+ try {
116
+ const value = await compileProgram(code).call({ tools });
117
+ if (value === void 0) return {
118
+ status: "fulfilled",
119
+ value: { $void: true },
120
+ logs
121
+ };
122
+ const json = JSON.stringify(value);
123
+ if (json === void 0) return {
124
+ status: "rejected",
125
+ error: "程序返回值不可序列化",
126
+ logs
127
+ };
128
+ return {
129
+ status: "fulfilled",
130
+ value: JSON.parse(json),
131
+ logs
132
+ };
133
+ } catch (error) {
134
+ return {
135
+ status: "rejected",
136
+ error: error instanceof Error ? error.message : String(error),
137
+ logs
138
+ };
139
+ }
140
+ }
141
+ }
142
+ function installConsole(emit) {
143
+ for (const level of [
144
+ "log",
145
+ "info",
146
+ "warn",
147
+ "error",
148
+ "debug"
149
+ ]) console[level] = (...args) => emit(level, args.map((a) => typeof a === "string" ? a : inspect(a)).join(" "));
150
+ }
151
+ // ../ptc/src/worker-entry.ts
152
+ if (!parentPort) throw new Error("worker-entry 只能在 worker 线程里加载");
153
+ Object.freeze(workerData);
154
+ workerMain({
155
+ post: (frame) => parentPort.postMessage(frame),
156
+ onFrame: (handler) => parentPort.on("message", (frame) => handler(frame))
157
+ });
158
+ export {};
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "pi-ptc-subagents",
3
- "version": "5.0.0",
4
- "description": "Programmable tool calling (PTC) and subagent fan-out for pi — the model writes JS/TS programs that call pi's tools, or fans out to a fresh pi subprocess per subagent task",
3
+ "version": "6.0.0",
4
+ "description": "Programmable tool calling (PTC) and subagent fan-out for pi",
5
5
  "keywords": [
6
6
  "pi",
7
7
  "pi-package",
8
- "programmatic-tool-calling",
9
- "ptc"
8
+ "programmable-tool-calling",
9
+ "ptc",
10
+ "subagents"
10
11
  ],
11
- "homepage": "https://a1121611810.github.io/pi-ptc-subagents/",
12
+ "homepage": "https://github.com/a1121611810/pi-ptc-subagents",
12
13
  "bugs": {
13
14
  "url": "https://github.com/a1121611810/pi-ptc-subagents/issues"
14
15
  },
@@ -19,78 +20,42 @@
19
20
  },
20
21
  "files": [
21
22
  "dist/**/*.js",
22
- "dist/**/*.d.ts",
23
23
  "README.md",
24
- "LICENSE",
25
- "THIRD_PARTY_NOTICES.md",
26
- "CHANGELOG.md"
24
+ "LICENSE"
27
25
  ],
28
26
  "type": "module",
29
27
  "main": "./dist/index.js",
30
- "types": "./dist/index.d.ts",
31
28
  "exports": {
32
- ".": {
33
- "types": "./dist/index.d.ts",
34
- "import": "./dist/index.js"
35
- }
29
+ ".": "./dist/index.js"
36
30
  },
37
31
  "scripts": {
38
- "build": "vp pack",
39
- "typecheck": "pnpm exec tsc --noEmit",
32
+ "build": "pnpm -r run build",
33
+ "prepack": "pnpm run build && rm -rf dist && cp -R packages/entry/dist dist",
34
+ "check": "pnpm -r run check",
40
35
  "lint": "oxlint",
41
- "lint:fix": "oxlint --fix",
42
36
  "fmt": "oxfmt",
43
- "fmt:check": "oxfmt --check",
44
- "compat:pi": "node scripts/pi-compat-matrix.mjs",
45
- "test": "pnpm exec vp test --run --coverage",
46
- "test:watch": "pnpm exec vp test",
47
- "test:ui": "pnpm exec vp test --ui",
48
- "verify:dist": "node scripts/verify-dist-render.mjs",
49
- "prepublishOnly": "npm run typecheck && npm run lint && npm run fmt:check && npm run build && npm run test && npm run verify:dist"
50
- },
51
- "dependencies": {
52
- "minimatch": "^10.2.6"
37
+ "fmt:check": "oxfmt --check"
53
38
  },
54
39
  "devDependencies": {
55
- "@earendil-works/pi-ai": "^1.0.0",
56
- "@earendil-works/pi-coding-agent": "^1.0.0",
57
- "@earendil-works/pi-tui": "^1.0.0",
58
- "@types/node": "^22.0.0",
59
- "@vitest/coverage-v8": "4.1.11",
60
- "oxfmt": "^0.70.0",
61
- "oxlint": "^1.85.0",
62
- "oxlint-tsgolint": "^7.0.2002",
63
- "rolldown": "^1.2.9",
64
- "rolldown-plugin-dts": "^0.28.6",
65
- "typebox": "^1.3.34",
66
- "typescript": "^7.0.2",
67
- "vite-plus": "0.3.3",
68
- "vitest": "^4.1.11"
40
+ "@earendil-works/pi-coding-agent": "^1.4.0",
41
+ "@types/node": "^26.6.5",
42
+ "oxfmt": "^0.74.0",
43
+ "oxlint": "^1.89.0",
44
+ "rolldown": "^1.2.15",
45
+ "tsrs": "0.18.0-ts7.1.0-dev.20260929"
69
46
  },
70
47
  "peerDependencies": {
71
- "@earendil-works/pi-ai": "*",
72
- "@earendil-works/pi-coding-agent": ">=0.86.0",
73
- "@earendil-works/pi-tui": "*",
74
- "typebox": "*"
48
+ "@earendil-works/pi-coding-agent": ">=0.86.0"
75
49
  },
76
50
  "peerDependenciesMeta": {
77
51
  "@earendil-works/pi-coding-agent": {
78
52
  "optional": true
79
- },
80
- "@earendil-works/pi-tui": {
81
- "optional": true
82
- },
83
- "@earendil-works/pi-ai": {
84
- "optional": true
85
- },
86
- "typebox": {
87
- "optional": true
88
53
  }
89
54
  },
90
55
  "engines": {
91
56
  "node": ">=22.19.0"
92
57
  },
93
- "packageManager": "pnpm@12.5.1",
58
+ "packageManager": "pnpm@12.15.0",
94
59
  "pi": {
95
60
  "extensions": [
96
61
  "./dist/index.js"