@cydm/pie 4.3.11 → 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 +76 -9
- package/dist/builtin/extensions/im-forward/README.md +17 -1
- package/dist/builtin/extensions/im-forward/index.js +478 -95
- 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 +49 -268
- package/dist/builtin/extensions/pie-proc/index.js +1 -1
- 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-DGXIR6CV.js → chunk-EGNGHINP.js} +516 -266
- package/dist/chunks/{chunk-ZAJEKDPV.js → chunk-GY47MKCJ.js} +2 -2
- package/dist/chunks/{chunk-KCZGLNRM.js → chunk-I7PBXG5Q.js} +1 -1
- package/dist/chunks/{chunk-LLCEO7ET.js → chunk-N5B5IGJ6.js} +350 -231
- package/dist/chunks/{src-5EZU72ZA.js → src-V2M7FLWY.js} +5 -1
- package/dist/cli.js +39 -6
- package/package.json +2 -2
|
@@ -553,251 +553,6 @@ function resolveImForwardConfig(cwd, dir) {
|
|
|
553
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_...)"] };
|
|
554
554
|
}
|
|
555
555
|
|
|
556
|
-
// builtin/extensions/im-forward/event-hub.ts
|
|
557
|
-
import * as fs3 from "node:fs";
|
|
558
|
-
import * as path3 from "node:path";
|
|
559
|
-
|
|
560
|
-
// builtin/extensions/im-forward/address-book.ts
|
|
561
|
-
import * as fs2 from "node:fs";
|
|
562
|
-
import * as os2 from "node:os";
|
|
563
|
-
import * as path2 from "node:path";
|
|
564
|
-
var MAX_ADDRESS_HEAD = 64;
|
|
565
|
-
function addressBookRoot() {
|
|
566
|
-
const raw = process.env.PIE_IPC_CONFIG_DIR;
|
|
567
|
-
if (raw !== void 0) {
|
|
568
|
-
if (raw.length > 0) {
|
|
569
|
-
const expanded = raw === "~" ? os2.homedir() : raw.startsWith("~/") ? path2.join(os2.homedir(), raw.slice(2)) : raw;
|
|
570
|
-
return path2.resolve(expanded);
|
|
571
|
-
}
|
|
572
|
-
return path2.resolve("");
|
|
573
|
-
}
|
|
574
|
-
return path2.resolve(resolveConfigDir());
|
|
575
|
-
}
|
|
576
|
-
function pidAlive(pid) {
|
|
577
|
-
if (!Number.isInteger(pid) || pid <= 0) return true;
|
|
578
|
-
try {
|
|
579
|
-
process.kill(pid, 0);
|
|
580
|
-
return true;
|
|
581
|
-
} catch (error) {
|
|
582
|
-
return error.code === "EPERM";
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
function scanAddressBook() {
|
|
586
|
-
const names = /* @__PURE__ */ new Map();
|
|
587
|
-
let complete = true;
|
|
588
|
-
let selfPublished;
|
|
589
|
-
let dirExists = false;
|
|
590
|
-
const dir = path2.join(addressBookRoot(), "run", "ipc");
|
|
591
|
-
let entries;
|
|
592
|
-
try {
|
|
593
|
-
entries = fs2.readdirSync(dir);
|
|
594
|
-
dirExists = true;
|
|
595
|
-
} catch (error) {
|
|
596
|
-
if (error.code === "ENOENT") return { names, complete: true, dirExists: false };
|
|
597
|
-
return { names, complete: false, dirExists: true };
|
|
598
|
-
}
|
|
599
|
-
for (const [index, file] of entries.entries()) {
|
|
600
|
-
if (!file.endsWith(".json")) continue;
|
|
601
|
-
try {
|
|
602
|
-
const status = JSON.parse(fs2.readFileSync(path2.join(dir, file), "utf8"));
|
|
603
|
-
dirExists = true;
|
|
604
|
-
const name = typeof status.sessionName === "string" ? status.sessionName.trim() : "";
|
|
605
|
-
const pid = typeof status.pid === "number" ? status.pid : void 0;
|
|
606
|
-
if (!name) continue;
|
|
607
|
-
if (pid === process.pid) {
|
|
608
|
-
selfPublished = name;
|
|
609
|
-
continue;
|
|
610
|
-
}
|
|
611
|
-
if (pid !== void 0 && !pidAlive(pid)) continue;
|
|
612
|
-
const owners = names.get(name) ?? /* @__PURE__ */ new Set();
|
|
613
|
-
owners.add(pid ?? -(index + 1));
|
|
614
|
-
names.set(name, owners);
|
|
615
|
-
} catch {
|
|
616
|
-
complete = false;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
return { names, selfPublished, complete, dirExists };
|
|
620
|
-
}
|
|
621
|
-
var SEPARATORS = [":", "\uFF1A"];
|
|
622
|
-
function matchAddressName(text, candidates) {
|
|
623
|
-
const trimmed = text.trimStart();
|
|
624
|
-
let best;
|
|
625
|
-
for (const name of candidates) {
|
|
626
|
-
const value = name?.trim();
|
|
627
|
-
if (!value) continue;
|
|
628
|
-
if (!SEPARATORS.some((sep) => trimmed.startsWith(`${value}${sep}`))) continue;
|
|
629
|
-
if (!best || value.length > best.length) best = value;
|
|
630
|
-
}
|
|
631
|
-
return best;
|
|
632
|
-
}
|
|
633
|
-
function resolveAddress(text, ownLiveName, book) {
|
|
634
|
-
if (!/[::]/.test(text)) return { kind: "none" };
|
|
635
|
-
const resolvedBook = book ?? scanAddressBook();
|
|
636
|
-
const candidates = new Set(resolvedBook.names.keys());
|
|
637
|
-
if (resolvedBook.selfPublished) candidates.add(resolvedBook.selfPublished);
|
|
638
|
-
const live = ownLiveName?.trim();
|
|
639
|
-
if (live) candidates.add(live);
|
|
640
|
-
const name = matchAddressName(text, candidates);
|
|
641
|
-
if (!name) {
|
|
642
|
-
if (!resolvedBook.complete) {
|
|
643
|
-
return { kind: "ambiguous", name: text.trim().slice(0, MAX_ADDRESS_HEAD), reason: "address book unreadable" };
|
|
644
|
-
}
|
|
645
|
-
return { kind: "none" };
|
|
646
|
-
}
|
|
647
|
-
const body = text.trimStart().slice(name.length + 1).trim();
|
|
648
|
-
const foreignOwners = resolvedBook.names.get(name);
|
|
649
|
-
if (foreignOwners && foreignOwners.size > 0) {
|
|
650
|
-
if (resolvedBook.selfPublished === name || live === name) return { kind: "ambiguous", name, reason: "duplicate live session name" };
|
|
651
|
-
return { kind: "other", name };
|
|
652
|
-
}
|
|
653
|
-
if (!resolvedBook.complete) return { kind: "ambiguous", name, reason: "address book unreadable" };
|
|
654
|
-
const acceptName = resolvedBook.selfPublished ?? (resolvedBook.dirExists ? void 0 : live);
|
|
655
|
-
if (!acceptName) return { kind: "ambiguous", name, reason: "own advertisement not published" };
|
|
656
|
-
if (name === acceptName) return { kind: "self", body };
|
|
657
|
-
return { kind: "ambiguous", name, reason: "unpublished address" };
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
// builtin/extensions/im-forward/event-hub.ts
|
|
661
|
-
var DISPATCH_PREFIX = "[im-forward-dispatch]";
|
|
662
|
-
var dispatchReceiver;
|
|
663
|
-
var turnImDriverOracle;
|
|
664
|
-
function registerTurnImDriver(oracle) {
|
|
665
|
-
turnImDriverOracle = oracle;
|
|
666
|
-
}
|
|
667
|
-
function isTurnUserImDriven() {
|
|
668
|
-
try {
|
|
669
|
-
return turnImDriverOracle?.() === true;
|
|
670
|
-
} catch {
|
|
671
|
-
return false;
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
function registerDispatchReceiver(receiver) {
|
|
675
|
-
dispatchReceiver = receiver;
|
|
676
|
-
}
|
|
677
|
-
function takeDispatchFrame(text) {
|
|
678
|
-
if (!dispatchReceiver) return false;
|
|
679
|
-
try {
|
|
680
|
-
return dispatchReceiver(text);
|
|
681
|
-
} catch {
|
|
682
|
-
return true;
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
var LOCK_STALE_MS = 2e4;
|
|
686
|
-
function hubDir() {
|
|
687
|
-
return path3.join(addressBookRoot(), "im-forward");
|
|
688
|
-
}
|
|
689
|
-
function lockPath(binding) {
|
|
690
|
-
const safe = binding.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
691
|
-
return path3.join(hubDir(), `consumer-${safe}.lock`);
|
|
692
|
-
}
|
|
693
|
-
function pidAlive2(pid) {
|
|
694
|
-
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
695
|
-
try {
|
|
696
|
-
process.kill(pid, 0);
|
|
697
|
-
return true;
|
|
698
|
-
} catch (error) {
|
|
699
|
-
return error.code === "EPERM";
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
function readLock(binding) {
|
|
703
|
-
try {
|
|
704
|
-
const parsed = JSON.parse(fs3.readFileSync(lockPath(binding), "utf8"));
|
|
705
|
-
if (typeof parsed.pid === "number" && typeof parsed.holder === "string" && typeof parsed.heartbeatAt === "number" && typeof parsed.binding === "string") {
|
|
706
|
-
return {
|
|
707
|
-
pid: parsed.pid,
|
|
708
|
-
holder: parsed.holder,
|
|
709
|
-
heartbeatAt: parsed.heartbeatAt,
|
|
710
|
-
binding: parsed.binding
|
|
711
|
-
};
|
|
712
|
-
}
|
|
713
|
-
return void 0;
|
|
714
|
-
} catch {
|
|
715
|
-
return void 0;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
function lockAlive(info, now = Date.now()) {
|
|
719
|
-
if (!info) return false;
|
|
720
|
-
return pidAlive2(info.pid) && now - info.heartbeatAt < LOCK_STALE_MS;
|
|
721
|
-
}
|
|
722
|
-
function tryClaimConsumer(binding, holder, onLost, shouldYield) {
|
|
723
|
-
const target = lockPath(binding);
|
|
724
|
-
const mine = JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding });
|
|
725
|
-
const takeOver = () => {
|
|
726
|
-
const tmp = `${target}.${process.pid}.${Date.now()}.tmp`;
|
|
727
|
-
try {
|
|
728
|
-
fs3.mkdirSync(hubDir(), { recursive: true, mode: 448 });
|
|
729
|
-
fs3.writeFileSync(tmp, `${mine}
|
|
730
|
-
`);
|
|
731
|
-
try {
|
|
732
|
-
fs3.linkSync(tmp, target);
|
|
733
|
-
fs3.rmSync(tmp, { force: true });
|
|
734
|
-
return true;
|
|
735
|
-
} catch (error) {
|
|
736
|
-
fs3.rmSync(tmp, { force: true });
|
|
737
|
-
if (error.code !== "EEXIST") throw error;
|
|
738
|
-
return false;
|
|
739
|
-
}
|
|
740
|
-
} catch {
|
|
741
|
-
return false;
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
const existing = readLock(binding);
|
|
745
|
-
if (existing && lockAlive(existing)) return void 0;
|
|
746
|
-
if (existing) {
|
|
747
|
-
try {
|
|
748
|
-
fs3.rmSync(target, { force: true });
|
|
749
|
-
} catch {
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
if (!takeOver()) return void 0;
|
|
753
|
-
let released = false;
|
|
754
|
-
const heartbeatTimer = setInterval(() => {
|
|
755
|
-
if (released) return;
|
|
756
|
-
try {
|
|
757
|
-
const current = readLock(binding);
|
|
758
|
-
if (current && current.pid !== process.pid) {
|
|
759
|
-
released = true;
|
|
760
|
-
clearInterval(heartbeatTimer);
|
|
761
|
-
onLost?.();
|
|
762
|
-
return;
|
|
763
|
-
}
|
|
764
|
-
if (shouldYield?.() === true) {
|
|
765
|
-
released = true;
|
|
766
|
-
clearInterval(heartbeatTimer);
|
|
767
|
-
try {
|
|
768
|
-
if (readLock(binding)?.pid === process.pid) fs3.rmSync(target, { force: true });
|
|
769
|
-
} catch {
|
|
770
|
-
}
|
|
771
|
-
onLost?.();
|
|
772
|
-
return;
|
|
773
|
-
}
|
|
774
|
-
fs3.writeFileSync(target, `${JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding })}
|
|
775
|
-
`);
|
|
776
|
-
} catch {
|
|
777
|
-
}
|
|
778
|
-
}, 5e3);
|
|
779
|
-
heartbeatTimer.unref();
|
|
780
|
-
return {
|
|
781
|
-
heartbeat: () => {
|
|
782
|
-
try {
|
|
783
|
-
fs3.writeFileSync(target, `${JSON.stringify({ pid: process.pid, holder, heartbeatAt: Date.now(), binding })}
|
|
784
|
-
`);
|
|
785
|
-
} catch {
|
|
786
|
-
}
|
|
787
|
-
},
|
|
788
|
-
release: () => {
|
|
789
|
-
if (released) return;
|
|
790
|
-
released = true;
|
|
791
|
-
clearInterval(heartbeatTimer);
|
|
792
|
-
try {
|
|
793
|
-
const current = readLock(binding);
|
|
794
|
-
if (current && current.pid === process.pid) fs3.rmSync(target, { force: true });
|
|
795
|
-
} catch {
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
};
|
|
799
|
-
}
|
|
800
|
-
|
|
801
556
|
// builtin/extensions/pie-ipc/pure.ts
|
|
802
557
|
import { randomBytes } from "node:crypto";
|
|
803
558
|
var IPC_PROTOCOL_VERSION = 2;
|
|
@@ -1131,17 +886,513 @@ function assistantBodyText(message) {
|
|
|
1131
886
|
return text.trim();
|
|
1132
887
|
}
|
|
1133
888
|
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
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
|
+
|
|
1385
|
+
export {
|
|
1386
|
+
isRecord,
|
|
1387
|
+
assistantForwardText,
|
|
1388
|
+
sessionLabel,
|
|
1389
|
+
inputHeaderLine,
|
|
1390
|
+
composeForwardBody,
|
|
1391
|
+
estimatePostRequestBytes,
|
|
1392
|
+
truncateMarkdown,
|
|
1393
|
+
chunkKey,
|
|
1394
|
+
chunkMarkdown,
|
|
1395
|
+
collectFileReferences,
|
|
1145
1396
|
failureReason,
|
|
1146
1397
|
resolveConfigDir,
|
|
1147
1398
|
resolveConfig,
|
|
@@ -1156,12 +1407,6 @@ export {
|
|
|
1156
1407
|
resolveImForwardConfig,
|
|
1157
1408
|
addressBookRoot,
|
|
1158
1409
|
resolveAddress,
|
|
1159
|
-
DISPATCH_PREFIX,
|
|
1160
|
-
registerTurnImDriver,
|
|
1161
|
-
isTurnUserImDriven,
|
|
1162
|
-
registerDispatchReceiver,
|
|
1163
|
-
takeDispatchFrame,
|
|
1164
|
-
tryClaimConsumer,
|
|
1165
1410
|
IPC_PROTOCOL_VERSION,
|
|
1166
1411
|
MAX_STATUS_FILE_BYTES,
|
|
1167
1412
|
MAX_BODY_CHARS,
|
|
@@ -1169,19 +1414,24 @@ export {
|
|
|
1169
1414
|
HEARTBEAT_INTERVAL_MS,
|
|
1170
1415
|
DEFAULT_SEND_WAIT_MS,
|
|
1171
1416
|
MAX_INBOX_MESSAGES,
|
|
1172
|
-
MAX_INFLIGHT_SENDS,
|
|
1173
1417
|
generateInstanceId,
|
|
1174
1418
|
isValidInstanceId,
|
|
1175
1419
|
peerLabel,
|
|
1176
|
-
FrameDecoder,
|
|
1177
1420
|
wrapInboundText,
|
|
1178
1421
|
isWrappedInboundText,
|
|
1179
1422
|
wrappedMessageMarker,
|
|
1180
1423
|
clipBody,
|
|
1181
|
-
BoundedIdSet,
|
|
1182
1424
|
peerHealth,
|
|
1183
1425
|
parseStatusFile,
|
|
1184
1426
|
foldLifecycleEvent,
|
|
1185
1427
|
resolveIpcConfig,
|
|
1186
|
-
assistantBodyText
|
|
1428
|
+
assistantBodyText,
|
|
1429
|
+
signEnvelope,
|
|
1430
|
+
IpcTransport,
|
|
1431
|
+
DISPATCH_PREFIX,
|
|
1432
|
+
registerTurnImDriver,
|
|
1433
|
+
isTurnUserImDriven,
|
|
1434
|
+
registerDispatchReceiver,
|
|
1435
|
+
takeDispatchFrame,
|
|
1436
|
+
tryClaimConsumer
|
|
1187
1437
|
};
|