@cydm/pie 4.8.0 → 4.9.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/README.md +51 -27
- package/dist/builtin/extensions/ask-user/index.js +4 -4
- package/dist/builtin/extensions/cron/index.js +906 -158
- package/dist/builtin/extensions/im-forward/README.md +4 -2
- package/dist/builtin/extensions/im-forward/index.js +6822 -3951
- package/dist/builtin/extensions/memory/index.js +76 -28
- package/dist/builtin/extensions/pie-ipc/README.md +2 -2
- package/dist/builtin/extensions/pie-ipc/index.js +282 -58
- package/dist/builtin/extensions/pie-proc/README.md +7 -7
- package/dist/builtin/extensions/pie-proc/index.js +227 -75
- package/dist/builtin/extensions/plan-mode/index.js +5 -5
- package/dist/builtin/extensions/subagent/index.js +8 -8
- package/dist/builtin/extensions/todo/index.js +4 -4
- package/dist/builtin/skills/chrome-devtools-axi/SKILL.md +26 -24
- package/dist/builtin/skills/chrome-devtools-axi/chrome-devtools-axi-bridge.js +199 -121
- package/dist/builtin/skills/chrome-devtools-axi/chrome-devtools-axi-runtime.js +1 -1
- package/dist/builtin/skills/chrome-devtools-axi/chunk-HAUDUMDO.js +795 -0
- package/dist/builtin/skills/chrome-devtools-axi/{cli-ZE754CTH.js → cli-K7ESY3XD.js} +433 -255
- package/dist/builtin/skills/computer-use-axi/SKILL.md +25 -8
- package/dist/builtin/skills/computer-use-axi/computer-use-axi-broker.js +293 -64
- package/dist/builtin/skills/computer-use-axi/computer-use-axi-runtime.js +2 -2
- package/dist/builtin/skills/tui-use/tui-use-daemon.cjs +451 -43
- package/dist/chunks/{chunk-3W3SXVPB.js → chunk-4UIUMTHG.js} +993 -371
- package/dist/chunks/chunk-6WKBQZWM.js +19 -0
- package/dist/chunks/{chunk-5KI4Y4LA.js → chunk-BA4Z27XT.js} +4 -3
- package/dist/chunks/{chunk-BLDI2GRF.js → chunk-DBDU3RRN.js} +1364 -446
- package/dist/chunks/{chunk-LAJXWOKS.js → chunk-EEC5PJTC.js} +11 -9
- package/dist/chunks/chunk-IZKHSUNQ.js +30 -0
- package/dist/chunks/{chunk-Y3I2UV5Y.js → chunk-LRHNAG4V.js} +349 -288
- package/dist/chunks/chunk-OU33ROFD.js +717 -0
- package/dist/chunks/{chunk-Z2FKA2ZH.js → chunk-PN6B54VT.js} +104 -22
- package/dist/chunks/{chunk-NHV2CALB.js → chunk-WYXUF4MP.js} +388 -32
- package/dist/chunks/{chunk-4PR34UZF.js → chunk-Z5KL6AO2.js} +159 -145
- package/dist/chunks/{host-image-fallback-N7RHNPYN.js → host-image-fallback-RASJSVZY.js} +1 -1
- package/dist/chunks/{manager-cli-NEZA46M5.js → manager-cli-Y6HM3MHA.js} +5 -5
- package/dist/chunks/{model-registry-3XAHXXTA.js → model-registry-5TLYILYR.js} +2 -2
- package/dist/chunks/{read-file-understanding-FMPXR6EJ.js → read-file-understanding-VPKDBV23.js} +5 -5
- package/dist/chunks/{src-B44QWSGM.js → src-2XI5P62J.js} +2 -2
- package/dist/chunks/{src-EGXFJ3WA.js → src-6QGGDHIW.js} +3 -3
- package/dist/chunks/{src-Q7LU27C2.js → src-CCEENRKC.js} +31 -1
- package/dist/chunks/{src-CLE5ILN4.js → src-DI4MCAGT.js} +20 -4
- package/dist/chunks/{test-stream-OR3EJBNF.js → test-stream-SYVWIPPW.js} +2 -2
- package/dist/cli.js +1890 -1489
- package/models.schema.json +271 -0
- package/package.json +2 -2
- package/dist/builtin/skills/chrome-devtools-axi/chunk-Z2A5LA5A.js +0 -407
- package/dist/chunks/chunk-EGZ5TZRM.js +0 -1444
- package/dist/chunks/{chunk-CGSFBWOM.js → chunk-WBL73KRI.js} +3 -3
- package/dist/chunks/{manager-upgrade-CTU3ISCW.js → manager-upgrade-LSTOHBUV.js} +3 -3
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// builtin/extensions/pie-ipc/runtime-registry.ts
|
|
4
|
+
var runtimes = /* @__PURE__ */ new Map();
|
|
5
|
+
function key(root) {
|
|
6
|
+
const trimmed = root.replace(/[\\/]+$/, "");
|
|
7
|
+
return process.platform === "win32" ? trimmed.replace(/\//g, "\\").toLowerCase() : trimmed;
|
|
8
|
+
}
|
|
9
|
+
function registerIpcRuntime(root, runtime) {
|
|
10
|
+
const k = key(root);
|
|
11
|
+
runtimes.set(k, runtime);
|
|
12
|
+
return () => {
|
|
13
|
+
if (runtimes.get(k) === runtime) runtimes.delete(k);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function currentIpcRuntime(root) {
|
|
17
|
+
return runtimes.get(key(root));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// builtin/extensions/pie-ipc/pure.ts
|
|
21
|
+
import { randomBytes } from "node:crypto";
|
|
22
|
+
var IPC_PROTOCOL_VERSION = 2;
|
|
23
|
+
var INSTANCE_ID_PATTERN = /^pie-[a-z0-9]{12}$/;
|
|
24
|
+
var MAX_STATUS_FILE_BYTES = 128 * 1024;
|
|
25
|
+
var MAX_DISPLAY_NAME_CHARS = 64;
|
|
26
|
+
var MAX_STATUS_TEXT_FIELD_CHARS = 4096;
|
|
27
|
+
var MAX_BODY_CHARS = 8e3;
|
|
28
|
+
var STATUS_FILE_STALE_MS = 12e4;
|
|
29
|
+
var HEARTBEAT_INTERVAL_MS = 2e4;
|
|
30
|
+
var DEFAULT_SEND_WAIT_MS = 12e4;
|
|
31
|
+
var MAX_SEEN_ENVELOPE_IDS = 1e3;
|
|
32
|
+
var MAX_INBOX_MESSAGES = 100;
|
|
33
|
+
var MAX_INFLIGHT_SENDS = 8;
|
|
34
|
+
var INSTANCE_ID_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
35
|
+
function generateInstanceId(randomSource = defaultRandomBytes) {
|
|
36
|
+
const bytes = randomSource(12);
|
|
37
|
+
let id = "";
|
|
38
|
+
for (const byte of bytes) {
|
|
39
|
+
id += INSTANCE_ID_ALPHABET[byte % INSTANCE_ID_ALPHABET.length];
|
|
40
|
+
}
|
|
41
|
+
return `pie-${id}`;
|
|
42
|
+
}
|
|
43
|
+
function defaultRandomBytes(count) {
|
|
44
|
+
return randomBytes(count);
|
|
45
|
+
}
|
|
46
|
+
function isValidInstanceId(id) {
|
|
47
|
+
return INSTANCE_ID_PATTERN.test(id);
|
|
48
|
+
}
|
|
49
|
+
function peerLabel(instanceId, displayName) {
|
|
50
|
+
const name = displayName?.trim();
|
|
51
|
+
if (name) return name;
|
|
52
|
+
return instanceId.length > 12 ? instanceId.slice(0, 12) : instanceId;
|
|
53
|
+
}
|
|
54
|
+
var ENVELOPE_KINDS = /* @__PURE__ */ new Set([
|
|
55
|
+
"message",
|
|
56
|
+
"ack",
|
|
57
|
+
"result",
|
|
58
|
+
"state.get",
|
|
59
|
+
"state",
|
|
60
|
+
"result.get",
|
|
61
|
+
"control"
|
|
62
|
+
]);
|
|
63
|
+
var LIFECYCLE_STATES = /* @__PURE__ */ new Set(["working", "idle", "blocked"]);
|
|
64
|
+
function parseEnvelope(raw) {
|
|
65
|
+
let parsed;
|
|
66
|
+
try {
|
|
67
|
+
parsed = JSON.parse(raw);
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
72
|
+
const record = parsed;
|
|
73
|
+
if (typeof record.v !== "number" || record.v !== IPC_PROTOCOL_VERSION) return null;
|
|
74
|
+
if (typeof record.id !== "string" || !/^[a-zA-Z0-9:-]{1,128}$/.test(record.id)) return null;
|
|
75
|
+
if (typeof record.from !== "string" || !isValidInstanceId(record.from)) return null;
|
|
76
|
+
if (typeof record.to !== "string" || !isValidInstanceId(record.to)) return null;
|
|
77
|
+
if (record.text !== void 0 && (typeof record.text !== "string" || record.text.length > MAX_BODY_CHARS + 100)) return null;
|
|
78
|
+
if (record.sessionId !== void 0 && (typeof record.sessionId !== "string" || record.sessionId.length > 256)) return null;
|
|
79
|
+
if (record.replyTo !== void 0 && (typeof record.replyTo !== "string" || !/^[a-zA-Z0-9:-]{1,128}$/.test(record.replyTo))) return null;
|
|
80
|
+
if (record.waitMs !== void 0 && (typeof record.waitMs !== "number" || !Number.isFinite(record.waitMs) || record.waitMs < 0 || record.waitMs > 6e5)) return null;
|
|
81
|
+
if (record.notifyCompletion !== void 0 && typeof record.notifyCompletion !== "boolean") return null;
|
|
82
|
+
if (record.subscriberSession !== void 0 && (typeof record.subscriberSession !== "string" || record.subscriberSession.length > 256)) return null;
|
|
83
|
+
if (typeof record.kind !== "string" || !ENVELOPE_KINDS.has(record.kind)) return null;
|
|
84
|
+
if (typeof record.createdAt !== "number" || !Number.isSafeInteger(record.createdAt) || record.createdAt < 0) return null;
|
|
85
|
+
const envelope = {
|
|
86
|
+
v: record.v,
|
|
87
|
+
id: record.id,
|
|
88
|
+
from: record.from,
|
|
89
|
+
to: record.to,
|
|
90
|
+
kind: record.kind,
|
|
91
|
+
createdAt: record.createdAt
|
|
92
|
+
};
|
|
93
|
+
if (typeof record.auth === "string" && /^[a-f0-9]{64}$/.test(record.auth)) envelope.auth = record.auth;
|
|
94
|
+
if (typeof record.sessionId === "string") envelope.sessionId = record.sessionId;
|
|
95
|
+
if (typeof record.text === "string") envelope.text = record.text;
|
|
96
|
+
if (typeof record.replyTo === "string") envelope.replyTo = record.replyTo;
|
|
97
|
+
if (typeof record.waitMs === "number" && Number.isFinite(record.waitMs)) envelope.waitMs = record.waitMs;
|
|
98
|
+
if (record.notifyCompletion === true) envelope.notifyCompletion = true;
|
|
99
|
+
if (record.control === true) envelope.control = true;
|
|
100
|
+
if (record.userAssigned === true) envelope.userAssigned = true;
|
|
101
|
+
if (typeof record.subscriberSession === "string") envelope.subscriberSession = record.subscriberSession;
|
|
102
|
+
return envelope;
|
|
103
|
+
}
|
|
104
|
+
var FrameTooLargeError = class extends Error {
|
|
105
|
+
constructor() {
|
|
106
|
+
super("pie-ipc frame exceeds byte cap");
|
|
107
|
+
this.name = "FrameTooLargeError";
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var FrameDecoder = class {
|
|
111
|
+
buffer = "";
|
|
112
|
+
get partial() {
|
|
113
|
+
return this.buffer.length > 0;
|
|
114
|
+
}
|
|
115
|
+
push(chunk) {
|
|
116
|
+
this.buffer += chunk;
|
|
117
|
+
if (this.buffer.split("\n").some((line) => Buffer.byteLength(line, "utf8") > 64 * 1024)) {
|
|
118
|
+
this.buffer = "";
|
|
119
|
+
throw new FrameTooLargeError();
|
|
120
|
+
}
|
|
121
|
+
const envelopes = [];
|
|
122
|
+
let newline = this.buffer.indexOf("\n");
|
|
123
|
+
while (newline !== -1) {
|
|
124
|
+
const line = this.buffer.slice(0, newline).trim();
|
|
125
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
126
|
+
if (line) {
|
|
127
|
+
const envelope = parseEnvelope(line);
|
|
128
|
+
if (envelope) envelopes.push(envelope);
|
|
129
|
+
}
|
|
130
|
+
newline = this.buffer.indexOf("\n");
|
|
131
|
+
}
|
|
132
|
+
return envelopes;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var WRAP_PREFIX = "[pie-ipc]";
|
|
136
|
+
function wrapInboundText(fromLabel, text, at = Date.now(), messageId) {
|
|
137
|
+
const stamp = new Date(at).toISOString().slice(11, 19);
|
|
138
|
+
const marker = messageId ? ` #${clipMarker(messageId)} ` : " ";
|
|
139
|
+
return `${WRAP_PREFIX} ${stamp}${marker}peer "${clipLabel(fromLabel)}" says:
|
|
140
|
+
${clipBody(text, MAX_BODY_CHARS)}`;
|
|
141
|
+
}
|
|
142
|
+
function isWrappedInboundText(text) {
|
|
143
|
+
return text.startsWith(`${WRAP_PREFIX} `);
|
|
144
|
+
}
|
|
145
|
+
function wrappedMessageMarker(text) {
|
|
146
|
+
if (!isWrappedInboundText(text)) return null;
|
|
147
|
+
const firstLine = text.split("\n", 1)[0];
|
|
148
|
+
const match = / #(\S+) peer "/.exec(firstLine);
|
|
149
|
+
return match ? match[1] : null;
|
|
150
|
+
}
|
|
151
|
+
function clipMarker(id) {
|
|
152
|
+
const clipped = id;
|
|
153
|
+
return clipped || "noid";
|
|
154
|
+
}
|
|
155
|
+
function clipLabel(label) {
|
|
156
|
+
const collapsed = label.replace(/\s+/g, " ").trim().slice(0, 60);
|
|
157
|
+
return collapsed || "unknown";
|
|
158
|
+
}
|
|
159
|
+
function clipBody(text, maxChars = MAX_BODY_CHARS) {
|
|
160
|
+
if (text.length <= maxChars) return text;
|
|
161
|
+
return `${text.slice(0, maxChars)}\u2026[truncated ${text.length - maxChars} chars]`;
|
|
162
|
+
}
|
|
163
|
+
var BoundedIdSet = class {
|
|
164
|
+
constructor(max = MAX_SEEN_ENVELOPE_IDS) {
|
|
165
|
+
this.max = max;
|
|
166
|
+
}
|
|
167
|
+
max;
|
|
168
|
+
seen = /* @__PURE__ */ new Map();
|
|
169
|
+
/** Returns true when the id is new (and records it). */
|
|
170
|
+
add(id, now = Date.now()) {
|
|
171
|
+
if (this.seen.has(id)) return false;
|
|
172
|
+
this.seen.set(id, now);
|
|
173
|
+
if (this.seen.size > this.max) {
|
|
174
|
+
const drop = Math.max(1, Math.floor(this.max / 4));
|
|
175
|
+
let dropped = 0;
|
|
176
|
+
for (const key2 of this.seen.keys()) {
|
|
177
|
+
if (dropped >= drop) break;
|
|
178
|
+
this.seen.delete(key2);
|
|
179
|
+
dropped += 1;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
has(id) {
|
|
185
|
+
return this.seen.has(id);
|
|
186
|
+
}
|
|
187
|
+
get size() {
|
|
188
|
+
return this.seen.size;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
var signalAliveCheck = (pid) => {
|
|
192
|
+
try {
|
|
193
|
+
process.kill(pid, 0);
|
|
194
|
+
return true;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
return error.code === "EPERM";
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
function peerHealth(file, now = Date.now(), aliveCheck = signalAliveCheck) {
|
|
200
|
+
if (Number.isInteger(file.pid) && file.pid > 0 && !aliveCheck(file.pid)) return "dead";
|
|
201
|
+
if (now - file.updatedAt > STATUS_FILE_STALE_MS) return "stale";
|
|
202
|
+
return "alive";
|
|
203
|
+
}
|
|
204
|
+
function parseStatusFile(raw) {
|
|
205
|
+
let parsed;
|
|
206
|
+
try {
|
|
207
|
+
parsed = JSON.parse(raw);
|
|
208
|
+
} catch {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
212
|
+
const record = parsed;
|
|
213
|
+
if (record.schema !== 1) return null;
|
|
214
|
+
if (typeof record.instanceId !== "string" || !isValidInstanceId(record.instanceId)) return null;
|
|
215
|
+
if (typeof record.pid !== "number" || !Number.isInteger(record.pid) || record.pid <= 0 || record.pid > 99999999) return null;
|
|
216
|
+
if (typeof record.protocol !== "number" || record.protocol !== IPC_PROTOCOL_VERSION) return null;
|
|
217
|
+
if (typeof record.displayName !== "string" || !record.displayName || record.displayName.length > MAX_DISPLAY_NAME_CHARS) return null;
|
|
218
|
+
if (hasControlChars(record.displayName)) return null;
|
|
219
|
+
if (record.sessionId !== null && (typeof record.sessionId !== "string" || record.sessionId.length > 256 || hasControlChars(record.sessionId))) return null;
|
|
220
|
+
if (typeof record.cwd !== "string" || record.cwd.length > MAX_STATUS_TEXT_FIELD_CHARS || hasControlChars(record.cwd)) return null;
|
|
221
|
+
if (typeof record.startedAt !== "number" || !Number.isSafeInteger(record.startedAt) || record.startedAt < 0) return null;
|
|
222
|
+
if (typeof record.updatedAt !== "number" || !Number.isSafeInteger(record.updatedAt) || record.updatedAt < 0) return null;
|
|
223
|
+
if (typeof record.state !== "string" || !LIFECYCLE_STATES.has(record.state)) return null;
|
|
224
|
+
const file = {
|
|
225
|
+
schema: 1,
|
|
226
|
+
instanceId: record.instanceId,
|
|
227
|
+
pid: record.pid,
|
|
228
|
+
protocol: record.protocol,
|
|
229
|
+
displayName: record.displayName,
|
|
230
|
+
sessionId: record.sessionId,
|
|
231
|
+
cwd: record.cwd,
|
|
232
|
+
startedAt: record.startedAt,
|
|
233
|
+
updatedAt: record.updatedAt,
|
|
234
|
+
state: record.state
|
|
235
|
+
};
|
|
236
|
+
if (typeof record.unattended === "boolean") file.unattended = record.unattended;
|
|
237
|
+
if (typeof record.isManager === "boolean") file.isManager = record.isManager;
|
|
238
|
+
if (typeof record.sessionControls === "boolean") file.sessionControls = record.sessionControls;
|
|
239
|
+
if (typeof record.workerPeek === "boolean") file.workerPeek = record.workerPeek;
|
|
240
|
+
if (typeof record.version === "string" && record.version.length <= 64 && !hasControlChars(record.version)) file.version = record.version;
|
|
241
|
+
if (typeof record.model === "string" && record.model.length <= 160 && !hasControlChars(record.model)) file.model = record.model;
|
|
242
|
+
const thinkingRe = /^(?:off|minimal|low|medium|high|xhigh|max)$/;
|
|
243
|
+
if (typeof record.thinkingLevel === "string" && thinkingRe.test(record.thinkingLevel)) file.thinkingLevel = record.thinkingLevel;
|
|
244
|
+
if (typeof record.effectiveThinkingLevel === "string" && thinkingRe.test(record.effectiveThinkingLevel)) file.effectiveThinkingLevel = record.effectiveThinkingLevel;
|
|
245
|
+
if (Array.isArray(record.supportedThinkingLevels) && record.supportedThinkingLevels.length <= 8 && record.supportedThinkingLevels.every((l) => typeof l === "string" && thinkingRe.test(l))) {
|
|
246
|
+
file.supportedThinkingLevels = record.supportedThinkingLevels;
|
|
247
|
+
}
|
|
248
|
+
if (typeof record.imBinding === "string" && record.imBinding.length <= 512 && !hasControlChars(record.imBinding)) file.imBinding = record.imBinding;
|
|
249
|
+
if (Array.isArray(record.allowPeers) && record.allowPeers.length <= 1e3 && record.allowPeers.every((id) => typeof id === "string" && (id === "*" || isValidInstanceId(id)))) file.allowPeers = record.allowPeers;
|
|
250
|
+
if (typeof record.secret === "string" && /^[a-f0-9]{64}$/.test(record.secret)) file.secret = record.secret;
|
|
251
|
+
if (typeof record.sessionName === "string" && !hasControlChars(record.sessionName) && record.sessionName.length <= MAX_DISPLAY_NAME_CHARS) file.sessionName = record.sessionName;
|
|
252
|
+
if (typeof record.currentTool === "string" && !hasControlChars(record.currentTool) && record.currentTool.length <= 200) file.currentTool = record.currentTool;
|
|
253
|
+
if (typeof record.blockedSince === "number" && Number.isSafeInteger(record.blockedSince) && record.blockedSince >= 0) file.blockedSince = record.blockedSince;
|
|
254
|
+
if (typeof record.turnStartedAt === "number" && Number.isSafeInteger(record.turnStartedAt) && record.turnStartedAt >= 0) file.turnStartedAt = record.turnStartedAt;
|
|
255
|
+
if (typeof record.undeliveredInjections === "number" && Number.isFinite(record.undeliveredInjections)) file.undeliveredInjections = Math.min(100, Math.max(0, Math.floor(record.undeliveredInjections)));
|
|
256
|
+
if (typeof record.port === "number" && Number.isInteger(record.port) && record.port > 0 && record.port <= 65535) file.port = record.port;
|
|
257
|
+
return file;
|
|
258
|
+
}
|
|
259
|
+
function hasControlChars(text) {
|
|
260
|
+
return /[\u0000-\u001f\u007f]/.test(text);
|
|
261
|
+
}
|
|
262
|
+
var BLOCKING_TOOL_NAMES = /* @__PURE__ */ new Set(["ask_user_multi"]);
|
|
263
|
+
function isBlockingTool(toolName) {
|
|
264
|
+
return BLOCKING_TOOL_NAMES.has(toolName);
|
|
265
|
+
}
|
|
266
|
+
function foldLifecycleEvent(current, event, now = Date.now()) {
|
|
267
|
+
switch (event.type) {
|
|
268
|
+
case "turn:start":
|
|
269
|
+
case "agent:start": {
|
|
270
|
+
const next = { state: "working", turnStartedAt: now };
|
|
271
|
+
if (current.blockedSince !== void 0) next.blockedSince = current.blockedSince;
|
|
272
|
+
return next;
|
|
273
|
+
}
|
|
274
|
+
case "tool:execution:start": {
|
|
275
|
+
if (current.state === "idle") return { ...current, state: "working", turnStartedAt: current.turnStartedAt ?? now };
|
|
276
|
+
if (event.toolName && isBlockingTool(event.toolName) && current.state !== "blocked") {
|
|
277
|
+
return { ...current, state: "blocked", blockedSince: now, currentTool: event.toolName };
|
|
278
|
+
}
|
|
279
|
+
return { ...current, currentTool: event.toolName };
|
|
280
|
+
}
|
|
281
|
+
case "tool:execution:end": {
|
|
282
|
+
if (current.state === "blocked" && (!event.toolName || isBlockingTool(event.toolName))) {
|
|
283
|
+
return { ...current, state: "working", blockedSince: void 0, currentTool: void 0 };
|
|
284
|
+
}
|
|
285
|
+
return current;
|
|
286
|
+
}
|
|
287
|
+
case "turn:completed":
|
|
288
|
+
case "turn:failed":
|
|
289
|
+
case "turn:cancelled":
|
|
290
|
+
case "agent:end": {
|
|
291
|
+
if (current.state === "idle") return current;
|
|
292
|
+
return { state: "idle" };
|
|
293
|
+
}
|
|
294
|
+
default:
|
|
295
|
+
return current;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function resolveIpcConfig(configLayers) {
|
|
299
|
+
const problems = [];
|
|
300
|
+
let invalidConfig = false;
|
|
301
|
+
const merged = {};
|
|
302
|
+
for (const layer of configLayers) {
|
|
303
|
+
if (layer.content === void 0) continue;
|
|
304
|
+
try {
|
|
305
|
+
const parsed = JSON.parse(layer.content);
|
|
306
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
307
|
+
problems.push(`${layer.file}: root is not an object`);
|
|
308
|
+
invalidConfig = true;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
Object.assign(merged, parsed);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
problems.push(`${layer.file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
314
|
+
invalidConfig = true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (merged.enabled !== void 0 && typeof merged.enabled !== "boolean") {
|
|
318
|
+
problems.push("pie-ipc: enabled must be a boolean");
|
|
319
|
+
invalidConfig = true;
|
|
320
|
+
}
|
|
321
|
+
if (invalidConfig || merged.enabled === false) return { config: null, problems };
|
|
322
|
+
const config = { enabled: true, allowPeers: ["*"], autoInject: true };
|
|
323
|
+
if (typeof merged.displayName === "string" && merged.displayName.trim()) {
|
|
324
|
+
config.displayName = merged.displayName.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 60);
|
|
325
|
+
}
|
|
326
|
+
const envName = process.env.PIE_IPC_DISPLAY_NAME;
|
|
327
|
+
if (typeof envName === "string" && envName.trim()) {
|
|
328
|
+
config.displayName = envName.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 60);
|
|
329
|
+
}
|
|
330
|
+
if (Array.isArray(merged.allowPeers)) {
|
|
331
|
+
config.allowPeers = merged.allowPeers.filter((id) => typeof id === "string" && (id === "*" || isValidInstanceId(id))).slice(0, 1e3);
|
|
332
|
+
} else if (merged.allowPeers !== void 0) {
|
|
333
|
+
config.allowPeers = [];
|
|
334
|
+
problems.push("pie-ipc: allowPeers must be an array");
|
|
335
|
+
}
|
|
336
|
+
if (merged.autoInject !== void 0) config.autoInject = merged.autoInject === true;
|
|
337
|
+
if (typeof merged.maxBodyChars === "number" && Number.isFinite(merged.maxBodyChars)) {
|
|
338
|
+
const bounded = Math.min(Math.max(Math.floor(merged.maxBodyChars), 100), MAX_BODY_CHARS);
|
|
339
|
+
if (bounded !== merged.maxBodyChars) {
|
|
340
|
+
problems.push(`pie-ipc: maxBodyChars clamped to ${bounded}`);
|
|
341
|
+
}
|
|
342
|
+
config.maxBodyChars = bounded;
|
|
343
|
+
}
|
|
344
|
+
return { config, problems };
|
|
345
|
+
}
|
|
346
|
+
function assistantBodyText(message) {
|
|
347
|
+
if (message?.role !== "assistant" || !Array.isArray(message.content)) return "";
|
|
348
|
+
let text = "";
|
|
349
|
+
let hasToolCalls = false;
|
|
350
|
+
for (const block of message.content) {
|
|
351
|
+
if (typeof block !== "object" || block === null) continue;
|
|
352
|
+
const record = block;
|
|
353
|
+
if (record.type === "text" && typeof record.text === "string") text += record.text;
|
|
354
|
+
else if (record.type === "toolCall") hasToolCalls = true;
|
|
355
|
+
}
|
|
356
|
+
if (hasToolCalls) return "";
|
|
357
|
+
return text.trim();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// builtin/extensions/pie-ipc/transport.ts
|
|
361
|
+
import * as net from "node:net";
|
|
362
|
+
import { createHmac, randomBytes as randomBytes2, randomUUID, timingSafeEqual } from "node:crypto";
|
|
363
|
+
import { StringDecoder } from "node:string_decoder";
|
|
364
|
+
var CLOSE_FLUSH_TIMEOUT_MS = 1e3;
|
|
365
|
+
function signEnvelope(e, secret) {
|
|
366
|
+
const payload = [e.v, e.id, e.from, e.to, e.kind, e.createdAt, e.sessionId ?? null, e.text ?? null, e.replyTo ?? null, e.waitMs ?? null, e.notifyCompletion ?? null, e.control ?? null, e.userAssigned ?? null, e.subscriberSession ?? null];
|
|
367
|
+
return { ...e, auth: createHmac("sha256", secret).update(JSON.stringify(payload)).digest("hex") };
|
|
368
|
+
}
|
|
369
|
+
function verified(e, secret) {
|
|
370
|
+
if (!e.auth || !/^[a-f0-9]{64}$/.test(e.auth)) return false;
|
|
371
|
+
return timingSafeEqual(Buffer.from(e.auth, "hex"), Buffer.from(signEnvelope(e, secret).auth, "hex"));
|
|
372
|
+
}
|
|
373
|
+
var IpcTransport = class {
|
|
374
|
+
constructor(host) {
|
|
375
|
+
this.host = host;
|
|
376
|
+
this.instanceId = host.instanceId;
|
|
377
|
+
this.secret = host.secret ?? randomBytes2(32).toString("hex");
|
|
378
|
+
this.server = net.createServer((socket) => this.accept(socket));
|
|
379
|
+
this.server.on("error", () => {
|
|
380
|
+
});
|
|
381
|
+
this.server.unref();
|
|
382
|
+
}
|
|
383
|
+
host;
|
|
384
|
+
instanceId;
|
|
385
|
+
secret;
|
|
386
|
+
server;
|
|
387
|
+
sockets = /* @__PURE__ */ new Set();
|
|
388
|
+
cancels = /* @__PURE__ */ new Set();
|
|
389
|
+
seen = new BoundedIdSet();
|
|
390
|
+
closed = false;
|
|
391
|
+
inflight = 0;
|
|
392
|
+
inbound = 0;
|
|
393
|
+
rateWindow = Date.now();
|
|
394
|
+
rateCount = 0;
|
|
395
|
+
port = null;
|
|
396
|
+
get listenPort() {
|
|
397
|
+
return this.port;
|
|
398
|
+
}
|
|
399
|
+
listen() {
|
|
400
|
+
if (this.closed) return Promise.reject(new Error("closed"));
|
|
401
|
+
return new Promise((resolve, reject) => {
|
|
402
|
+
const cancel = () => {
|
|
403
|
+
this.cancels.delete(cancel);
|
|
404
|
+
reject(new Error("closed"));
|
|
405
|
+
};
|
|
406
|
+
this.cancels.add(cancel);
|
|
407
|
+
const error = (e) => {
|
|
408
|
+
this.cancels.delete(cancel);
|
|
409
|
+
reject(e);
|
|
410
|
+
};
|
|
411
|
+
this.server.once("error", error);
|
|
412
|
+
this.server.listen(0, "127.0.0.1", () => {
|
|
413
|
+
this.server.off("error", error);
|
|
414
|
+
this.cancels.delete(cancel);
|
|
415
|
+
if (this.closed) {
|
|
416
|
+
this.server.close();
|
|
417
|
+
reject(new Error("closed"));
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
const address = this.server.address();
|
|
421
|
+
if (!address || typeof address === "string") {
|
|
422
|
+
reject(new Error("closed"));
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
this.port = address.port;
|
|
426
|
+
resolve(address.port);
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
write(socket, envelope, secret) {
|
|
431
|
+
if (socket.destroyed) return false;
|
|
432
|
+
const data = JSON.stringify(signEnvelope(envelope, secret)) + "\n";
|
|
433
|
+
if (socket.writableLength + Buffer.byteLength(data) > 512 * 1024) {
|
|
434
|
+
socket.destroy();
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
try {
|
|
438
|
+
socket.write(data);
|
|
439
|
+
return true;
|
|
440
|
+
} catch {
|
|
441
|
+
socket.destroy();
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
read(socket, receive, idleMs) {
|
|
446
|
+
const decoder = new FrameDecoder();
|
|
447
|
+
const utf8 = new StringDecoder("utf8");
|
|
448
|
+
let frameTimer;
|
|
449
|
+
let frames = 0;
|
|
450
|
+
socket.setTimeout(idleMs, () => socket.destroy());
|
|
451
|
+
socket.on("error", () => socket.destroy());
|
|
452
|
+
socket.on("close", () => {
|
|
453
|
+
if (frameTimer) clearTimeout(frameTimer);
|
|
454
|
+
this.sockets.delete(socket);
|
|
455
|
+
});
|
|
456
|
+
socket.on("data", (chunk) => {
|
|
457
|
+
try {
|
|
458
|
+
frames += chunk.reduce((n, b) => n + (b === 10 ? 1 : 0), 0);
|
|
459
|
+
if (frames > 128) {
|
|
460
|
+
socket.destroy();
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const envelopes = decoder.push(utf8.write(chunk));
|
|
464
|
+
const partial = decoder.partial || chunk.at(-1) !== 10;
|
|
465
|
+
if (frameTimer && (!partial || chunk.includes(10))) {
|
|
466
|
+
clearTimeout(frameTimer);
|
|
467
|
+
frameTimer = void 0;
|
|
468
|
+
}
|
|
469
|
+
if (partial && !frameTimer) {
|
|
470
|
+
frameTimer = setTimeout(() => socket.destroy(), 15e3);
|
|
471
|
+
frameTimer.unref();
|
|
472
|
+
}
|
|
473
|
+
for (const e of envelopes) {
|
|
474
|
+
if (socket.destroyed) break;
|
|
475
|
+
receive(e);
|
|
476
|
+
}
|
|
477
|
+
} catch {
|
|
478
|
+
socket.destroy();
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
accept(socket) {
|
|
483
|
+
if (this.closed || this.inbound >= 16) {
|
|
484
|
+
socket.destroy();
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
this.inbound++;
|
|
488
|
+
this.sockets.add(socket);
|
|
489
|
+
socket.setNoDelay(true);
|
|
490
|
+
socket.once("close", () => {
|
|
491
|
+
this.inbound--;
|
|
492
|
+
});
|
|
493
|
+
const lifecycle = new AbortController();
|
|
494
|
+
socket.once("close", () => lifecycle.abort());
|
|
495
|
+
let sender;
|
|
496
|
+
this.read(socket, (e) => {
|
|
497
|
+
if (e.to !== this.instanceId || !verified(e, this.secret) || sender && sender !== e.from) {
|
|
498
|
+
socket.destroy();
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
sender = e.from;
|
|
502
|
+
if (Date.now() - this.rateWindow >= 1e3) {
|
|
503
|
+
this.rateWindow = Date.now();
|
|
504
|
+
this.rateCount = 0;
|
|
505
|
+
}
|
|
506
|
+
if (++this.rateCount > 128) {
|
|
507
|
+
socket.destroy();
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (e.waitMs) socket.setTimeout(Math.max(6e4, e.waitMs + 1e3));
|
|
511
|
+
if (!this.seen.add(`${e.from}:${e.id}`)) return;
|
|
512
|
+
try {
|
|
513
|
+
if (this.closed) return;
|
|
514
|
+
void Promise.resolve(this.host.onEnvelope(e, (reply) => {
|
|
515
|
+
if (reply.from === this.instanceId && reply.to === e.from && reply.replyTo === e.id) this.write(socket, reply, this.secret);
|
|
516
|
+
}, lifecycle.signal)).catch(() => socket.destroy());
|
|
517
|
+
} catch {
|
|
518
|
+
socket.destroy();
|
|
519
|
+
}
|
|
520
|
+
}, 6e4);
|
|
521
|
+
}
|
|
522
|
+
/** Synchronous CAPACITY probe (review r3): transport.send is async, so a
|
|
523
|
+
* capacity rejection still yields a truthy Promise resolving null — callers
|
|
524
|
+
* distinguishing 'never left the process' from 'sent, outcome unknown' must
|
|
525
|
+
* check this BEFORE awaiting send. Scope: closed/inflight ONLY (capacity &
|
|
526
|
+
* lifecycle), NOT send()'s full validation (signal/secret/port/waitMs can
|
|
527
|
+
* still null-reject after a true probe — those are peer/discovery outcomes,
|
|
528
|
+
* covered by the best-effort contract). */
|
|
529
|
+
canSendNow() {
|
|
530
|
+
return !this.closed && this.inflight < MAX_INFLIGHT_SENDS;
|
|
531
|
+
}
|
|
532
|
+
async send(peerInstanceId, port, kind, options = {}) {
|
|
533
|
+
if (this.closed || options.signal?.aborted || this.inflight >= MAX_INFLIGHT_SENDS) return null;
|
|
534
|
+
let secret;
|
|
535
|
+
try {
|
|
536
|
+
secret = this.host.resolveSecret?.(peerInstanceId);
|
|
537
|
+
} catch {
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
if (!secret || !/^[a-f0-9]{64}$/.test(secret) || !Number.isInteger(port) || port < 1 || port > 65535) return null;
|
|
541
|
+
const waitMs = options.waitMs ?? DEFAULT_SEND_WAIT_MS;
|
|
542
|
+
if (!Number.isFinite(waitMs) || waitMs < 0 || waitMs > 6e5) return null;
|
|
543
|
+
const holdOpen = options.expectReply === true || options.awaitAck === true;
|
|
544
|
+
this.inflight++;
|
|
545
|
+
return new Promise((resolve) => {
|
|
546
|
+
const socket = new net.Socket();
|
|
547
|
+
this.sockets.add(socket);
|
|
548
|
+
const envelope = { v: IPC_PROTOCOL_VERSION, id: options.requestId ?? randomUUID(), from: this.instanceId, to: peerInstanceId, kind, createdAt: Date.now(), ...options.text !== void 0 ? { text: clipBody(options.text) } : {}, ...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {}, ...options.replyTo ? { replyTo: options.replyTo } : {}, waitMs: options.expectReply ? waitMs : 0, ...options.notifyCompletion === true ? { notifyCompletion: true } : {}, ...options.subscriberSession !== void 0 ? { subscriberSession: options.subscriberSession } : {}, ...options.userAssigned === true ? { userAssigned: true } : {} };
|
|
549
|
+
let done = false;
|
|
550
|
+
const finish = (value) => {
|
|
551
|
+
if (done) return;
|
|
552
|
+
done = true;
|
|
553
|
+
clearTimeout(timer);
|
|
554
|
+
clearTimeout(dialTimer);
|
|
555
|
+
options.signal?.removeEventListener("abort", abort);
|
|
556
|
+
this.cancels.delete(abort);
|
|
557
|
+
this.inflight--;
|
|
558
|
+
socket.destroy();
|
|
559
|
+
resolve(value);
|
|
560
|
+
};
|
|
561
|
+
const abort = () => finish(null);
|
|
562
|
+
const timer = setTimeout(abort, holdOpen ? waitMs : 3e3);
|
|
563
|
+
const dialTimer = setTimeout(abort, 3e3);
|
|
564
|
+
this.cancels.add(abort);
|
|
565
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
566
|
+
socket.once("close", abort);
|
|
567
|
+
this.read(socket, (e) => {
|
|
568
|
+
if (e.to !== this.instanceId || e.from !== peerInstanceId || e.replyTo !== envelope.id || !verified(e, secret)) {
|
|
569
|
+
finish(null);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if ((options.replyKinds ?? /* @__PURE__ */ new Set(["result", "ack", "state"])).has(e.kind)) finish(e);
|
|
573
|
+
}, Math.max(waitMs, 3e3));
|
|
574
|
+
socket.once("connect", () => {
|
|
575
|
+
clearTimeout(dialTimer);
|
|
576
|
+
if (done || options.signal?.aborted || this.closed) {
|
|
577
|
+
finish(null);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (!this.write(socket, envelope, secret)) {
|
|
581
|
+
finish(null);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (!holdOpen) {
|
|
585
|
+
socket.end(() => finish(envelope));
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
try {
|
|
589
|
+
socket.connect(port, "127.0.0.1");
|
|
590
|
+
} catch {
|
|
591
|
+
finish(null);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
close() {
|
|
596
|
+
void this.closeAndFlush();
|
|
597
|
+
}
|
|
598
|
+
async closeAndFlush() {
|
|
599
|
+
if (this.closed) return;
|
|
600
|
+
this.closed = true;
|
|
601
|
+
for (const cancel of [...this.cancels]) cancel();
|
|
602
|
+
const flushes = [];
|
|
603
|
+
for (const socket of this.sockets) {
|
|
604
|
+
if (socket.destroyed) continue;
|
|
605
|
+
flushes.push(new Promise((resolve) => {
|
|
606
|
+
let settled = false;
|
|
607
|
+
const done = () => {
|
|
608
|
+
if (!settled) {
|
|
609
|
+
settled = true;
|
|
610
|
+
clearTimeout(timer);
|
|
611
|
+
resolve();
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
const timer = setTimeout(() => {
|
|
615
|
+
socket.destroy();
|
|
616
|
+
done();
|
|
617
|
+
}, CLOSE_FLUSH_TIMEOUT_MS);
|
|
618
|
+
timer.unref();
|
|
619
|
+
socket.once("close", done);
|
|
620
|
+
socket.once("error", done);
|
|
621
|
+
socket.end();
|
|
622
|
+
}));
|
|
623
|
+
}
|
|
624
|
+
this.sockets.clear();
|
|
625
|
+
this.server.close();
|
|
626
|
+
this.port = null;
|
|
627
|
+
if (flushes.length > 0) await Promise.allSettled(flushes);
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
// builtin/extensions/pie-ipc/im-bridge.ts
|
|
632
|
+
var DISPATCH_PREFIX = "[im-forward-dispatch]";
|
|
633
|
+
var dispatchReceiver;
|
|
634
|
+
function registerDispatchReceiver(receiver) {
|
|
635
|
+
dispatchReceiver = receiver;
|
|
636
|
+
}
|
|
637
|
+
function takeDispatchFrame(text) {
|
|
638
|
+
if (!dispatchReceiver) return false;
|
|
639
|
+
try {
|
|
640
|
+
return dispatchReceiver(text);
|
|
641
|
+
} catch {
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
var imBinding;
|
|
646
|
+
var bindingListeners = /* @__PURE__ */ new Set();
|
|
647
|
+
function bindingChanged() {
|
|
648
|
+
for (const fn of bindingListeners) {
|
|
649
|
+
try {
|
|
650
|
+
fn();
|
|
651
|
+
} catch {
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function onImBindingChanged(fn) {
|
|
656
|
+
bindingListeners.add(fn);
|
|
657
|
+
return () => {
|
|
658
|
+
bindingListeners.delete(fn);
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function registerImBinding(value) {
|
|
662
|
+
const token = Symbol(value);
|
|
663
|
+
imBinding = { value, token };
|
|
664
|
+
bindingChanged();
|
|
665
|
+
return () => {
|
|
666
|
+
if (imBinding?.token === token) {
|
|
667
|
+
imBinding = void 0;
|
|
668
|
+
bindingChanged();
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function currentImBinding() {
|
|
673
|
+
return imBinding?.value;
|
|
674
|
+
}
|
|
675
|
+
var turnImDriverOracle;
|
|
676
|
+
function registerTurnImDriver(oracle) {
|
|
677
|
+
turnImDriverOracle = oracle;
|
|
678
|
+
}
|
|
679
|
+
function isTurnUserImDriven() {
|
|
680
|
+
try {
|
|
681
|
+
return turnImDriverOracle?.() === true;
|
|
682
|
+
} catch {
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
export {
|
|
688
|
+
registerIpcRuntime,
|
|
689
|
+
currentIpcRuntime,
|
|
690
|
+
IPC_PROTOCOL_VERSION,
|
|
691
|
+
MAX_STATUS_FILE_BYTES,
|
|
692
|
+
MAX_BODY_CHARS,
|
|
693
|
+
HEARTBEAT_INTERVAL_MS,
|
|
694
|
+
DEFAULT_SEND_WAIT_MS,
|
|
695
|
+
MAX_INBOX_MESSAGES,
|
|
696
|
+
generateInstanceId,
|
|
697
|
+
isValidInstanceId,
|
|
698
|
+
peerLabel,
|
|
699
|
+
wrapInboundText,
|
|
700
|
+
isWrappedInboundText,
|
|
701
|
+
wrappedMessageMarker,
|
|
702
|
+
clipBody,
|
|
703
|
+
peerHealth,
|
|
704
|
+
parseStatusFile,
|
|
705
|
+
foldLifecycleEvent,
|
|
706
|
+
resolveIpcConfig,
|
|
707
|
+
assistantBodyText,
|
|
708
|
+
IpcTransport,
|
|
709
|
+
DISPATCH_PREFIX,
|
|
710
|
+
registerDispatchReceiver,
|
|
711
|
+
takeDispatchFrame,
|
|
712
|
+
onImBindingChanged,
|
|
713
|
+
registerImBinding,
|
|
714
|
+
currentImBinding,
|
|
715
|
+
registerTurnImDriver,
|
|
716
|
+
isTurnUserImDriven
|
|
717
|
+
};
|