@poolse/react 3.8.0 → 3.9.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/CHANGELOG.md +18 -0
- package/README.md +5 -1
- package/dist/index.cjs +573 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +187 -3
- package/dist/index.d.ts +187 -3
- package/dist/index.js +518 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { Poolse, MessageQueuedError, toTombstone, markQuoteDeleted, safeUuid, mergeMessage, isCallLogMessage, applyThreadUpdate } from '@poolse/sdk';
|
|
3
|
-
export { ApiError, AuthError, MessageDiscardedError, MessageQueuedError, NetworkError, Poolse, PoolseError, RateLimitedError, callLogDisplayFor, createIndexedDbStorage, describeCallLog, formatCallDuration, formatCallLogPreview, isCallLogMessage, isMessageVisibleTo, isTombstone, memoryStorage, mergeMessage, parseCallLog, parseCallLogBody, toTombstone } from '@poolse/sdk';
|
|
2
|
+
import { Poolse, MessageQueuedError, toTombstone, markQuoteDeleted, safeUuid, mergeMessage, isCallLogMessage, applyLiveSegment, ApiError, applyThreadUpdate } from '@poolse/sdk';
|
|
3
|
+
export { ApiError, AuthError, MessageDiscardedError, MessageQueuedError, NetworkError, POOLSE_SERVICE_IDENTITY_PREFIX, Poolse, PoolseError, RateLimitedError, applyLiveSegment, callLogDisplayFor, captureErrorMessage, captureLabel, createIndexedDbStorage, describeCallLog, describeRecordingNotice, findInText, formatCallDuration, formatCallLogPreview, formatRecordingNoticePreview, groupTranscript, isCallLogMessage, isMessageVisibleTo, isServiceParticipant, isTombstone, memoryStorage, mergeMessage, parseCallLog, parseCallLogBody, parseRecordingNotice, parseRecordingNoticeBody, recordingNoticeDisplayFor, speakerName, toTombstone } from '@poolse/sdk';
|
|
4
4
|
import { createContext, useRef, useMemo, useEffect, useContext, useState, useCallback, useSyncExternalStore } from 'react';
|
|
5
5
|
import { jsx } from 'react/jsx-runtime';
|
|
6
6
|
|
|
@@ -1767,6 +1767,7 @@ function useCallRoom(conversationId, opts) {
|
|
|
1767
1767
|
const [activeDevices, setActiveDevices] = useState({});
|
|
1768
1768
|
const [error, setError] = useState(null);
|
|
1769
1769
|
const [sfuAvailable, setSfuAvailable] = useState(null);
|
|
1770
|
+
const [canPublish, setCanPublish] = useState(true);
|
|
1770
1771
|
const optsRef = useRef(opts);
|
|
1771
1772
|
optsRef.current = opts;
|
|
1772
1773
|
const joined = useRef(false);
|
|
@@ -1778,7 +1779,8 @@ function useCallRoom(conversationId, opts) {
|
|
|
1778
1779
|
...optsRef.current.video !== void 0 ? { video: optsRef.current.video } : {},
|
|
1779
1780
|
...optsRef.current.muted !== void 0 ? { muted: optsRef.current.muted } : {},
|
|
1780
1781
|
...optsRef.current.facing !== void 0 ? { facing: optsRef.current.facing } : {},
|
|
1781
|
-
...optsRef.current.aloneGraceMs !== void 0 ? { aloneGraceMs: optsRef.current.aloneGraceMs } : {}
|
|
1782
|
+
...optsRef.current.aloneGraceMs !== void 0 ? { aloneGraceMs: optsRef.current.aloneGraceMs } : {},
|
|
1783
|
+
...optsRef.current.republishOnPermission !== void 0 ? { republishOnPermission: optsRef.current.republishOnPermission } : {}
|
|
1782
1784
|
});
|
|
1783
1785
|
}, [poolse, conversationId]);
|
|
1784
1786
|
const refreshDevices = useCallback((target) => {
|
|
@@ -1803,6 +1805,7 @@ function useCallRoom(conversationId, opts) {
|
|
|
1803
1805
|
setScreen(target.isScreenShareEnabled());
|
|
1804
1806
|
setCanShare(target.canShareScreen());
|
|
1805
1807
|
setFacing(target.getFacingMode());
|
|
1808
|
+
setCanPublish(target.canPublish());
|
|
1806
1809
|
}, []);
|
|
1807
1810
|
useEffect(() => {
|
|
1808
1811
|
if (!room) {
|
|
@@ -1822,12 +1825,14 @@ function useCallRoom(conversationId, opts) {
|
|
|
1822
1825
|
const offAlone = room.onAlone(() => setAlone(true));
|
|
1823
1826
|
const offDevices = room.onDevices(() => refreshDevices(room));
|
|
1824
1827
|
const offError = room.onError(setError);
|
|
1828
|
+
const offPublish = room.onPublishPermission(() => sync(room));
|
|
1825
1829
|
return () => {
|
|
1826
1830
|
offStatus();
|
|
1827
1831
|
offRoster();
|
|
1828
1832
|
offAlone();
|
|
1829
1833
|
offDevices();
|
|
1830
1834
|
offError();
|
|
1835
|
+
offPublish();
|
|
1831
1836
|
held.current = null;
|
|
1832
1837
|
if (joined.current) {
|
|
1833
1838
|
joined.current = false;
|
|
@@ -1920,6 +1925,11 @@ function useCallRoom(conversationId, opts) {
|
|
|
1920
1925
|
},
|
|
1921
1926
|
[room]
|
|
1922
1927
|
);
|
|
1928
|
+
const republish = useCallback(async () => {
|
|
1929
|
+
if (!room) return;
|
|
1930
|
+
await room.republish();
|
|
1931
|
+
sync(room);
|
|
1932
|
+
}, [room, sync]);
|
|
1923
1933
|
return {
|
|
1924
1934
|
status,
|
|
1925
1935
|
participants,
|
|
@@ -1944,9 +1954,110 @@ function useCallRoom(conversationId, opts) {
|
|
|
1944
1954
|
setMicrophoneEnabled,
|
|
1945
1955
|
toggleMic,
|
|
1946
1956
|
setScreenShareEnabled,
|
|
1947
|
-
toggleScreenShare
|
|
1957
|
+
toggleScreenShare,
|
|
1958
|
+
canPublish,
|
|
1959
|
+
republish
|
|
1948
1960
|
};
|
|
1949
1961
|
}
|
|
1962
|
+
var IDLE_RECORDING = {
|
|
1963
|
+
available: true,
|
|
1964
|
+
active: false,
|
|
1965
|
+
id: null,
|
|
1966
|
+
mode: null,
|
|
1967
|
+
started_at: null,
|
|
1968
|
+
started_by_user_id: null,
|
|
1969
|
+
auto: false,
|
|
1970
|
+
consent_required: false,
|
|
1971
|
+
consent: { accepted: [], declined: [] }
|
|
1972
|
+
};
|
|
1973
|
+
var without = (list, id) => list.filter((x) => x !== id);
|
|
1974
|
+
function applyCaptureEvent(state, e) {
|
|
1975
|
+
const rec = state.recording ?? IDLE_RECORDING;
|
|
1976
|
+
switch (e.type) {
|
|
1977
|
+
case "recording:started": {
|
|
1978
|
+
const r = e.evt.recording;
|
|
1979
|
+
const starter = r.started_by_user_id;
|
|
1980
|
+
return {
|
|
1981
|
+
...state,
|
|
1982
|
+
recording: {
|
|
1983
|
+
...rec,
|
|
1984
|
+
active: true,
|
|
1985
|
+
id: r.id,
|
|
1986
|
+
mode: r.mode,
|
|
1987
|
+
started_at: r.started_at,
|
|
1988
|
+
started_by_user_id: starter,
|
|
1989
|
+
auto: r.auto,
|
|
1990
|
+
consent_required: e.evt.consent_required,
|
|
1991
|
+
// Starting is accepting.
|
|
1992
|
+
consent: starter ? {
|
|
1993
|
+
accepted: [...without(rec.consent.accepted, starter), starter],
|
|
1994
|
+
declined: without(rec.consent.declined, starter)
|
|
1995
|
+
} : rec.consent
|
|
1996
|
+
}
|
|
1997
|
+
};
|
|
1998
|
+
}
|
|
1999
|
+
case "recording:stopped":
|
|
2000
|
+
case "recording:ready":
|
|
2001
|
+
if (rec.id !== null && rec.id !== e.evt.recording.id) return state;
|
|
2002
|
+
return {
|
|
2003
|
+
...state,
|
|
2004
|
+
recording: { ...rec, active: false, id: null, mode: null, consent_required: false }
|
|
2005
|
+
};
|
|
2006
|
+
case "recording:consent": {
|
|
2007
|
+
const { user_id: who, decision } = e.evt;
|
|
2008
|
+
const accepted = without(rec.consent.accepted, who);
|
|
2009
|
+
const declined = without(rec.consent.declined, who);
|
|
2010
|
+
(decision === "accepted" ? accepted : declined).push(who);
|
|
2011
|
+
return { ...state, recording: { ...rec, consent: { accepted, declined } } };
|
|
2012
|
+
}
|
|
2013
|
+
case "transcript:changed": {
|
|
2014
|
+
const t = e.evt.transcript;
|
|
2015
|
+
return {
|
|
2016
|
+
...state,
|
|
2017
|
+
transcription: {
|
|
2018
|
+
available: state.transcription?.available ?? true,
|
|
2019
|
+
active: t.status === "live",
|
|
2020
|
+
id: t.id,
|
|
2021
|
+
status: t.status,
|
|
2022
|
+
language: t.language
|
|
2023
|
+
}
|
|
2024
|
+
};
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
function subscribeCapture(chat, conversationId, callId, fn) {
|
|
2029
|
+
const conv = chat.realtime.conversation(conversationId);
|
|
2030
|
+
const mine = (id) => callId === null || id === callId;
|
|
2031
|
+
const offs = [
|
|
2032
|
+
conv.onRecordingStarted((evt) => mine(evt.call_id) && fn({ type: "recording:started", evt })),
|
|
2033
|
+
conv.onRecordingStopped((evt) => mine(evt.call_id) && fn({ type: "recording:stopped", evt })),
|
|
2034
|
+
conv.onRecordingReady((evt) => mine(evt.call_id) && fn({ type: "recording:ready", evt })),
|
|
2035
|
+
conv.onRecordingConsent((evt) => mine(evt.call_id) && fn({ type: "recording:consent", evt })),
|
|
2036
|
+
conv.onTranscriptStarted((evt) => mine(evt.call_id) && fn({ type: "transcript:changed", evt })),
|
|
2037
|
+
conv.onTranscriptStopped((evt) => mine(evt.call_id) && fn({ type: "transcript:changed", evt })),
|
|
2038
|
+
conv.onTranscriptReady((evt) => mine(evt.call_id) && fn({ type: "transcript:changed", evt }))
|
|
2039
|
+
];
|
|
2040
|
+
return () => offs.forEach((off) => off());
|
|
2041
|
+
}
|
|
2042
|
+
function useMeId(chat) {
|
|
2043
|
+
const [id, setId] = useState(() => chat.me.peek()?.id ?? null);
|
|
2044
|
+
useEffect(() => {
|
|
2045
|
+
setId(chat.me.peek()?.id ?? null);
|
|
2046
|
+
return chat.me.onChange((me) => setId(me.id));
|
|
2047
|
+
}, [chat]);
|
|
2048
|
+
return id;
|
|
2049
|
+
}
|
|
2050
|
+
function onReconnect(chat, fn) {
|
|
2051
|
+
let wasConnected = chat.realtime.getStatus() === "connected";
|
|
2052
|
+
let connectedBefore = wasConnected;
|
|
2053
|
+
return chat.realtime.onStatus((status) => {
|
|
2054
|
+
if (status === "connected" && !wasConnected && connectedBefore) fn();
|
|
2055
|
+
wasConnected = status === "connected";
|
|
2056
|
+
connectedBefore ||= wasConnected;
|
|
2057
|
+
});
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
// src/use-live-call.ts
|
|
1950
2061
|
function useLiveCall(conversationId) {
|
|
1951
2062
|
const chat = usePoolse();
|
|
1952
2063
|
const [call, setCall] = useState(null);
|
|
@@ -1979,6 +2090,21 @@ function useLiveCall(conversationId) {
|
|
|
1979
2090
|
if (evt.call_id === callIdRef.current) setParticipantIds(evt.participants);
|
|
1980
2091
|
else refresh();
|
|
1981
2092
|
});
|
|
2093
|
+
const offCapture = subscribeCapture(chat, conversationId, null, (e) => {
|
|
2094
|
+
if (e.evt.call_id !== callIdRef.current) return;
|
|
2095
|
+
setCall((c) => {
|
|
2096
|
+
if (!c) return c;
|
|
2097
|
+
const next = applyCaptureEvent(
|
|
2098
|
+
{ recording: c.recording ?? null, transcription: c.transcription ?? null },
|
|
2099
|
+
e
|
|
2100
|
+
);
|
|
2101
|
+
return {
|
|
2102
|
+
...c,
|
|
2103
|
+
...next.recording ? { recording: next.recording } : {},
|
|
2104
|
+
...next.transcription ? { transcription: next.transcription } : {}
|
|
2105
|
+
};
|
|
2106
|
+
});
|
|
2107
|
+
});
|
|
1982
2108
|
const offFinished = conv.onCallFinished((evt) => {
|
|
1983
2109
|
if (evt.call_id !== callIdRef.current) return;
|
|
1984
2110
|
seq.current++;
|
|
@@ -1995,6 +2121,7 @@ function useLiveCall(conversationId) {
|
|
|
1995
2121
|
offStarted();
|
|
1996
2122
|
offParticipants();
|
|
1997
2123
|
offFinished();
|
|
2124
|
+
offCapture();
|
|
1998
2125
|
offStatus();
|
|
1999
2126
|
};
|
|
2000
2127
|
}, [chat, conversationId, apply, refresh]);
|
|
@@ -2049,6 +2176,392 @@ function useRoom(conversationId, opts) {
|
|
|
2049
2176
|
error: error ?? media.error
|
|
2050
2177
|
};
|
|
2051
2178
|
}
|
|
2179
|
+
function useRecording(callId, conversationId, initial) {
|
|
2180
|
+
const chat = usePoolse();
|
|
2181
|
+
const meId = useMeId(chat);
|
|
2182
|
+
const [state, setState] = useState(
|
|
2183
|
+
() => initial && initial.id === callId ? { recording: initial.recording ?? null, transcription: initial.transcription ?? null } : { recording: null, transcription: null }
|
|
2184
|
+
);
|
|
2185
|
+
const [notice, setNotice] = useState(null);
|
|
2186
|
+
const [lastRecording, setLastRecording] = useState(null);
|
|
2187
|
+
const [pending, setPending] = useState(false);
|
|
2188
|
+
const [error, setError] = useState(null);
|
|
2189
|
+
const [transcriptConsent, setTranscriptConsent] = useState(false);
|
|
2190
|
+
const seq = useRef(0);
|
|
2191
|
+
const hasInitial = useRef(!!initial && initial.id === callId);
|
|
2192
|
+
const refresh = useCallback(() => {
|
|
2193
|
+
if (!callId || !conversationId) return;
|
|
2194
|
+
const mine = ++seq.current;
|
|
2195
|
+
chat.liveCalls.get(conversationId).then((c) => {
|
|
2196
|
+
if (mine !== seq.current) return;
|
|
2197
|
+
if (c && c.id === callId)
|
|
2198
|
+
setState({ recording: c.recording ?? null, transcription: c.transcription ?? null });
|
|
2199
|
+
else
|
|
2200
|
+
setState((s) => ({
|
|
2201
|
+
recording: s.recording ? { ...s.recording, active: false } : null,
|
|
2202
|
+
transcription: s.transcription ? { ...s.transcription, active: false } : null
|
|
2203
|
+
}));
|
|
2204
|
+
}).catch(() => {
|
|
2205
|
+
});
|
|
2206
|
+
}, [chat, callId, conversationId]);
|
|
2207
|
+
useEffect(() => {
|
|
2208
|
+
if (!callId || !conversationId) {
|
|
2209
|
+
setState({ recording: null, transcription: null });
|
|
2210
|
+
return;
|
|
2211
|
+
}
|
|
2212
|
+
if (hasInitial.current) hasInitial.current = false;
|
|
2213
|
+
else refresh();
|
|
2214
|
+
const offCapture = subscribeCapture(chat, conversationId, callId, (e) => {
|
|
2215
|
+
seq.current++;
|
|
2216
|
+
setState((s) => applyCaptureEvent(s, e));
|
|
2217
|
+
if (e.type === "recording:started") {
|
|
2218
|
+
setNotice(e.evt.notice);
|
|
2219
|
+
setLastRecording(e.evt.recording);
|
|
2220
|
+
} else if (e.type === "recording:stopped" || e.type === "recording:ready") {
|
|
2221
|
+
setLastRecording(e.evt.recording);
|
|
2222
|
+
}
|
|
2223
|
+
});
|
|
2224
|
+
const conv = chat.realtime.conversation(conversationId);
|
|
2225
|
+
const offFinished = conv.onCallFinished((evt) => {
|
|
2226
|
+
if (evt.call_id !== callId) return;
|
|
2227
|
+
seq.current++;
|
|
2228
|
+
setState((s) => ({
|
|
2229
|
+
recording: s.recording ? { ...s.recording, active: false } : null,
|
|
2230
|
+
transcription: s.transcription ? { ...s.transcription, active: false } : null
|
|
2231
|
+
}));
|
|
2232
|
+
});
|
|
2233
|
+
const offStatus = onReconnect(chat, refresh);
|
|
2234
|
+
return () => {
|
|
2235
|
+
offCapture();
|
|
2236
|
+
offFinished();
|
|
2237
|
+
offStatus();
|
|
2238
|
+
};
|
|
2239
|
+
}, [chat, callId, conversationId, refresh]);
|
|
2240
|
+
const transcribing = !!state.transcription?.active;
|
|
2241
|
+
useEffect(() => {
|
|
2242
|
+
if (!conversationId || !transcribing) {
|
|
2243
|
+
setTranscriptConsent(false);
|
|
2244
|
+
return;
|
|
2245
|
+
}
|
|
2246
|
+
let cancelled = false;
|
|
2247
|
+
chat.calls.recording.settings(conversationId).then((s) => {
|
|
2248
|
+
if (!cancelled) setTranscriptConsent(s.effective.consent === "required");
|
|
2249
|
+
}).catch(() => {
|
|
2250
|
+
});
|
|
2251
|
+
return () => {
|
|
2252
|
+
cancelled = true;
|
|
2253
|
+
};
|
|
2254
|
+
}, [chat, conversationId, transcribing]);
|
|
2255
|
+
const run = useCallback(async (fn) => {
|
|
2256
|
+
setPending(true);
|
|
2257
|
+
setError(null);
|
|
2258
|
+
try {
|
|
2259
|
+
await fn();
|
|
2260
|
+
return true;
|
|
2261
|
+
} catch (err) {
|
|
2262
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
2263
|
+
return false;
|
|
2264
|
+
} finally {
|
|
2265
|
+
setPending(false);
|
|
2266
|
+
}
|
|
2267
|
+
}, []);
|
|
2268
|
+
const start = useCallback(
|
|
2269
|
+
(mode) => run(async () => {
|
|
2270
|
+
if (!callId) throw new Error("no call");
|
|
2271
|
+
const r = await chat.calls.recording.start(callId, mode ? { mode } : {});
|
|
2272
|
+
setLastRecording(r);
|
|
2273
|
+
}),
|
|
2274
|
+
[chat, callId, run]
|
|
2275
|
+
);
|
|
2276
|
+
const stop = useCallback(
|
|
2277
|
+
() => run(async () => {
|
|
2278
|
+
if (!callId) throw new Error("no call");
|
|
2279
|
+
const r = await chat.calls.recording.stop(callId);
|
|
2280
|
+
setLastRecording(r);
|
|
2281
|
+
}),
|
|
2282
|
+
[chat, callId, run]
|
|
2283
|
+
);
|
|
2284
|
+
const consent = useCallback(
|
|
2285
|
+
(decision) => run(async () => {
|
|
2286
|
+
if (!callId) throw new Error("no call");
|
|
2287
|
+
const res = await chat.calls.consent(callId, decision);
|
|
2288
|
+
setState(
|
|
2289
|
+
(s) => applyCaptureEvent(s, {
|
|
2290
|
+
type: "recording:consent",
|
|
2291
|
+
evt: { cv: 1, call_id: res.call_id, user_id: res.user_id, decision: res.decision }
|
|
2292
|
+
})
|
|
2293
|
+
);
|
|
2294
|
+
}),
|
|
2295
|
+
[chat, callId, run]
|
|
2296
|
+
);
|
|
2297
|
+
const rec = state.recording;
|
|
2298
|
+
const active = !!rec?.active;
|
|
2299
|
+
const capturing = active || transcribing;
|
|
2300
|
+
const consentRequired = active && !!rec?.consent_required || transcribing && transcriptConsent;
|
|
2301
|
+
const myConsent = meId && rec ? rec.consent.accepted.includes(meId) || active && rec.started_by_user_id === meId ? "accepted" : rec.consent.declined.includes(meId) ? "declined" : null : null;
|
|
2302
|
+
return {
|
|
2303
|
+
available: rec?.available ?? false,
|
|
2304
|
+
active,
|
|
2305
|
+
recording: rec,
|
|
2306
|
+
transcription: state.transcription,
|
|
2307
|
+
capturing,
|
|
2308
|
+
consentRequired,
|
|
2309
|
+
myConsent,
|
|
2310
|
+
needsConsent: capturing && consentRequired && myConsent !== "accepted",
|
|
2311
|
+
notice,
|
|
2312
|
+
lastRecording,
|
|
2313
|
+
pending,
|
|
2314
|
+
error,
|
|
2315
|
+
start,
|
|
2316
|
+
stop,
|
|
2317
|
+
consent,
|
|
2318
|
+
refresh
|
|
2319
|
+
};
|
|
2320
|
+
}
|
|
2321
|
+
function useLiveTranscript(callId, opts = {}) {
|
|
2322
|
+
const chat = usePoolse();
|
|
2323
|
+
const enabled = opts.enabled ?? true;
|
|
2324
|
+
const max = opts.maxSegments ?? 1e3;
|
|
2325
|
+
const [segments, setSegments] = useState([]);
|
|
2326
|
+
const [transcript, setTranscript] = useState(null);
|
|
2327
|
+
const [conversationId, setConversationId] = useState(null);
|
|
2328
|
+
const [joined, setJoined] = useState(false);
|
|
2329
|
+
const [error, setError] = useState(null);
|
|
2330
|
+
const [pending, setPending] = useState(false);
|
|
2331
|
+
useEffect(() => {
|
|
2332
|
+
setSegments([]);
|
|
2333
|
+
setTranscript(null);
|
|
2334
|
+
setJoined(false);
|
|
2335
|
+
setError(null);
|
|
2336
|
+
if (!callId || !enabled) return;
|
|
2337
|
+
let handle;
|
|
2338
|
+
try {
|
|
2339
|
+
handle = chat.realtime.transcript(callId);
|
|
2340
|
+
} catch (err) {
|
|
2341
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
2342
|
+
return;
|
|
2343
|
+
}
|
|
2344
|
+
const offJoined = handle.onJoined((reply) => {
|
|
2345
|
+
setJoined(true);
|
|
2346
|
+
setError(null);
|
|
2347
|
+
setConversationId(reply.conversation_id || null);
|
|
2348
|
+
setTranscript(reply.transcript);
|
|
2349
|
+
setSegments(reply.segments.slice(-max).map((s) => ({ ...s, final: true })));
|
|
2350
|
+
});
|
|
2351
|
+
const offSegment = handle.onSegment((evt) => {
|
|
2352
|
+
const { cv: _cv, call_id: _call, transcript_id: _t, ...seg } = evt;
|
|
2353
|
+
setSegments((list) => applyLiveSegment(list, seg, max));
|
|
2354
|
+
});
|
|
2355
|
+
const offError = handle.onError((reason) => {
|
|
2356
|
+
setJoined(false);
|
|
2357
|
+
setError(new Error(reason));
|
|
2358
|
+
});
|
|
2359
|
+
return () => {
|
|
2360
|
+
offJoined();
|
|
2361
|
+
offSegment();
|
|
2362
|
+
offError();
|
|
2363
|
+
handle.release();
|
|
2364
|
+
};
|
|
2365
|
+
}, [chat, callId, enabled, max]);
|
|
2366
|
+
useEffect(() => {
|
|
2367
|
+
if (!callId || !conversationId || !enabled) return;
|
|
2368
|
+
const conv = chat.realtime.conversation(conversationId);
|
|
2369
|
+
const apply = (evt) => {
|
|
2370
|
+
if (evt.call_id === callId) setTranscript(evt.transcript);
|
|
2371
|
+
};
|
|
2372
|
+
const offs = [
|
|
2373
|
+
conv.onTranscriptStarted(apply),
|
|
2374
|
+
conv.onTranscriptStopped(apply),
|
|
2375
|
+
conv.onTranscriptReady(apply),
|
|
2376
|
+
conv.onTranscriptSpeakers((evt) => {
|
|
2377
|
+
if (evt.call_id !== callId) return;
|
|
2378
|
+
const names = new Map(evt.speakers.map((s) => [s.key, s.name]));
|
|
2379
|
+
setSegments(
|
|
2380
|
+
(list) => list.map(
|
|
2381
|
+
(s) => names.has(s.speaker) ? { ...s, speaker_name: names.get(s.speaker) } : s
|
|
2382
|
+
)
|
|
2383
|
+
);
|
|
2384
|
+
})
|
|
2385
|
+
];
|
|
2386
|
+
return () => offs.forEach((off) => off());
|
|
2387
|
+
}, [chat, callId, conversationId, enabled]);
|
|
2388
|
+
const run = useCallback(async (fn) => {
|
|
2389
|
+
setPending(true);
|
|
2390
|
+
setError(null);
|
|
2391
|
+
try {
|
|
2392
|
+
setTranscript(await fn());
|
|
2393
|
+
return true;
|
|
2394
|
+
} catch (err) {
|
|
2395
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
2396
|
+
return false;
|
|
2397
|
+
} finally {
|
|
2398
|
+
setPending(false);
|
|
2399
|
+
}
|
|
2400
|
+
}, []);
|
|
2401
|
+
const start = useCallback(
|
|
2402
|
+
() => callId ? run(() => chat.calls.transcription.start(callId)) : Promise.resolve(false),
|
|
2403
|
+
[chat, callId, run]
|
|
2404
|
+
);
|
|
2405
|
+
const stop = useCallback(
|
|
2406
|
+
() => callId ? run(() => chat.calls.transcription.stop(callId)) : Promise.resolve(false),
|
|
2407
|
+
[chat, callId, run]
|
|
2408
|
+
);
|
|
2409
|
+
const finals = useMemo(() => segments.filter((s) => s.final), [segments]);
|
|
2410
|
+
const interim = useMemo(() => segments.filter((s) => !s.final), [segments]);
|
|
2411
|
+
return {
|
|
2412
|
+
segments,
|
|
2413
|
+
finals,
|
|
2414
|
+
interim,
|
|
2415
|
+
transcript,
|
|
2416
|
+
live: transcript?.status === "live",
|
|
2417
|
+
joined,
|
|
2418
|
+
error,
|
|
2419
|
+
pending,
|
|
2420
|
+
start,
|
|
2421
|
+
stop
|
|
2422
|
+
};
|
|
2423
|
+
}
|
|
2424
|
+
function withSpeakers(t, speakers) {
|
|
2425
|
+
const names = new Map(speakers.map((s) => [s.key, s.name]));
|
|
2426
|
+
return {
|
|
2427
|
+
...t,
|
|
2428
|
+
speakers,
|
|
2429
|
+
segments: t.segments.map(
|
|
2430
|
+
(s) => names.has(s.speaker) ? { ...s, speaker_name: names.get(s.speaker) ?? null } : s
|
|
2431
|
+
)
|
|
2432
|
+
};
|
|
2433
|
+
}
|
|
2434
|
+
function useTranscript(callId) {
|
|
2435
|
+
const chat = usePoolse();
|
|
2436
|
+
const [transcript, setTranscript] = useState(null);
|
|
2437
|
+
const [loading, setLoading] = useState(!!callId);
|
|
2438
|
+
const [missing, setMissing] = useState(false);
|
|
2439
|
+
const [error, setError] = useState(null);
|
|
2440
|
+
const seq = useRef(0);
|
|
2441
|
+
const refresh = useCallback(() => {
|
|
2442
|
+
if (!callId) return;
|
|
2443
|
+
const mine = ++seq.current;
|
|
2444
|
+
setLoading(true);
|
|
2445
|
+
chat.calls.transcript.get(callId).then((t) => {
|
|
2446
|
+
if (mine !== seq.current) return;
|
|
2447
|
+
setTranscript(t);
|
|
2448
|
+
setMissing(false);
|
|
2449
|
+
setError(null);
|
|
2450
|
+
}).catch((err) => {
|
|
2451
|
+
if (mine !== seq.current) return;
|
|
2452
|
+
if (err instanceof ApiError && err.status === 404) {
|
|
2453
|
+
setMissing(true);
|
|
2454
|
+
setTranscript(null);
|
|
2455
|
+
} else setError(err instanceof Error ? err : new Error(String(err)));
|
|
2456
|
+
}).finally(() => {
|
|
2457
|
+
if (mine === seq.current) setLoading(false);
|
|
2458
|
+
});
|
|
2459
|
+
}, [chat, callId]);
|
|
2460
|
+
useEffect(() => {
|
|
2461
|
+
setTranscript(null);
|
|
2462
|
+
setMissing(false);
|
|
2463
|
+
setError(null);
|
|
2464
|
+
setLoading(!!callId);
|
|
2465
|
+
refresh();
|
|
2466
|
+
}, [callId, refresh]);
|
|
2467
|
+
const conversationId = transcript?.conversation_id ?? null;
|
|
2468
|
+
useEffect(() => {
|
|
2469
|
+
if (!callId || !conversationId) return;
|
|
2470
|
+
const conv = chat.realtime.conversation(conversationId);
|
|
2471
|
+
const offSpeakers = conv.onTranscriptSpeakers((evt) => {
|
|
2472
|
+
if (evt.call_id !== callId) return;
|
|
2473
|
+
seq.current++;
|
|
2474
|
+
setTranscript((t) => t ? withSpeakers(t, evt.speakers) : t);
|
|
2475
|
+
});
|
|
2476
|
+
const offReady = conv.onTranscriptReady((evt) => {
|
|
2477
|
+
if (evt.call_id === callId) refresh();
|
|
2478
|
+
});
|
|
2479
|
+
const offStatus = onReconnect(chat, refresh);
|
|
2480
|
+
return () => {
|
|
2481
|
+
offSpeakers();
|
|
2482
|
+
offReady();
|
|
2483
|
+
offStatus();
|
|
2484
|
+
};
|
|
2485
|
+
}, [chat, callId, conversationId, refresh]);
|
|
2486
|
+
const renameSpeakers = useCallback(
|
|
2487
|
+
async (names) => {
|
|
2488
|
+
if (!callId) return;
|
|
2489
|
+
const t = await chat.calls.transcript.renameSpeakers(callId, names);
|
|
2490
|
+
seq.current++;
|
|
2491
|
+
setTranscript(t);
|
|
2492
|
+
},
|
|
2493
|
+
[chat, callId]
|
|
2494
|
+
);
|
|
2495
|
+
return { transcript, loading, missing, error, renameSpeakers, refresh };
|
|
2496
|
+
}
|
|
2497
|
+
function useCaptureList(conversationId, load, subscribe) {
|
|
2498
|
+
const chat = usePoolse();
|
|
2499
|
+
const [items, setItems] = useState([]);
|
|
2500
|
+
const [loading, setLoading] = useState(!!conversationId);
|
|
2501
|
+
const [error, setError] = useState(null);
|
|
2502
|
+
const seq = useRef(0);
|
|
2503
|
+
const loadRef = useRef(load);
|
|
2504
|
+
loadRef.current = load;
|
|
2505
|
+
const subscribeRef = useRef(subscribe);
|
|
2506
|
+
subscribeRef.current = subscribe;
|
|
2507
|
+
const refresh = useCallback(() => {
|
|
2508
|
+
if (!conversationId) return;
|
|
2509
|
+
const mine = ++seq.current;
|
|
2510
|
+
setLoading(true);
|
|
2511
|
+
loadRef.current(conversationId).then((list) => {
|
|
2512
|
+
if (mine !== seq.current) return;
|
|
2513
|
+
setItems(list);
|
|
2514
|
+
setError(null);
|
|
2515
|
+
}).catch((err) => {
|
|
2516
|
+
if (mine === seq.current) setError(err instanceof Error ? err : new Error(String(err)));
|
|
2517
|
+
}).finally(() => {
|
|
2518
|
+
if (mine === seq.current) setLoading(false);
|
|
2519
|
+
});
|
|
2520
|
+
}, [conversationId]);
|
|
2521
|
+
useEffect(() => {
|
|
2522
|
+
setItems([]);
|
|
2523
|
+
if (!conversationId) {
|
|
2524
|
+
setLoading(false);
|
|
2525
|
+
return;
|
|
2526
|
+
}
|
|
2527
|
+
refresh();
|
|
2528
|
+
const off = subscribeRef.current(conversationId, refresh);
|
|
2529
|
+
const offStatus = onReconnect(chat, refresh);
|
|
2530
|
+
return () => {
|
|
2531
|
+
off();
|
|
2532
|
+
offStatus();
|
|
2533
|
+
};
|
|
2534
|
+
}, [chat, conversationId, refresh]);
|
|
2535
|
+
return { items, loading, error, refresh };
|
|
2536
|
+
}
|
|
2537
|
+
function useTranscripts(conversationId, opts = {}) {
|
|
2538
|
+
const chat = usePoolse();
|
|
2539
|
+
return useCaptureList(
|
|
2540
|
+
conversationId,
|
|
2541
|
+
(id) => chat.calls.transcript.list(id, opts.limit !== void 0 ? { limit: opts.limit } : {}),
|
|
2542
|
+
(id, refresh) => {
|
|
2543
|
+
const conv = chat.realtime.conversation(id);
|
|
2544
|
+
const offs = [conv.onTranscriptStarted(refresh), conv.onTranscriptReady(refresh)];
|
|
2545
|
+
return () => offs.forEach((off) => off());
|
|
2546
|
+
}
|
|
2547
|
+
);
|
|
2548
|
+
}
|
|
2549
|
+
function useRecordings(conversationId, opts = {}) {
|
|
2550
|
+
const chat = usePoolse();
|
|
2551
|
+
return useCaptureList(
|
|
2552
|
+
conversationId,
|
|
2553
|
+
(id) => chat.calls.recording.list(id, opts.limit !== void 0 ? { limit: opts.limit } : {}),
|
|
2554
|
+
(id, refresh) => {
|
|
2555
|
+
const conv = chat.realtime.conversation(id);
|
|
2556
|
+
const offs = [
|
|
2557
|
+
conv.onRecordingStarted(refresh),
|
|
2558
|
+
conv.onRecordingStopped(refresh),
|
|
2559
|
+
conv.onRecordingReady(refresh)
|
|
2560
|
+
];
|
|
2561
|
+
return () => offs.forEach((off) => off());
|
|
2562
|
+
}
|
|
2563
|
+
);
|
|
2564
|
+
}
|
|
2052
2565
|
function useReadReceipts() {
|
|
2053
2566
|
const chat = usePoolse();
|
|
2054
2567
|
const { me } = useMe();
|
|
@@ -2078,6 +2591,6 @@ function canEditOwnMessage(args) {
|
|
|
2078
2591
|
return Number.isFinite(sent) && (args.now ?? Date.now()) - sent < EDIT_WINDOW_MS;
|
|
2079
2592
|
}
|
|
2080
2593
|
|
|
2081
|
-
export { EDIT_WINDOW_MS, PoolseProvider, canEditOwnMessage, computeGroupPosition, filterConversations, formatDayLabel, orderUnread, receiptsOf, sameDay, sameGroup, useAppEvent, useAttachmentUpload, useAttachmentUrl, useCallRoom, useClientTimings, useConversation, useConversationPresence, useConversations, useLiveCall, useMe, useMembers, useMessageStreams, useMessages, useOutbox, usePoolse, usePresence, usePresenceMany, useReactions, useReadReceipts, useRealtimeStatus, useRoom, useThread, useTyping, useUnreadQueue, useUnreadTotals, useUser, useUserPresence };
|
|
2594
|
+
export { EDIT_WINDOW_MS, PoolseProvider, applyCaptureEvent, canEditOwnMessage, computeGroupPosition, filterConversations, formatDayLabel, orderUnread, receiptsOf, sameDay, sameGroup, useAppEvent, useAttachmentUpload, useAttachmentUrl, useCallRoom, useClientTimings, useConversation, useConversationPresence, useConversations, useLiveCall, useLiveTranscript, useMe, useMembers, useMessageStreams, useMessages, useOutbox, usePoolse, usePresence, usePresenceMany, useReactions, useReadReceipts, useRealtimeStatus, useRecording, useRecordings, useRoom, useThread, useTranscript, useTranscripts, useTyping, useUnreadQueue, useUnreadTotals, useUser, useUserPresence };
|
|
2082
2595
|
//# sourceMappingURL=index.js.map
|
|
2083
2596
|
//# sourceMappingURL=index.js.map
|