@arhen/pi-core-subagent 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mailbox.ts ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Agent↔agent mailbox. Pure logic, no pi imports — easily unit-tested.
3
+ * Agents talk by polling, not push: send() enqueues, poll() drains.
4
+ */
5
+
6
+ export interface MailboxMessage {
7
+ from: string;
8
+ text: string;
9
+ at: number;
10
+ }
11
+
12
+ export interface Mailbox {
13
+ open(taskId: string): void;
14
+ /** Returns false when sender or target is unknown (no silent drops). */
15
+ send(from: string, to: string, text: string): boolean;
16
+ /** Return and clear all pending messages for taskId. */
17
+ poll(taskId: string): MailboxMessage[];
18
+ close(taskId: string): void;
19
+ }
20
+
21
+ export function createMailbox(): Mailbox {
22
+ const boxes = new Map<string, MailboxMessage[]>();
23
+ return {
24
+ open(taskId: string): void {
25
+ if (!boxes.has(taskId)) boxes.set(taskId, []);
26
+ },
27
+ send(from: string, to: string, text: string): boolean {
28
+ const box = boxes.get(to);
29
+ if (!box || !boxes.has(from)) return false;
30
+ box.push({ from, text, at: Date.now() });
31
+ return true;
32
+ },
33
+ poll(taskId: string): MailboxMessage[] {
34
+ const box = boxes.get(taskId);
35
+ if (!box) return [];
36
+ return box.splice(0);
37
+ },
38
+ close(taskId: string): void {
39
+ boxes.delete(taskId);
40
+ },
41
+ };
42
+ }