@alexsun-top/opencode-chat-room 0.3.0 → 0.3.2

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/watch.ts CHANGED
@@ -14,6 +14,8 @@ import { assertDeliveryLease } from "./session-delivery";
14
14
  import type { QueueTransport } from "./queue-transport";
15
15
  import { pollOwnedSessions } from "./v2-receiver";
16
16
  import { pollKnownSessions } from "../plugin";
17
+ import { locallyOwnedSessions, reportAgentPresence } from "./agent-presence";
18
+ import { flushReceiptCandidates, receiptDestination } from "./agent-receipts";
17
19
 
18
20
  type WatchRecord = { phase: "starting" | "running"; token: string; nonce: string; instanceID: string; pid: number; port?: number; createdAt?: number };
19
21
  function canonicalPath(value: string): string {
@@ -115,7 +117,9 @@ type Host = NonNullable<Awaited<ReturnType<typeof discoverCompatibleRegisteredHo
115
117
  export type WatchSweepDeps = {
116
118
  discover(): Promise<Host | undefined>;
117
119
  owner(host: Host): Promise<string | undefined>;
120
+ flush?(ownerID: string): Promise<void>;
118
121
  memberships(ownerID: string): Promise<Set<string>>;
122
+ presence?(ids: ReadonlySet<string>, host: Host, ownerID: string): Promise<void>;
119
123
  poll(ids: ReadonlySet<string>, host: Host, ownerID: string): Promise<void>;
120
124
  };
121
125
  export async function watcherSweep(deps: WatchSweepDeps = defaultSweepDeps()): Promise<boolean> {
@@ -124,7 +128,9 @@ export async function watcherSweep(deps: WatchSweepDeps = defaultSweepDeps()): P
124
128
  const ownerID = await deps.owner(host).catch(() => undefined);
125
129
  if (!ownerID) return false;
126
130
  const ids = await deps.memberships(ownerID).catch(() => undefined);
127
- if (!ids?.size) return true;
131
+ if (!ids) return true;
132
+ await deps.presence?.(ids, host, ownerID).catch(() => {});
133
+ if (!ids.size) { await deps.flush?.(ownerID).catch(() => {}); return true; }
128
134
  await deps.poll(ids, host, ownerID);
129
135
  return true;
130
136
  }
@@ -137,15 +143,23 @@ function defaultSweepDeps(): WatchSweepDeps {
137
143
  return {
138
144
  discover: discoverCompatibleRegisteredHost,
139
145
  owner: (host) => receiverOwnerID(host.origin),
146
+ flush: (ownerID) => flushReceiptCandidates({ destination: receiptDestination(center.center), receiverOwnerID: ownerID,
147
+ ...(center.center ? { center: center.center, token: center.token } : {}) }),
140
148
  memberships: async (ownerID) => {
141
149
  if (center.center) {
142
150
  const result = await request(center, `/receiver/memberships?ownerID=${encodeURIComponent(ownerID)}`, "GET") as { sessionIDs?: string[] };
143
151
  return new Set(result.sessionIDs ?? []);
144
152
  }
145
153
  const registry = await loadRegistry();
146
- return new Set(Object.values(registry).flatMap((entries) => Object.entries(entries)
147
- .filter(([, entry]) => entry.protocol === "v2" && entry.receiverOwnerID === ownerID).map(([id]) => id)));
154
+ return locallyOwnedSessions(registry, ownerID);
148
155
  },
156
+ presence: (ids, host, ownerID) => reportAgentPresence({
157
+ host, ownerID, center, sessionIDs: ids,
158
+ current: async () => {
159
+ const current = await discoverCompatibleRegisteredHost();
160
+ return Boolean(current && current.origin === host.origin && current.pid === host.pid && current.version === host.version);
161
+ },
162
+ }),
149
163
  poll: (ids, host, ownerID) => {
150
164
  const transport: QueueTransport = {
151
165
  protocol: "v2", receiverOwnerID: ownerID, mentionOnlySessions: ids,