Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

607 B
Raw Blame
import type { Port } from "@pibot/protocol";

export function memoryPorts(): [Port, Port] {
  const listeners = [new Set<(message: unknown) => void>(), new Set<(message: unknown) => void>()];
  const port = (index: number): Port => ({
    post(message) {
      const snapshot = structuredClone(message);
      queueMicrotask(() => {
        for (const receive of listeners[1 - index] ?? []) receive(snapshot);
      });
    },
    onMessage(receive) {
      listeners[index]?.add(receive);
      return () => {
        listeners[index]?.delete(receive);
      };
    },
  });
  return [port(0), port(1)];
}