@decentnetwork/beagle 0.1.131 → 0.1.138
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/call-claims.d.ts +73 -0
- package/dist/call-claims.js +270 -0
- package/dist/desktop/app.js +240 -22
- package/dist/desktop/vendor/peer-webrtc.js +176 -65
- package/dist/embedded-host.js +49 -12
- package/dist/group-file.d.ts +18 -0
- package/dist/group-file.js +20 -0
- package/dist/message-store.d.ts +9 -0
- package/dist/message-store.js +17 -0
- package/dist/server.js +55 -10
- package/package.json +5 -4
- package/dist/chat-components.d.ts +0 -67
- package/dist/chat-components.js +0 -208
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export type CallState = "ringing" | "claimed" | "ended";
|
|
2
|
+
export type ClaimAction = "accepted" | "rejected";
|
|
3
|
+
export interface CallRecord {
|
|
4
|
+
callId: string;
|
|
5
|
+
peerId: string;
|
|
6
|
+
state: CallState;
|
|
7
|
+
/** When it started ringing here (ms). */
|
|
8
|
+
ringAt: number;
|
|
9
|
+
video?: boolean;
|
|
10
|
+
/** "doorbell" for a ring that came before any offer. */
|
|
11
|
+
source?: string;
|
|
12
|
+
claim?: {
|
|
13
|
+
page: string;
|
|
14
|
+
action: ClaimAction;
|
|
15
|
+
at: number;
|
|
16
|
+
};
|
|
17
|
+
ended?: {
|
|
18
|
+
reason: string;
|
|
19
|
+
at: number;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export interface ClaimResult {
|
|
23
|
+
/** true = this page owns the call (it claimed first, or already did). */
|
|
24
|
+
won: boolean;
|
|
25
|
+
record: CallRecord;
|
|
26
|
+
}
|
|
27
|
+
/** The page id a client sends; one without (an older UI, the CLI) is "anon". */
|
|
28
|
+
export declare function pageOf(v: unknown): string;
|
|
29
|
+
/** The fields of a call signal the registry cares about: a peer-webrtc
|
|
30
|
+
* RtcSignal ({type, callId, action, reason, options}) or a beagle `dkRtc`
|
|
31
|
+
* envelope ({dkRtc:1, kind, callId, video}). Undefined when it is neither. */
|
|
32
|
+
export declare function parseCallSignal(data: unknown): Record<string, unknown> | undefined;
|
|
33
|
+
export interface CallRegistryOptions {
|
|
34
|
+
/** A call nobody answers is ended after this (matches lan's 70 s). */
|
|
35
|
+
ringTimeoutMs?: number;
|
|
36
|
+
/** Claimed / ended calls are kept this long, so a duplicate offer (Android
|
|
37
|
+
* re-sends on peer join) or a late page never rings them. */
|
|
38
|
+
keepMs?: number;
|
|
39
|
+
now?: () => number;
|
|
40
|
+
}
|
|
41
|
+
export declare class CallRegistry {
|
|
42
|
+
#private;
|
|
43
|
+
constructor(opts?: CallRegistryOptions);
|
|
44
|
+
/** Bumped on every change; pages send back the one they hold. */
|
|
45
|
+
get version(): number;
|
|
46
|
+
get(callId: string): CallRecord | undefined;
|
|
47
|
+
/** Recent calls, newest first. Expires overdue rings and prunes old ones. */
|
|
48
|
+
snapshot(): CallRecord[];
|
|
49
|
+
/** A signal that came IN from a peer (or from the daemon), as a page polls it. */
|
|
50
|
+
observeInbound(userid: string, data: unknown): void;
|
|
51
|
+
/** A page answers or declines. First claim wins; the same page claiming
|
|
52
|
+
* again (accept after a retry) still wins. */
|
|
53
|
+
claim(callId: string, peerId: string, page: string, action: ClaimAction): ClaimResult;
|
|
54
|
+
/** A signal a page is about to SEND. Returns whether it may go out.
|
|
55
|
+
* - answer / action accept|reject / bye before anyone claimed: this page
|
|
56
|
+
* claims the call (an older UI that never calls /api/call-claim).
|
|
57
|
+
* - anything for a call another page claimed: refused — the caller must
|
|
58
|
+
* not get a second answer, and a losing page's decline must not hang up
|
|
59
|
+
* the call the winner is on.
|
|
60
|
+
* - a call this registry never saw ringing (our own outgoing call): free. */
|
|
61
|
+
observeOutbound(page: string, userid: string, data: unknown): {
|
|
62
|
+
allow: boolean;
|
|
63
|
+
record?: CallRecord;
|
|
64
|
+
};
|
|
65
|
+
/** Resolves true when the version moves past `since`, false after `ms` or
|
|
66
|
+
* on cancel(). */
|
|
67
|
+
wait(since: number, ms: number): {
|
|
68
|
+
promise: Promise<boolean>;
|
|
69
|
+
cancel: () => void;
|
|
70
|
+
};
|
|
71
|
+
waitForChange(since: number, ms: number): Promise<boolean>;
|
|
72
|
+
dispose(): void;
|
|
73
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// "Answered elsewhere": the beagle server is the authority on who has an
|
|
2
|
+
// incoming call.
|
|
3
|
+
//
|
|
4
|
+
// Every page connected to this server polls the same broadcast call log, so
|
|
5
|
+
// every page rings on an incoming call. Until now only a BroadcastChannel in
|
|
6
|
+
// beagle-ui told the others when one answered — and that reaches tabs of ONE
|
|
7
|
+
// browser. mac-dev's page open in Chrome and in the Claude app's browser both
|
|
8
|
+
// rang; one answered, the other rang on for minutes (2026-11-13).
|
|
9
|
+
//
|
|
10
|
+
// The server sees every signal in both directions (pages poll /api/call-poll
|
|
11
|
+
// and send through /api/call-signal), so it keeps the state of recent incoming
|
|
12
|
+
// calls here:
|
|
13
|
+
// ringing — an inbound offer or a doorbell `call-ring` with a new callId;
|
|
14
|
+
// claimed — the first page (or client) that answered or declined it;
|
|
15
|
+
// ended — the caller gave up (bye / action:reject), or nobody answered
|
|
16
|
+
// within the ring timeout.
|
|
17
|
+
// The whole table rides on every /api/call-poll answer and a change wakes
|
|
18
|
+
// waiting polls at once, so every page learns of a claim within one poll.
|
|
19
|
+
// First claim wins: an answer / accept / decline from any other page for a
|
|
20
|
+
// claimed call is refused at /api/call-signal, so the caller never gets two.
|
|
21
|
+
//
|
|
22
|
+
// decent-hq docs/tasks/TASK-2026-11-13-answered-elsewhere.md
|
|
23
|
+
/** The page id a client sends; one without (an older UI, the CLI) is "anon". */
|
|
24
|
+
export function pageOf(v) {
|
|
25
|
+
return typeof v === "string" && v.trim() ? v.trim().slice(0, 64) : "anon";
|
|
26
|
+
}
|
|
27
|
+
const norm = (id) => (typeof id === "string" ? id.trim().toLowerCase() : "");
|
|
28
|
+
/** The fields of a call signal the registry cares about: a peer-webrtc
|
|
29
|
+
* RtcSignal ({type, callId, action, reason, options}) or a beagle `dkRtc`
|
|
30
|
+
* envelope ({dkRtc:1, kind, callId, video}). Undefined when it is neither. */
|
|
31
|
+
export function parseCallSignal(data) {
|
|
32
|
+
if (typeof data !== "string")
|
|
33
|
+
return undefined;
|
|
34
|
+
const text = data.replace(/\0+$/u, "").trim();
|
|
35
|
+
if (!text.startsWith("{"))
|
|
36
|
+
return undefined;
|
|
37
|
+
try {
|
|
38
|
+
const o = JSON.parse(text);
|
|
39
|
+
return o && typeof o === "object" && norm(o.callId) ? o : undefined;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export class CallRegistry {
|
|
46
|
+
#calls = new Map();
|
|
47
|
+
#version = 0;
|
|
48
|
+
#waiters = new Set();
|
|
49
|
+
#timers = new Map();
|
|
50
|
+
#ringTimeoutMs;
|
|
51
|
+
#keepMs;
|
|
52
|
+
#now;
|
|
53
|
+
constructor(opts = {}) {
|
|
54
|
+
this.#ringTimeoutMs = opts.ringTimeoutMs ?? 70_000;
|
|
55
|
+
this.#keepMs = opts.keepMs ?? 5 * 60_000;
|
|
56
|
+
this.#now = opts.now ?? Date.now;
|
|
57
|
+
}
|
|
58
|
+
/** Bumped on every change; pages send back the one they hold. */
|
|
59
|
+
get version() {
|
|
60
|
+
return this.#version;
|
|
61
|
+
}
|
|
62
|
+
get(callId) {
|
|
63
|
+
return this.#calls.get(norm(callId));
|
|
64
|
+
}
|
|
65
|
+
/** Recent calls, newest first. Expires overdue rings and prunes old ones. */
|
|
66
|
+
snapshot() {
|
|
67
|
+
this.#sweep();
|
|
68
|
+
return [...this.#calls.values()].sort((a, b) => b.ringAt - a.ringAt).map((r) => ({ ...r }));
|
|
69
|
+
}
|
|
70
|
+
/** A signal that came IN from a peer (or from the daemon), as a page polls it. */
|
|
71
|
+
observeInbound(userid, data) {
|
|
72
|
+
const s = parseCallSignal(data);
|
|
73
|
+
if (!s)
|
|
74
|
+
return;
|
|
75
|
+
const id = norm(s.callId);
|
|
76
|
+
const rec = this.#calls.get(id);
|
|
77
|
+
if (s.dkRtc === 1) {
|
|
78
|
+
if (s.kind === "call-ring") {
|
|
79
|
+
// lan 0.1.320 says where it came from in `via`; `source` is the older
|
|
80
|
+
// spelling of the same field.
|
|
81
|
+
const source = typeof s.via === "string" ? s.via : typeof s.source === "string" ? s.source : "doorbell";
|
|
82
|
+
this.#ring(id, userid, { video: s.video === true, source });
|
|
83
|
+
}
|
|
84
|
+
else if (s.kind === "call-ring-stop") {
|
|
85
|
+
// lan 0.1.320: {kind:"call-ring-stop", callId, reason}. "answered" =
|
|
86
|
+
// something answered it — a page here (already the claim: no-op) or a
|
|
87
|
+
// client of the daemon we never saw (the agent): it claims the call.
|
|
88
|
+
// Any other reason (declined / caller-left / timeout / hangup /
|
|
89
|
+
// failed) ends it for every page.
|
|
90
|
+
const reason = typeof s.reason === "string" && s.reason ? s.reason : "stopped";
|
|
91
|
+
if (reason === "answered") {
|
|
92
|
+
if (!rec)
|
|
93
|
+
this.#ring(id, userid, {});
|
|
94
|
+
if (!this.#calls.get(id)?.claim)
|
|
95
|
+
this.claim(id, userid, "daemon", "accepted");
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.#end(id, reason);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (s.kind === "call-claimed") {
|
|
102
|
+
// The daemon's own agent answered (never passes through this server).
|
|
103
|
+
if (!rec)
|
|
104
|
+
this.#ring(id, userid, {});
|
|
105
|
+
this.claim(id, userid, typeof s.by === "string" ? `daemon:${s.by}` : "daemon", s.action === "rejected" ? "rejected" : "accepted");
|
|
106
|
+
}
|
|
107
|
+
else if (s.kind === "call-ended") {
|
|
108
|
+
this.#end(id, typeof s.reason === "string" ? s.reason : "ended");
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
switch (s.type) {
|
|
113
|
+
case "offer": {
|
|
114
|
+
// A re-offer for a known call (screen share, Android's replay) is not
|
|
115
|
+
// a new ring, and an ended call never rings again.
|
|
116
|
+
if (rec)
|
|
117
|
+
return;
|
|
118
|
+
const options = Array.isArray(s.options) ? s.options : [];
|
|
119
|
+
this.#ring(id, userid, { video: options.includes("video") });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
case "bye":
|
|
123
|
+
if (rec)
|
|
124
|
+
this.#end(id, typeof s.reason === "string" && s.reason ? s.reason : "bye");
|
|
125
|
+
return;
|
|
126
|
+
case "action":
|
|
127
|
+
// The caller cancelling a ring-first call.
|
|
128
|
+
if (rec && s.action === "reject")
|
|
129
|
+
this.#end(id, "declined");
|
|
130
|
+
return;
|
|
131
|
+
default:
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** A page answers or declines. First claim wins; the same page claiming
|
|
136
|
+
* again (accept after a retry) still wins. */
|
|
137
|
+
claim(callId, peerId, page, action) {
|
|
138
|
+
const id = norm(callId);
|
|
139
|
+
let rec = this.#calls.get(id);
|
|
140
|
+
if (!rec)
|
|
141
|
+
rec = this.#ring(id, peerId, {});
|
|
142
|
+
if (rec.claim)
|
|
143
|
+
return { won: rec.claim.page === page, record: { ...rec } };
|
|
144
|
+
if (rec.state === "ended")
|
|
145
|
+
return { won: false, record: { ...rec } };
|
|
146
|
+
rec.claim = { page, action, at: this.#now() };
|
|
147
|
+
rec.state = "claimed";
|
|
148
|
+
this.#clearTimer(id);
|
|
149
|
+
this.#changed();
|
|
150
|
+
return { won: true, record: { ...rec } };
|
|
151
|
+
}
|
|
152
|
+
/** A signal a page is about to SEND. Returns whether it may go out.
|
|
153
|
+
* - answer / action accept|reject / bye before anyone claimed: this page
|
|
154
|
+
* claims the call (an older UI that never calls /api/call-claim).
|
|
155
|
+
* - anything for a call another page claimed: refused — the caller must
|
|
156
|
+
* not get a second answer, and a losing page's decline must not hang up
|
|
157
|
+
* the call the winner is on.
|
|
158
|
+
* - a call this registry never saw ringing (our own outgoing call): free. */
|
|
159
|
+
observeOutbound(page, userid, data) {
|
|
160
|
+
const s = parseCallSignal(data);
|
|
161
|
+
if (!s || s.dkRtc === 1)
|
|
162
|
+
return { allow: true };
|
|
163
|
+
const id = norm(s.callId);
|
|
164
|
+
const rec = this.#calls.get(id);
|
|
165
|
+
if (!rec)
|
|
166
|
+
return { allow: true };
|
|
167
|
+
if (rec.claim) {
|
|
168
|
+
if (rec.claim.page !== page)
|
|
169
|
+
return { allow: false, record: { ...rec } };
|
|
170
|
+
// The owner hanging up: the call is over for every page.
|
|
171
|
+
if (s.type === "bye")
|
|
172
|
+
this.#end(id, typeof s.reason === "string" && s.reason ? s.reason : "bye");
|
|
173
|
+
return { allow: true };
|
|
174
|
+
}
|
|
175
|
+
if (rec.state === "ended") {
|
|
176
|
+
// Nothing to answer any more; a bye to a gone call is harmless.
|
|
177
|
+
return { allow: s.type === "bye" || s.type === "candidate", record: { ...rec } };
|
|
178
|
+
}
|
|
179
|
+
const accepting = s.type === "answer" || (s.type === "action" && s.action === "accept");
|
|
180
|
+
const declining = (s.type === "action" && s.action === "reject") || s.type === "bye";
|
|
181
|
+
if (accepting || declining) {
|
|
182
|
+
this.claim(id, userid, page, accepting ? "accepted" : "rejected");
|
|
183
|
+
if (declining)
|
|
184
|
+
this.#end(id, "declined");
|
|
185
|
+
}
|
|
186
|
+
return { allow: true };
|
|
187
|
+
}
|
|
188
|
+
/** Resolves true when the version moves past `since`, false after `ms` or
|
|
189
|
+
* on cancel(). */
|
|
190
|
+
wait(since, ms) {
|
|
191
|
+
if (this.#version !== since)
|
|
192
|
+
return { promise: Promise.resolve(true), cancel: () => { } };
|
|
193
|
+
let done = () => { };
|
|
194
|
+
const promise = new Promise((resolve) => {
|
|
195
|
+
let timer;
|
|
196
|
+
const wake = () => done(true);
|
|
197
|
+
done = (changed) => {
|
|
198
|
+
if (timer)
|
|
199
|
+
clearTimeout(timer);
|
|
200
|
+
this.#waiters.delete(wake);
|
|
201
|
+
resolve(changed);
|
|
202
|
+
};
|
|
203
|
+
timer = setTimeout(() => done(false), ms);
|
|
204
|
+
timer.unref?.();
|
|
205
|
+
this.#waiters.add(wake);
|
|
206
|
+
});
|
|
207
|
+
return { promise, cancel: () => done(false) };
|
|
208
|
+
}
|
|
209
|
+
waitForChange(since, ms) {
|
|
210
|
+
return this.wait(since, ms).promise;
|
|
211
|
+
}
|
|
212
|
+
dispose() {
|
|
213
|
+
for (const t of this.#timers.values())
|
|
214
|
+
clearTimeout(t);
|
|
215
|
+
this.#timers.clear();
|
|
216
|
+
for (const w of [...this.#waiters])
|
|
217
|
+
w();
|
|
218
|
+
}
|
|
219
|
+
#ring(id, peerId, extra) {
|
|
220
|
+
const had = this.#calls.get(id);
|
|
221
|
+
if (had)
|
|
222
|
+
return had;
|
|
223
|
+
const rec = { callId: id, peerId, state: "ringing", ringAt: this.#now(), ...extra };
|
|
224
|
+
this.#calls.set(id, rec);
|
|
225
|
+
const t = setTimeout(() => this.#end(id, "timeout"), this.#ringTimeoutMs);
|
|
226
|
+
t.unref?.();
|
|
227
|
+
this.#timers.set(id, t);
|
|
228
|
+
this.#changed();
|
|
229
|
+
return rec;
|
|
230
|
+
}
|
|
231
|
+
#end(id, reason) {
|
|
232
|
+
const rec = this.#calls.get(id);
|
|
233
|
+
if (!rec || rec.state === "ended")
|
|
234
|
+
return;
|
|
235
|
+
// A ring timeout only ends a call nobody picked up.
|
|
236
|
+
if (reason === "timeout" && rec.claim)
|
|
237
|
+
return;
|
|
238
|
+
rec.state = "ended";
|
|
239
|
+
rec.ended = { reason, at: this.#now() };
|
|
240
|
+
this.#clearTimer(id);
|
|
241
|
+
this.#changed();
|
|
242
|
+
}
|
|
243
|
+
#sweep() {
|
|
244
|
+
const now = this.#now();
|
|
245
|
+
for (const [id, r] of this.#calls) {
|
|
246
|
+
if (r.state === "ringing" && now - r.ringAt >= this.#ringTimeoutMs)
|
|
247
|
+
this.#end(id, "timeout");
|
|
248
|
+
// An ended call is kept keepMs. A claimed call is a call in progress:
|
|
249
|
+
// dropping it would let its next re-offer (screen share) look like a
|
|
250
|
+
// new ring, so it stays until it ends (12 h at most).
|
|
251
|
+
const stale = (r.state === "ended" && now - (r.ended?.at ?? r.ringAt) > this.#keepMs) ||
|
|
252
|
+
(r.state === "claimed" && now - (r.claim?.at ?? r.ringAt) > 12 * 3600_000);
|
|
253
|
+
if (stale) {
|
|
254
|
+
this.#calls.delete(id);
|
|
255
|
+
this.#clearTimer(id);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
#clearTimer(id) {
|
|
260
|
+
const t = this.#timers.get(id);
|
|
261
|
+
if (t)
|
|
262
|
+
clearTimeout(t);
|
|
263
|
+
this.#timers.delete(id);
|
|
264
|
+
}
|
|
265
|
+
#changed() {
|
|
266
|
+
this.#version++;
|
|
267
|
+
for (const w of [...this.#waiters])
|
|
268
|
+
w();
|
|
269
|
+
}
|
|
270
|
+
}
|