@cydm/pie 4.3.7 → 4.3.12
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/builtin/extensions/ask-user/index.js +1 -1
- package/dist/builtin/extensions/cron/index.js +112 -8
- package/dist/builtin/extensions/im-forward/README.md +17 -1
- package/dist/builtin/extensions/im-forward/index.js +918 -347
- package/dist/builtin/extensions/mcp-adapter/index.js +2 -2
- package/dist/builtin/extensions/memory/index.js +10 -3
- package/dist/builtin/extensions/pie-ipc/index.js +179 -378
- package/dist/builtin/extensions/pie-proc/index.js +36 -4
- package/dist/builtin/extensions/plan-mode/index.js +6 -5
- package/dist/builtin/extensions/subagent/index.js +15 -5
- package/dist/builtin/extensions/todo/index.js +6 -2
- package/dist/chunks/{chunk-N3BWFAMZ.js → chunk-EGNGHINP.js} +521 -161
- package/dist/chunks/{chunk-BJ4XN5VX.js → chunk-GY47MKCJ.js} +13 -8
- package/dist/chunks/{chunk-2SAFRLFJ.js → chunk-I7PBXG5Q.js} +1 -1
- package/dist/chunks/{chunk-WZLO5V2X.js → chunk-N5B5IGJ6.js} +355 -231
- package/dist/chunks/{manager-cli-QNMCCIFG.js → manager-cli-4Z6RZ7F4.js} +195 -205
- package/dist/chunks/{src-UVULNQOO.js → src-V2M7FLWY.js} +7 -1
- package/dist/cli.js +143 -20
- package/package.json +2 -2
|
@@ -38,11 +38,11 @@ function inputHeaderLine(userInput) {
|
|
|
38
38
|
return `> [UserInput] ${escaped}`;
|
|
39
39
|
}
|
|
40
40
|
function composeForwardBody(sessionId, sessionName, userInput, text, mode = "topic-root") {
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
const lines = [];
|
|
42
|
+
if (mode !== "topic-reply") lines.push(`**[Pie] \xB7 ${sessionLabel(sessionId, sessionName)}**`);
|
|
43
43
|
const input = inputHeaderLine(userInput);
|
|
44
44
|
if (input) lines.push(input);
|
|
45
|
-
lines.push("");
|
|
45
|
+
if (lines.length > 0) lines.push("");
|
|
46
46
|
lines.push(text);
|
|
47
47
|
return lines.join("\n");
|
|
48
48
|
}
|
|
@@ -143,7 +143,13 @@ function scanFenceLines(text) {
|
|
|
143
143
|
open = null;
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
-
const
|
|
146
|
+
const isListMarker = marker !== null;
|
|
147
|
+
const isListContinuation = !isListMarker && listIndent > 0 && source.startsWith(" ".repeat(listIndent));
|
|
148
|
+
const isContainerContent = isListMarker || isListContinuation || open !== null || opened !== null && open !== null;
|
|
149
|
+
const rawLine = lines[i];
|
|
150
|
+
const quotePrefix = /^(?: {0,3}> ?)+/.exec(rawLine)?.[0] ?? "";
|
|
151
|
+
const emitSource = isContainerContent || quotePrefix === "" ? source : rawLine;
|
|
152
|
+
const dedentColumns = expandLeadingTabs(emitSource).length - line.length;
|
|
147
153
|
const rawDedent = (raw, columns) => {
|
|
148
154
|
if (columns <= 0) return raw;
|
|
149
155
|
if (!raw.includes(" ")) return raw.slice(columns);
|
|
@@ -157,7 +163,7 @@ function scanFenceLines(text) {
|
|
|
157
163
|
}
|
|
158
164
|
return raw.slice(index);
|
|
159
165
|
};
|
|
160
|
-
lines[i] = stateAtLineStart[i] || opened ? rawDedent(
|
|
166
|
+
lines[i] = stateAtLineStart[i] || opened ? rawDedent(emitSource, dedentColumns) : emitSource;
|
|
161
167
|
openedBy.push(opened);
|
|
162
168
|
closedBy.push(closed);
|
|
163
169
|
stateAtLineEnd.push(open);
|
|
@@ -331,9 +337,9 @@ function collectFileReferences(text, cwd, fsStat, options) {
|
|
|
331
337
|
}
|
|
332
338
|
for (const candidate of candidates) {
|
|
333
339
|
if (results.length >= maxFiles) break;
|
|
334
|
-
let
|
|
335
|
-
if (!
|
|
336
|
-
const clean =
|
|
340
|
+
let path4 = candidate.trim();
|
|
341
|
+
if (!path4 || path4.startsWith("/") || path4.startsWith("~") || /^[a-z]+:/i.test(path4) || path4.includes("://")) continue;
|
|
342
|
+
const clean = path4.replace(/^\.\//, "").split("#")[0].split("?")[0];
|
|
337
343
|
if (clean.includes("..")) continue;
|
|
338
344
|
const extension = clean.split(".").pop().toLowerCase();
|
|
339
345
|
if (!extension || !ATTACHABLE_EXTENSIONS.has(extension)) continue;
|
|
@@ -547,149 +553,6 @@ function resolveImForwardConfig(cwd, dir) {
|
|
|
547
553
|
return { config: { enabled: cfg.enabled, chatId, allowUsers: allowUsers?.length ? allowUsers : void 0, larkCli: typeof raw.larkCli === "string" && raw.larkCli.trim() ? raw.larkCli.trim() : void 0, retryBackoffMs: cfg.retryBackoffMs }, problems: chatId.startsWith("oc_") ? [] : ["im-forward: chatId does not look like a chat id (oc_...)"] };
|
|
548
554
|
}
|
|
549
555
|
|
|
550
|
-
// builtin/extensions/im-forward/event-hub.ts
|
|
551
|
-
import * as fs2 from "node:fs";
|
|
552
|
-
import * as path2 from "node:path";
|
|
553
|
-
var DISPATCH_PREFIX = "[im-forward-dispatch]";
|
|
554
|
-
var dispatchReceiver;
|
|
555
|
-
var turnImDriverOracle;
|
|
556
|
-
function registerTurnImDriver(oracle) {
|
|
557
|
-
turnImDriverOracle = oracle;
|
|
558
|
-
}
|
|
559
|
-
function isTurnUserImDriven() {
|
|
560
|
-
try {
|
|
561
|
-
return turnImDriverOracle?.() === true;
|
|
562
|
-
} catch {
|
|
563
|
-
return false;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
function registerDispatchReceiver(receiver) {
|
|
567
|
-
dispatchReceiver = receiver;
|
|
568
|
-
}
|
|
569
|
-
function takeDispatchFrame(text) {
|
|
570
|
-
if (!dispatchReceiver) return false;
|
|
571
|
-
try {
|
|
572
|
-
return dispatchReceiver(text);
|
|
573
|
-
} catch {
|
|
574
|
-
return true;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
var LOCK_STALE_MS = 2e4;
|
|
578
|
-
function hubDir() {
|
|
579
|
-
return path2.join(resolveConfigDir(), "im-forward");
|
|
580
|
-
}
|
|
581
|
-
function lockPath(binding) {
|
|
582
|
-
const safe = binding.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
583
|
-
return path2.join(hubDir(), `consumer-${safe}.lock`);
|
|
584
|
-
}
|
|
585
|
-
function pidAlive(pid) {
|
|
586
|
-
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
587
|
-
try {
|
|
588
|
-
process.kill(pid, 0);
|
|
589
|
-
return true;
|
|
590
|
-
} catch (error) {
|
|
591
|
-
return error.code === "EPERM";
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
function readLock(binding) {
|
|
595
|
-
try {
|
|
596
|
-
const parsed = JSON.parse(fs2.readFileSync(lockPath(binding), "utf8"));
|
|
597
|
-
if (typeof parsed.pid === "number" && typeof parsed.holder === "string" && typeof parsed.heartbeatAt === "number" && typeof parsed.binding === "string") {
|
|
598
|
-
return {
|
|
599
|
-
pid: parsed.pid,
|
|
600
|
-
holder: parsed.holder,
|
|
601
|
-
heartbeatAt: parsed.heartbeatAt,
|
|
602
|
-
binding: parsed.binding
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
return void 0;
|
|
606
|
-
} catch {
|
|
607
|
-
return void 0;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
function lockAlive(info, now = Date.now()) {
|
|
611
|
-
if (!info) return false;
|
|
612
|
-
return pidAlive(info.pid) && now - info.heartbeatAt < LOCK_STALE_MS;
|
|
613
|
-
}
|
|
614
|
-
function tryClaimConsumer(binding, holder, onLost, shouldYield) {
|
|
615
|
-
const target = lockPath(binding);
|
|
616
|
-
const mine = JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding });
|
|
617
|
-
const takeOver = () => {
|
|
618
|
-
const tmp = `${target}.${process.pid}.${Date.now()}.tmp`;
|
|
619
|
-
try {
|
|
620
|
-
fs2.mkdirSync(hubDir(), { recursive: true, mode: 448 });
|
|
621
|
-
fs2.writeFileSync(tmp, `${mine}
|
|
622
|
-
`);
|
|
623
|
-
try {
|
|
624
|
-
fs2.linkSync(tmp, target);
|
|
625
|
-
fs2.rmSync(tmp, { force: true });
|
|
626
|
-
return true;
|
|
627
|
-
} catch (error) {
|
|
628
|
-
fs2.rmSync(tmp, { force: true });
|
|
629
|
-
if (error.code !== "EEXIST") throw error;
|
|
630
|
-
return false;
|
|
631
|
-
}
|
|
632
|
-
} catch {
|
|
633
|
-
return false;
|
|
634
|
-
}
|
|
635
|
-
};
|
|
636
|
-
const existing = readLock(binding);
|
|
637
|
-
if (existing && lockAlive(existing)) return void 0;
|
|
638
|
-
if (existing) {
|
|
639
|
-
try {
|
|
640
|
-
fs2.rmSync(target, { force: true });
|
|
641
|
-
} catch {
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
if (!takeOver()) return void 0;
|
|
645
|
-
let released = false;
|
|
646
|
-
const heartbeatTimer = setInterval(() => {
|
|
647
|
-
if (released) return;
|
|
648
|
-
try {
|
|
649
|
-
const current = readLock(binding);
|
|
650
|
-
if (current && current.pid !== process.pid) {
|
|
651
|
-
released = true;
|
|
652
|
-
clearInterval(heartbeatTimer);
|
|
653
|
-
onLost?.();
|
|
654
|
-
return;
|
|
655
|
-
}
|
|
656
|
-
if (shouldYield?.() === true) {
|
|
657
|
-
released = true;
|
|
658
|
-
clearInterval(heartbeatTimer);
|
|
659
|
-
try {
|
|
660
|
-
if (readLock(binding)?.pid === process.pid) fs2.rmSync(target, { force: true });
|
|
661
|
-
} catch {
|
|
662
|
-
}
|
|
663
|
-
onLost?.();
|
|
664
|
-
return;
|
|
665
|
-
}
|
|
666
|
-
fs2.writeFileSync(target, `${JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding })}
|
|
667
|
-
`);
|
|
668
|
-
} catch {
|
|
669
|
-
}
|
|
670
|
-
}, 5e3);
|
|
671
|
-
heartbeatTimer.unref();
|
|
672
|
-
return {
|
|
673
|
-
heartbeat: () => {
|
|
674
|
-
try {
|
|
675
|
-
fs2.writeFileSync(target, `${JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding })}
|
|
676
|
-
`);
|
|
677
|
-
} catch {
|
|
678
|
-
}
|
|
679
|
-
},
|
|
680
|
-
release: () => {
|
|
681
|
-
if (released) return;
|
|
682
|
-
released = true;
|
|
683
|
-
clearInterval(heartbeatTimer);
|
|
684
|
-
try {
|
|
685
|
-
const current = readLock(binding);
|
|
686
|
-
if (current && current.pid === process.pid) fs2.rmSync(target, { force: true });
|
|
687
|
-
} catch {
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
};
|
|
691
|
-
}
|
|
692
|
-
|
|
693
556
|
// builtin/extensions/pie-ipc/pure.ts
|
|
694
557
|
import { randomBytes } from "node:crypto";
|
|
695
558
|
var IPC_PROTOCOL_VERSION = 2;
|
|
@@ -1023,6 +886,502 @@ function assistantBodyText(message) {
|
|
|
1023
886
|
return text.trim();
|
|
1024
887
|
}
|
|
1025
888
|
|
|
889
|
+
// builtin/extensions/pie-ipc/transport.ts
|
|
890
|
+
import * as net from "node:net";
|
|
891
|
+
import { createHmac, randomBytes as randomBytes2, randomUUID as randomUUID2, timingSafeEqual } from "node:crypto";
|
|
892
|
+
import { StringDecoder } from "node:string_decoder";
|
|
893
|
+
var CLOSE_FLUSH_TIMEOUT_MS = 1e3;
|
|
894
|
+
function signEnvelope(e, secret) {
|
|
895
|
+
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];
|
|
896
|
+
return { ...e, auth: createHmac("sha256", secret).update(JSON.stringify(payload)).digest("hex") };
|
|
897
|
+
}
|
|
898
|
+
function verified(e, secret) {
|
|
899
|
+
if (!e.auth || !/^[a-f0-9]{64}$/.test(e.auth)) return false;
|
|
900
|
+
return timingSafeEqual(Buffer.from(e.auth, "hex"), Buffer.from(signEnvelope(e, secret).auth, "hex"));
|
|
901
|
+
}
|
|
902
|
+
var IpcTransport = class {
|
|
903
|
+
constructor(host) {
|
|
904
|
+
this.host = host;
|
|
905
|
+
this.instanceId = host.instanceId;
|
|
906
|
+
this.secret = host.secret ?? randomBytes2(32).toString("hex");
|
|
907
|
+
this.server = net.createServer((socket) => this.accept(socket));
|
|
908
|
+
this.server.on("error", () => {
|
|
909
|
+
});
|
|
910
|
+
this.server.unref();
|
|
911
|
+
}
|
|
912
|
+
host;
|
|
913
|
+
instanceId;
|
|
914
|
+
secret;
|
|
915
|
+
server;
|
|
916
|
+
sockets = /* @__PURE__ */ new Set();
|
|
917
|
+
cancels = /* @__PURE__ */ new Set();
|
|
918
|
+
seen = new BoundedIdSet();
|
|
919
|
+
closed = false;
|
|
920
|
+
inflight = 0;
|
|
921
|
+
inbound = 0;
|
|
922
|
+
rateWindow = Date.now();
|
|
923
|
+
rateCount = 0;
|
|
924
|
+
port = null;
|
|
925
|
+
get listenPort() {
|
|
926
|
+
return this.port;
|
|
927
|
+
}
|
|
928
|
+
listen() {
|
|
929
|
+
if (this.closed) return Promise.reject(new Error("closed"));
|
|
930
|
+
return new Promise((resolve2, reject) => {
|
|
931
|
+
const cancel = () => {
|
|
932
|
+
this.cancels.delete(cancel);
|
|
933
|
+
reject(new Error("closed"));
|
|
934
|
+
};
|
|
935
|
+
this.cancels.add(cancel);
|
|
936
|
+
const error = (e) => {
|
|
937
|
+
this.cancels.delete(cancel);
|
|
938
|
+
reject(e);
|
|
939
|
+
};
|
|
940
|
+
this.server.once("error", error);
|
|
941
|
+
this.server.listen(0, "127.0.0.1", () => {
|
|
942
|
+
this.server.off("error", error);
|
|
943
|
+
this.cancels.delete(cancel);
|
|
944
|
+
if (this.closed) {
|
|
945
|
+
this.server.close();
|
|
946
|
+
reject(new Error("closed"));
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
const address = this.server.address();
|
|
950
|
+
if (!address || typeof address === "string") {
|
|
951
|
+
reject(new Error("closed"));
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
this.port = address.port;
|
|
955
|
+
resolve2(address.port);
|
|
956
|
+
});
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
write(socket, envelope, secret) {
|
|
960
|
+
if (socket.destroyed) return false;
|
|
961
|
+
const data = JSON.stringify(signEnvelope(envelope, secret)) + "\n";
|
|
962
|
+
if (socket.writableLength + Buffer.byteLength(data) > 512 * 1024) {
|
|
963
|
+
socket.destroy();
|
|
964
|
+
return false;
|
|
965
|
+
}
|
|
966
|
+
try {
|
|
967
|
+
socket.write(data);
|
|
968
|
+
return true;
|
|
969
|
+
} catch {
|
|
970
|
+
socket.destroy();
|
|
971
|
+
return false;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
read(socket, receive, idleMs) {
|
|
975
|
+
const decoder = new FrameDecoder();
|
|
976
|
+
const utf8 = new StringDecoder("utf8");
|
|
977
|
+
let frameTimer;
|
|
978
|
+
let frames = 0;
|
|
979
|
+
socket.setTimeout(idleMs, () => socket.destroy());
|
|
980
|
+
socket.on("error", () => socket.destroy());
|
|
981
|
+
socket.on("close", () => {
|
|
982
|
+
if (frameTimer) clearTimeout(frameTimer);
|
|
983
|
+
this.sockets.delete(socket);
|
|
984
|
+
});
|
|
985
|
+
socket.on("data", (chunk) => {
|
|
986
|
+
try {
|
|
987
|
+
frames += chunk.reduce((n, b) => n + (b === 10 ? 1 : 0), 0);
|
|
988
|
+
if (frames > 128) {
|
|
989
|
+
socket.destroy();
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
const envelopes = decoder.push(utf8.write(chunk));
|
|
993
|
+
const partial = decoder.partial || chunk.at(-1) !== 10;
|
|
994
|
+
if (frameTimer && (!partial || chunk.includes(10))) {
|
|
995
|
+
clearTimeout(frameTimer);
|
|
996
|
+
frameTimer = void 0;
|
|
997
|
+
}
|
|
998
|
+
if (partial && !frameTimer) {
|
|
999
|
+
frameTimer = setTimeout(() => socket.destroy(), 15e3);
|
|
1000
|
+
frameTimer.unref();
|
|
1001
|
+
}
|
|
1002
|
+
for (const e of envelopes) {
|
|
1003
|
+
if (socket.destroyed) break;
|
|
1004
|
+
receive(e);
|
|
1005
|
+
}
|
|
1006
|
+
} catch {
|
|
1007
|
+
socket.destroy();
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
accept(socket) {
|
|
1012
|
+
if (this.closed || this.inbound >= 16) {
|
|
1013
|
+
socket.destroy();
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
this.inbound++;
|
|
1017
|
+
this.sockets.add(socket);
|
|
1018
|
+
socket.setNoDelay(true);
|
|
1019
|
+
socket.once("close", () => {
|
|
1020
|
+
this.inbound--;
|
|
1021
|
+
});
|
|
1022
|
+
const lifecycle = new AbortController();
|
|
1023
|
+
socket.once("close", () => lifecycle.abort());
|
|
1024
|
+
let sender;
|
|
1025
|
+
this.read(socket, (e) => {
|
|
1026
|
+
if (e.to !== this.instanceId || !verified(e, this.secret) || sender && sender !== e.from) {
|
|
1027
|
+
socket.destroy();
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
sender = e.from;
|
|
1031
|
+
if (Date.now() - this.rateWindow >= 1e3) {
|
|
1032
|
+
this.rateWindow = Date.now();
|
|
1033
|
+
this.rateCount = 0;
|
|
1034
|
+
}
|
|
1035
|
+
if (++this.rateCount > 128) {
|
|
1036
|
+
socket.destroy();
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
if (e.waitMs) socket.setTimeout(Math.max(6e4, e.waitMs + 1e3));
|
|
1040
|
+
if (!this.seen.add(`${e.from}:${e.id}`)) return;
|
|
1041
|
+
try {
|
|
1042
|
+
if (this.closed) return;
|
|
1043
|
+
void Promise.resolve(this.host.onEnvelope(e, (reply) => {
|
|
1044
|
+
if (reply.from === this.instanceId && reply.to === e.from && reply.replyTo === e.id) this.write(socket, reply, this.secret);
|
|
1045
|
+
}, lifecycle.signal)).catch(() => socket.destroy());
|
|
1046
|
+
} catch {
|
|
1047
|
+
socket.destroy();
|
|
1048
|
+
}
|
|
1049
|
+
}, 6e4);
|
|
1050
|
+
}
|
|
1051
|
+
/** Synchronous CAPACITY probe (review r3): transport.send is async, so a
|
|
1052
|
+
* capacity rejection still yields a truthy Promise resolving null — callers
|
|
1053
|
+
* distinguishing 'never left the process' from 'sent, outcome unknown' must
|
|
1054
|
+
* check this BEFORE awaiting send. Scope: closed/inflight ONLY (capacity &
|
|
1055
|
+
* lifecycle), NOT send()'s full validation (signal/secret/port/waitMs can
|
|
1056
|
+
* still null-reject after a true probe — those are peer/discovery outcomes,
|
|
1057
|
+
* covered by the best-effort contract). */
|
|
1058
|
+
canSendNow() {
|
|
1059
|
+
return !this.closed && this.inflight < MAX_INFLIGHT_SENDS;
|
|
1060
|
+
}
|
|
1061
|
+
async send(peerInstanceId, port, kind, options = {}) {
|
|
1062
|
+
if (this.closed || options.signal?.aborted || this.inflight >= MAX_INFLIGHT_SENDS) return null;
|
|
1063
|
+
let secret;
|
|
1064
|
+
try {
|
|
1065
|
+
secret = this.host.resolveSecret?.(peerInstanceId);
|
|
1066
|
+
} catch {
|
|
1067
|
+
return null;
|
|
1068
|
+
}
|
|
1069
|
+
if (!secret || !/^[a-f0-9]{64}$/.test(secret) || !Number.isInteger(port) || port < 1 || port > 65535) return null;
|
|
1070
|
+
const waitMs = options.waitMs ?? DEFAULT_SEND_WAIT_MS;
|
|
1071
|
+
if (!Number.isFinite(waitMs) || waitMs < 0 || waitMs > 6e5) return null;
|
|
1072
|
+
const holdOpen = options.expectReply === true || options.awaitAck === true;
|
|
1073
|
+
this.inflight++;
|
|
1074
|
+
return new Promise((resolve2) => {
|
|
1075
|
+
const socket = new net.Socket();
|
|
1076
|
+
this.sockets.add(socket);
|
|
1077
|
+
const envelope = { v: IPC_PROTOCOL_VERSION, id: options.requestId ?? randomUUID2(), 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 } : {} };
|
|
1078
|
+
let done = false;
|
|
1079
|
+
const finish = (value) => {
|
|
1080
|
+
if (done) return;
|
|
1081
|
+
done = true;
|
|
1082
|
+
clearTimeout(timer);
|
|
1083
|
+
clearTimeout(dialTimer);
|
|
1084
|
+
options.signal?.removeEventListener("abort", abort);
|
|
1085
|
+
this.cancels.delete(abort);
|
|
1086
|
+
this.inflight--;
|
|
1087
|
+
socket.destroy();
|
|
1088
|
+
resolve2(value);
|
|
1089
|
+
};
|
|
1090
|
+
const abort = () => finish(null);
|
|
1091
|
+
const timer = setTimeout(abort, holdOpen ? waitMs : 3e3);
|
|
1092
|
+
const dialTimer = setTimeout(abort, 3e3);
|
|
1093
|
+
this.cancels.add(abort);
|
|
1094
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
1095
|
+
socket.once("close", abort);
|
|
1096
|
+
this.read(socket, (e) => {
|
|
1097
|
+
if (e.to !== this.instanceId || e.from !== peerInstanceId || e.replyTo !== envelope.id || !verified(e, secret)) {
|
|
1098
|
+
finish(null);
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
if ((options.replyKinds ?? /* @__PURE__ */ new Set(["result", "ack", "state"])).has(e.kind)) finish(e);
|
|
1102
|
+
}, Math.max(waitMs, 3e3));
|
|
1103
|
+
socket.once("connect", () => {
|
|
1104
|
+
clearTimeout(dialTimer);
|
|
1105
|
+
if (done || options.signal?.aborted || this.closed) {
|
|
1106
|
+
finish(null);
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
if (!this.write(socket, envelope, secret)) {
|
|
1110
|
+
finish(null);
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
if (!holdOpen) {
|
|
1114
|
+
socket.end(() => finish(envelope));
|
|
1115
|
+
}
|
|
1116
|
+
});
|
|
1117
|
+
try {
|
|
1118
|
+
socket.connect(port, "127.0.0.1");
|
|
1119
|
+
} catch {
|
|
1120
|
+
finish(null);
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
close() {
|
|
1125
|
+
if (this.closed) return;
|
|
1126
|
+
this.closed = true;
|
|
1127
|
+
for (const cancel of [...this.cancels]) cancel();
|
|
1128
|
+
for (const socket of this.sockets) {
|
|
1129
|
+
if (socket.destroyed) continue;
|
|
1130
|
+
socket.end();
|
|
1131
|
+
const timer = setTimeout(() => socket.destroy(), CLOSE_FLUSH_TIMEOUT_MS);
|
|
1132
|
+
timer.unref();
|
|
1133
|
+
}
|
|
1134
|
+
this.sockets.clear();
|
|
1135
|
+
this.server.close();
|
|
1136
|
+
this.port = null;
|
|
1137
|
+
}
|
|
1138
|
+
};
|
|
1139
|
+
|
|
1140
|
+
// builtin/extensions/im-forward/event-hub.ts
|
|
1141
|
+
import * as fs3 from "node:fs";
|
|
1142
|
+
import * as path3 from "node:path";
|
|
1143
|
+
|
|
1144
|
+
// builtin/extensions/im-forward/address-book.ts
|
|
1145
|
+
import * as fs2 from "node:fs";
|
|
1146
|
+
import * as os2 from "node:os";
|
|
1147
|
+
import * as path2 from "node:path";
|
|
1148
|
+
var MAX_ADDRESS_HEAD = 64;
|
|
1149
|
+
function addressBookRoot() {
|
|
1150
|
+
const raw = process.env.PIE_IPC_CONFIG_DIR;
|
|
1151
|
+
if (raw !== void 0) {
|
|
1152
|
+
if (raw.length > 0) {
|
|
1153
|
+
const expanded = raw === "~" ? os2.homedir() : raw.startsWith("~/") ? path2.join(os2.homedir(), raw.slice(2)) : raw;
|
|
1154
|
+
return path2.resolve(expanded);
|
|
1155
|
+
}
|
|
1156
|
+
return path2.resolve("");
|
|
1157
|
+
}
|
|
1158
|
+
return path2.resolve(resolveConfigDir());
|
|
1159
|
+
}
|
|
1160
|
+
function pidAlive(pid) {
|
|
1161
|
+
if (!Number.isInteger(pid) || pid <= 0) return true;
|
|
1162
|
+
try {
|
|
1163
|
+
process.kill(pid, 0);
|
|
1164
|
+
return true;
|
|
1165
|
+
} catch (error) {
|
|
1166
|
+
return error.code === "EPERM";
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function scanAddressBook() {
|
|
1170
|
+
const names = /* @__PURE__ */ new Map();
|
|
1171
|
+
let complete = true;
|
|
1172
|
+
let selfPublished;
|
|
1173
|
+
let dirExists = false;
|
|
1174
|
+
const dir = path2.join(addressBookRoot(), "run", "ipc");
|
|
1175
|
+
let entries;
|
|
1176
|
+
try {
|
|
1177
|
+
entries = fs2.readdirSync(dir);
|
|
1178
|
+
dirExists = true;
|
|
1179
|
+
} catch (error) {
|
|
1180
|
+
if (error.code === "ENOENT") return { names, complete: true, dirExists: false };
|
|
1181
|
+
return { names, complete: false, dirExists: true };
|
|
1182
|
+
}
|
|
1183
|
+
for (const [index, file] of entries.entries()) {
|
|
1184
|
+
if (!file.endsWith(".json")) continue;
|
|
1185
|
+
try {
|
|
1186
|
+
const status = JSON.parse(fs2.readFileSync(path2.join(dir, file), "utf8"));
|
|
1187
|
+
dirExists = true;
|
|
1188
|
+
const name = typeof status.sessionName === "string" ? status.sessionName.trim() : "";
|
|
1189
|
+
const pid = typeof status.pid === "number" ? status.pid : void 0;
|
|
1190
|
+
if (!name) continue;
|
|
1191
|
+
if (pid === process.pid) {
|
|
1192
|
+
selfPublished = name;
|
|
1193
|
+
continue;
|
|
1194
|
+
}
|
|
1195
|
+
if (pid !== void 0 && !pidAlive(pid)) continue;
|
|
1196
|
+
const owners = names.get(name) ?? /* @__PURE__ */ new Set();
|
|
1197
|
+
owners.add(pid ?? -(index + 1));
|
|
1198
|
+
names.set(name, owners);
|
|
1199
|
+
} catch {
|
|
1200
|
+
complete = false;
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
return { names, selfPublished, complete, dirExists };
|
|
1204
|
+
}
|
|
1205
|
+
var SEPARATORS = [":", "\uFF1A"];
|
|
1206
|
+
function matchAddressName(text, candidates) {
|
|
1207
|
+
const trimmed = text.trimStart();
|
|
1208
|
+
let best;
|
|
1209
|
+
for (const name of candidates) {
|
|
1210
|
+
const value = name?.trim();
|
|
1211
|
+
if (!value) continue;
|
|
1212
|
+
if (!SEPARATORS.some((sep) => trimmed.startsWith(`${value}${sep}`))) continue;
|
|
1213
|
+
if (!best || value.length > best.length) best = value;
|
|
1214
|
+
}
|
|
1215
|
+
return best;
|
|
1216
|
+
}
|
|
1217
|
+
function resolveAddress(text, ownLiveName, book) {
|
|
1218
|
+
if (!/[::]/.test(text)) return { kind: "none" };
|
|
1219
|
+
const resolvedBook = book ?? scanAddressBook();
|
|
1220
|
+
const candidates = new Set(resolvedBook.names.keys());
|
|
1221
|
+
if (resolvedBook.selfPublished) candidates.add(resolvedBook.selfPublished);
|
|
1222
|
+
const live = ownLiveName?.trim();
|
|
1223
|
+
if (live) candidates.add(live);
|
|
1224
|
+
const name = matchAddressName(text, candidates);
|
|
1225
|
+
if (!name) {
|
|
1226
|
+
if (!resolvedBook.complete) {
|
|
1227
|
+
return { kind: "ambiguous", name: text.trim().slice(0, MAX_ADDRESS_HEAD), reason: "address book unreadable" };
|
|
1228
|
+
}
|
|
1229
|
+
return { kind: "none" };
|
|
1230
|
+
}
|
|
1231
|
+
const body = text.trimStart().slice(name.length + 1).trim();
|
|
1232
|
+
const foreignOwners = resolvedBook.names.get(name);
|
|
1233
|
+
if (foreignOwners && foreignOwners.size > 0) {
|
|
1234
|
+
if (resolvedBook.selfPublished === name || live === name) return { kind: "ambiguous", name, reason: "duplicate live session name" };
|
|
1235
|
+
return { kind: "other", name };
|
|
1236
|
+
}
|
|
1237
|
+
if (!resolvedBook.complete) return { kind: "ambiguous", name, reason: "address book unreadable" };
|
|
1238
|
+
const acceptName = resolvedBook.selfPublished ?? (resolvedBook.dirExists ? void 0 : live);
|
|
1239
|
+
if (!acceptName) return { kind: "ambiguous", name, reason: "own advertisement not published" };
|
|
1240
|
+
if (name === acceptName) return { kind: "self", body };
|
|
1241
|
+
return { kind: "ambiguous", name, reason: "unpublished address" };
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
// builtin/extensions/im-forward/event-hub.ts
|
|
1245
|
+
var DISPATCH_PREFIX = "[im-forward-dispatch]";
|
|
1246
|
+
var dispatchReceiver;
|
|
1247
|
+
var turnImDriverOracle;
|
|
1248
|
+
function registerTurnImDriver(oracle) {
|
|
1249
|
+
turnImDriverOracle = oracle;
|
|
1250
|
+
}
|
|
1251
|
+
function isTurnUserImDriven() {
|
|
1252
|
+
try {
|
|
1253
|
+
return turnImDriverOracle?.() === true;
|
|
1254
|
+
} catch {
|
|
1255
|
+
return false;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
function registerDispatchReceiver(receiver) {
|
|
1259
|
+
dispatchReceiver = receiver;
|
|
1260
|
+
}
|
|
1261
|
+
function takeDispatchFrame(text) {
|
|
1262
|
+
if (!dispatchReceiver) return false;
|
|
1263
|
+
try {
|
|
1264
|
+
return dispatchReceiver(text);
|
|
1265
|
+
} catch {
|
|
1266
|
+
return true;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
var LOCK_STALE_MS = 2e4;
|
|
1270
|
+
function hubDir() {
|
|
1271
|
+
return path3.join(addressBookRoot(), "im-forward");
|
|
1272
|
+
}
|
|
1273
|
+
function lockPath(binding) {
|
|
1274
|
+
const safe = binding.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
1275
|
+
return path3.join(hubDir(), `consumer-${safe}.lock`);
|
|
1276
|
+
}
|
|
1277
|
+
function pidAlive2(pid) {
|
|
1278
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
1279
|
+
try {
|
|
1280
|
+
process.kill(pid, 0);
|
|
1281
|
+
return true;
|
|
1282
|
+
} catch (error) {
|
|
1283
|
+
return error.code === "EPERM";
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
function readLock(binding) {
|
|
1287
|
+
try {
|
|
1288
|
+
const parsed = JSON.parse(fs3.readFileSync(lockPath(binding), "utf8"));
|
|
1289
|
+
if (typeof parsed.pid === "number" && typeof parsed.holder === "string" && typeof parsed.heartbeatAt === "number" && typeof parsed.binding === "string") {
|
|
1290
|
+
return {
|
|
1291
|
+
pid: parsed.pid,
|
|
1292
|
+
holder: parsed.holder,
|
|
1293
|
+
heartbeatAt: parsed.heartbeatAt,
|
|
1294
|
+
binding: parsed.binding
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
return void 0;
|
|
1298
|
+
} catch {
|
|
1299
|
+
return void 0;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
function lockAlive(info, now = Date.now()) {
|
|
1303
|
+
if (!info) return false;
|
|
1304
|
+
return pidAlive2(info.pid) && now - info.heartbeatAt < LOCK_STALE_MS;
|
|
1305
|
+
}
|
|
1306
|
+
function tryClaimConsumer(binding, holder, onLost, shouldYield) {
|
|
1307
|
+
const target = lockPath(binding);
|
|
1308
|
+
const mine = JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding });
|
|
1309
|
+
const takeOver = () => {
|
|
1310
|
+
const tmp = `${target}.${process.pid}.${Date.now()}.tmp`;
|
|
1311
|
+
try {
|
|
1312
|
+
fs3.mkdirSync(hubDir(), { recursive: true, mode: 448 });
|
|
1313
|
+
fs3.writeFileSync(tmp, `${mine}
|
|
1314
|
+
`);
|
|
1315
|
+
try {
|
|
1316
|
+
fs3.linkSync(tmp, target);
|
|
1317
|
+
fs3.rmSync(tmp, { force: true });
|
|
1318
|
+
return true;
|
|
1319
|
+
} catch (error) {
|
|
1320
|
+
fs3.rmSync(tmp, { force: true });
|
|
1321
|
+
if (error.code !== "EEXIST") throw error;
|
|
1322
|
+
return false;
|
|
1323
|
+
}
|
|
1324
|
+
} catch {
|
|
1325
|
+
return false;
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
const existing = readLock(binding);
|
|
1329
|
+
if (existing && lockAlive(existing)) return void 0;
|
|
1330
|
+
if (existing) {
|
|
1331
|
+
try {
|
|
1332
|
+
fs3.rmSync(target, { force: true });
|
|
1333
|
+
} catch {
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
if (!takeOver()) return void 0;
|
|
1337
|
+
let released = false;
|
|
1338
|
+
const heartbeatTimer = setInterval(() => {
|
|
1339
|
+
if (released) return;
|
|
1340
|
+
try {
|
|
1341
|
+
const current = readLock(binding);
|
|
1342
|
+
if (current && current.pid !== process.pid) {
|
|
1343
|
+
released = true;
|
|
1344
|
+
clearInterval(heartbeatTimer);
|
|
1345
|
+
onLost?.();
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
if (shouldYield?.() === true) {
|
|
1349
|
+
released = true;
|
|
1350
|
+
clearInterval(heartbeatTimer);
|
|
1351
|
+
try {
|
|
1352
|
+
if (readLock(binding)?.pid === process.pid) fs3.rmSync(target, { force: true });
|
|
1353
|
+
} catch {
|
|
1354
|
+
}
|
|
1355
|
+
onLost?.();
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
fs3.writeFileSync(target, `${JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding })}
|
|
1359
|
+
`);
|
|
1360
|
+
} catch {
|
|
1361
|
+
}
|
|
1362
|
+
}, 5e3);
|
|
1363
|
+
heartbeatTimer.unref();
|
|
1364
|
+
return {
|
|
1365
|
+
heartbeat: () => {
|
|
1366
|
+
try {
|
|
1367
|
+
fs3.writeFileSync(target, `${JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding })}
|
|
1368
|
+
`);
|
|
1369
|
+
} catch {
|
|
1370
|
+
}
|
|
1371
|
+
},
|
|
1372
|
+
release: () => {
|
|
1373
|
+
if (released) return;
|
|
1374
|
+
released = true;
|
|
1375
|
+
clearInterval(heartbeatTimer);
|
|
1376
|
+
try {
|
|
1377
|
+
const current = readLock(binding);
|
|
1378
|
+
if (current && current.pid === process.pid) fs3.rmSync(target, { force: true });
|
|
1379
|
+
} catch {
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1026
1385
|
export {
|
|
1027
1386
|
isRecord,
|
|
1028
1387
|
assistantForwardText,
|
|
@@ -1046,12 +1405,8 @@ export {
|
|
|
1046
1405
|
readLiveEnabled,
|
|
1047
1406
|
readLiveConfig,
|
|
1048
1407
|
resolveImForwardConfig,
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
isTurnUserImDriven,
|
|
1052
|
-
registerDispatchReceiver,
|
|
1053
|
-
takeDispatchFrame,
|
|
1054
|
-
tryClaimConsumer,
|
|
1408
|
+
addressBookRoot,
|
|
1409
|
+
resolveAddress,
|
|
1055
1410
|
IPC_PROTOCOL_VERSION,
|
|
1056
1411
|
MAX_STATUS_FILE_BYTES,
|
|
1057
1412
|
MAX_BODY_CHARS,
|
|
@@ -1059,19 +1414,24 @@ export {
|
|
|
1059
1414
|
HEARTBEAT_INTERVAL_MS,
|
|
1060
1415
|
DEFAULT_SEND_WAIT_MS,
|
|
1061
1416
|
MAX_INBOX_MESSAGES,
|
|
1062
|
-
MAX_INFLIGHT_SENDS,
|
|
1063
1417
|
generateInstanceId,
|
|
1064
1418
|
isValidInstanceId,
|
|
1065
1419
|
peerLabel,
|
|
1066
|
-
FrameDecoder,
|
|
1067
1420
|
wrapInboundText,
|
|
1068
1421
|
isWrappedInboundText,
|
|
1069
1422
|
wrappedMessageMarker,
|
|
1070
1423
|
clipBody,
|
|
1071
|
-
BoundedIdSet,
|
|
1072
1424
|
peerHealth,
|
|
1073
1425
|
parseStatusFile,
|
|
1074
1426
|
foldLifecycleEvent,
|
|
1075
1427
|
resolveIpcConfig,
|
|
1076
|
-
assistantBodyText
|
|
1428
|
+
assistantBodyText,
|
|
1429
|
+
signEnvelope,
|
|
1430
|
+
IpcTransport,
|
|
1431
|
+
DISPATCH_PREFIX,
|
|
1432
|
+
registerTurnImDriver,
|
|
1433
|
+
isTurnUserImDriven,
|
|
1434
|
+
registerDispatchReceiver,
|
|
1435
|
+
takeDispatchFrame,
|
|
1436
|
+
tryClaimConsumer
|
|
1077
1437
|
};
|