@lunora/react 1.0.0-alpha.26 → 1.0.0-alpha.260
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/README.md +28 -18
- package/dist/index.d.mts +1036 -583
- package/dist/index.d.ts +1036 -583
- package/dist/index.mjs +1 -25
- package/dist/packem_shared/AuthLoading-BNDsvgQ4.mjs +2 -0
- package/dist/packem_shared/LunoraProvider-B5zAiX86.mjs +1 -0
- package/dist/packem_shared/hydratePreloaded-B6d3r_u1.mjs +2 -0
- package/dist/packem_shared/lunora-provider-CW9Gd-pp.mjs +2 -0
- package/dist/packem_shared/lunoraQueryOptions-IJB_aFzi.mjs +1 -0
- package/dist/packem_shared/query-core-CqnQ_-E5.mjs +2 -0
- package/dist/packem_shared/query-key-BysXrewF.mjs +1 -0
- package/dist/packem_shared/query-options.d-CdgGQ9s4.d.mts +38 -0
- package/dist/packem_shared/query-options.d-CdgGQ9s4.d.ts +38 -0
- package/dist/packem_shared/stream-state-abygFxmy.mjs +1 -0
- package/dist/packem_shared/use-paginated-core-D9QnfDFs.mjs +2 -0
- package/dist/packem_shared/useAction-DnbG-lGf.mjs +2 -0
- package/dist/packem_shared/useAgent-C4XTDKr3.mjs +2 -0
- package/dist/packem_shared/useAgentApprovals-DIrIfUkR.mjs +2 -0
- package/dist/packem_shared/useAgentChat-DgdA3BU5.mjs +2 -0
- package/dist/packem_shared/useAgentState-CZkoGPL4.mjs +2 -0
- package/dist/packem_shared/useAgentToolEvents-BrIFgqyT.mjs +2 -0
- package/dist/packem_shared/useAuth-Do03WzEi.mjs +2 -0
- package/dist/packem_shared/useAuthSessions-BYqSu44Q.mjs +2 -0
- package/dist/packem_shared/useAuthState-CR-hIsvP.mjs +2 -0
- package/dist/packem_shared/useClientQuery-47csppXE.mjs +2 -0
- package/dist/packem_shared/useConnectionStatus-B4A2Vw8x.mjs +2 -0
- package/dist/packem_shared/useFlag-DhCr93VG.mjs +2 -0
- package/dist/packem_shared/useHttpStream-D0biamGu.mjs +2 -0
- package/dist/packem_shared/useInfiniteQuery-C6B9M0s9.mjs +2 -0
- package/dist/packem_shared/useLiveTopic-COTTJo8q.mjs +2 -0
- package/dist/packem_shared/useMutation-DmI3aZBl.mjs +2 -0
- package/dist/packem_shared/useMutator-BtUd-xEA.mjs +2 -0
- package/dist/packem_shared/usePaginatedQuery-CK5xnM3r.mjs +2 -0
- package/dist/packem_shared/usePresence-Bgw_pYzz.mjs +2 -0
- package/dist/packem_shared/useQuery-CHjpBW-E.mjs +2 -0
- package/dist/packem_shared/useRateLimit-Cckxi8m9.mjs +2 -0
- package/dist/packem_shared/useStream-De5SDR6Y.mjs +2 -0
- package/dist/packem_shared/useSubscription-CC39EoMT.mjs +2 -0
- package/dist/packem_shared/useSuspenseQuery-ZsmWeJKm.mjs +2 -0
- package/dist/packem_shared/useVoiceAgent-TyztaWYJ.mjs +2 -0
- package/dist/payment.d.mts +107 -0
- package/dist/payment.d.ts +107 -0
- package/dist/payment.mjs +2 -0
- package/dist/server.d.mts +47 -29
- package/dist/server.d.ts +47 -29
- package/dist/server.mjs +1 -31
- package/dist/upload.d.mts +24 -0
- package/dist/upload.d.ts +24 -0
- package/dist/upload.mjs +2 -0
- package/package.json +14 -4
- package/dist/packem_shared/AuthLoading-DezUWNB2.mjs +0 -33
- package/dist/packem_shared/CheckoutButton-DUite8jJ.mjs +0 -189
- package/dist/packem_shared/LunoraProvider-BsuiW4Lk.mjs +0 -81
- package/dist/packem_shared/cache-D1O_xfAe.mjs +0 -80
- package/dist/packem_shared/hydratePreloaded-uMDH_xA9.mjs +0 -54
- package/dist/packem_shared/lunoraQueryOptions-C3uwOTMW.mjs +0 -16
- package/dist/packem_shared/query-key-BmCbgidV.mjs +0 -13
- package/dist/packem_shared/query-options.d-D4okOpO8.d.mts +0 -38
- package/dist/packem_shared/query-options.d-D4okOpO8.d.ts +0 -38
- package/dist/packem_shared/stable-key-CGp4e2Ux.mjs +0 -38
- package/dist/packem_shared/use-paginated-core-DafedU4l.mjs +0 -179
- package/dist/packem_shared/useAgent-DwLxTc3P.mjs +0 -101
- package/dist/packem_shared/useAgentChat-WPm5iHgR.mjs +0 -283
- package/dist/packem_shared/useAgentState-Cikdlomg.mjs +0 -37
- package/dist/packem_shared/useAgentToolEvents-BM7ERo00.mjs +0 -129
- package/dist/packem_shared/useAuth-BSutjJaa.mjs +0 -129
- package/dist/packem_shared/useAuthState-CyF35qZR.mjs +0 -36
- package/dist/packem_shared/useClientQuery-CN1sC3cP.mjs +0 -56
- package/dist/packem_shared/useConnectionStatus-PJ7vyCHy.mjs +0 -30
- package/dist/packem_shared/useFlag-C2Pkghli.mjs +0 -128
- package/dist/packem_shared/useInfiniteQuery-BeaehE9f.mjs +0 -97
- package/dist/packem_shared/useMutation-BeqeIjTr.mjs +0 -67
- package/dist/packem_shared/useMutator-IIrLtDiM.mjs +0 -47
- package/dist/packem_shared/usePaginatedQuery-_6ETVUmf.mjs +0 -46
- package/dist/packem_shared/usePresence-Rnx1Fznc.mjs +0 -115
- package/dist/packem_shared/useQuery-C-hDzP-t.mjs +0 -77
- package/dist/packem_shared/useRateLimit-DTEffQEi.mjs +0 -64
- package/dist/packem_shared/useStream-CGC26afa.mjs +0 -128
- package/dist/packem_shared/useSubscription-D38Jdyr8.mjs +0 -139
- package/dist/packem_shared/useVoiceAgent-sC5_ADB8.mjs +0 -416
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import { useReducer, useRef, useEffect } from 'react';
|
|
3
|
-
import { useLunora } from './LunoraProvider-BsuiW4Lk.mjs';
|
|
4
|
-
import { s as stableStringify } from './stable-key-CGp4e2Ux.mjs';
|
|
5
|
-
|
|
6
|
-
const reducer = function(state, action) {
|
|
7
|
-
switch (action.type) {
|
|
8
|
-
case "chunk": {
|
|
9
|
-
return {
|
|
10
|
-
chunks: [...state.chunks, action.chunk],
|
|
11
|
-
error: void 0,
|
|
12
|
-
status: "streaming"
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
case "complete": {
|
|
16
|
-
return {
|
|
17
|
-
...state,
|
|
18
|
-
status: "complete"
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
case "error": {
|
|
22
|
-
return {
|
|
23
|
-
...state,
|
|
24
|
-
error: action.error,
|
|
25
|
-
status: "error"
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
case "reset": {
|
|
29
|
-
return {
|
|
30
|
-
chunks: [],
|
|
31
|
-
error: void 0,
|
|
32
|
-
status: "idle"
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
case "start": {
|
|
36
|
-
return {
|
|
37
|
-
...state,
|
|
38
|
-
status: "streaming"
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
default: {
|
|
42
|
-
return state;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const useStream = (function_, args, options = {}) => {
|
|
47
|
-
const client = useLunora();
|
|
48
|
-
const [state, dispatch] = useReducer(reducer, {
|
|
49
|
-
chunks: [],
|
|
50
|
-
error: void 0,
|
|
51
|
-
status: "idle"
|
|
52
|
-
});
|
|
53
|
-
const skipped = args === "skip";
|
|
54
|
-
const serialized = skipped ? "skip" : stableStringify(args);
|
|
55
|
-
const cancelRef = useRef(void 0);
|
|
56
|
-
useEffect(() => {
|
|
57
|
-
if (skipped) {
|
|
58
|
-
dispatch({
|
|
59
|
-
type: "reset"
|
|
60
|
-
});
|
|
61
|
-
return () => {
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
dispatch({
|
|
65
|
-
type: "reset"
|
|
66
|
-
});
|
|
67
|
-
dispatch({
|
|
68
|
-
type: "start"
|
|
69
|
-
});
|
|
70
|
-
let stillMounted = true;
|
|
71
|
-
let cancelled = false;
|
|
72
|
-
const iterable = client.stream(function_, args, {
|
|
73
|
-
maxBuffer: options.maxBuffer,
|
|
74
|
-
shardKey: options.shardKey
|
|
75
|
-
});
|
|
76
|
-
const cancel = () => {
|
|
77
|
-
if (cancelled) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
cancelled = true;
|
|
81
|
-
iterable.cancel();
|
|
82
|
-
};
|
|
83
|
-
cancelRef.current = cancel;
|
|
84
|
-
(async () => {
|
|
85
|
-
try {
|
|
86
|
-
for await (const chunk of iterable) {
|
|
87
|
-
if (!stillMounted) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
dispatch({
|
|
91
|
-
chunk,
|
|
92
|
-
type: "chunk"
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
if (stillMounted) {
|
|
96
|
-
dispatch({
|
|
97
|
-
type: "complete"
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
} catch (error) {
|
|
101
|
-
if (!stillMounted) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
105
|
-
dispatch({
|
|
106
|
-
error: normalized,
|
|
107
|
-
type: "error"
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
})().catch(() => {
|
|
111
|
-
});
|
|
112
|
-
return () => {
|
|
113
|
-
stillMounted = false;
|
|
114
|
-
cancel();
|
|
115
|
-
cancelRef.current = void 0;
|
|
116
|
-
};
|
|
117
|
-
}, [client, function_.__lunoraRef, serialized, skipped, options.shardKey, options.maxBuffer]);
|
|
118
|
-
return {
|
|
119
|
-
cancel: () => {
|
|
120
|
-
cancelRef.current?.();
|
|
121
|
-
},
|
|
122
|
-
chunks: state.chunks,
|
|
123
|
-
error: state.error,
|
|
124
|
-
status: state.status
|
|
125
|
-
};
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
export { useStream };
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import { c } from 'react/compiler-runtime';
|
|
3
|
-
import { createQuerySubscription } from '@lunora/client/query';
|
|
4
|
-
import { useState, useRef, useEffect } from 'react';
|
|
5
|
-
import { useLunora } from './LunoraProvider-BsuiW4Lk.mjs';
|
|
6
|
-
import { s as stableStringify } from './stable-key-CGp4e2Ux.mjs';
|
|
7
|
-
|
|
8
|
-
const useSubscription = (function_, args, t0) => {
|
|
9
|
-
const $ = c(22);
|
|
10
|
-
let t1;
|
|
11
|
-
if ($[0] !== t0) {
|
|
12
|
-
t1 = t0 === void 0 ? {} : t0;
|
|
13
|
-
$[0] = t0;
|
|
14
|
-
$[1] = t1;
|
|
15
|
-
} else {
|
|
16
|
-
t1 = $[1];
|
|
17
|
-
}
|
|
18
|
-
const options = t1;
|
|
19
|
-
const client = useLunora();
|
|
20
|
-
let t2;
|
|
21
|
-
if ($[2] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel")) {
|
|
22
|
-
t2 = {
|
|
23
|
-
data: void 0,
|
|
24
|
-
error: void 0
|
|
25
|
-
};
|
|
26
|
-
$[2] = t2;
|
|
27
|
-
} else {
|
|
28
|
-
t2 = $[2];
|
|
29
|
-
}
|
|
30
|
-
const [state, setState] = useState(t2);
|
|
31
|
-
const skipped = args === "skip";
|
|
32
|
-
let t3;
|
|
33
|
-
if ($[3] !== args || $[4] !== skipped) {
|
|
34
|
-
t3 = skipped ? "skip" : stableStringify(args);
|
|
35
|
-
$[3] = args;
|
|
36
|
-
$[4] = skipped;
|
|
37
|
-
$[5] = t3;
|
|
38
|
-
} else {
|
|
39
|
-
t3 = $[5];
|
|
40
|
-
}
|
|
41
|
-
const serialized = t3;
|
|
42
|
-
let t4;
|
|
43
|
-
if ($[6] !== args || $[7] !== function_) {
|
|
44
|
-
t4 = {
|
|
45
|
-
args,
|
|
46
|
-
fn: function_
|
|
47
|
-
};
|
|
48
|
-
$[6] = args;
|
|
49
|
-
$[7] = function_;
|
|
50
|
-
$[8] = t4;
|
|
51
|
-
} else {
|
|
52
|
-
t4 = $[8];
|
|
53
|
-
}
|
|
54
|
-
const subscribeRef = useRef(t4);
|
|
55
|
-
let t5;
|
|
56
|
-
if ($[9] !== args || $[10] !== function_) {
|
|
57
|
-
t5 = () => {
|
|
58
|
-
subscribeRef.current = {
|
|
59
|
-
args,
|
|
60
|
-
fn: function_
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
$[9] = args;
|
|
64
|
-
$[10] = function_;
|
|
65
|
-
$[11] = t5;
|
|
66
|
-
} else {
|
|
67
|
-
t5 = $[11];
|
|
68
|
-
}
|
|
69
|
-
useEffect(t5);
|
|
70
|
-
let t6;
|
|
71
|
-
if ($[12] !== client || $[13] !== options.shardKey || $[14] !== skipped) {
|
|
72
|
-
t6 = () => {
|
|
73
|
-
if (skipped) {
|
|
74
|
-
setState({
|
|
75
|
-
data: void 0,
|
|
76
|
-
error: void 0
|
|
77
|
-
});
|
|
78
|
-
return _temp;
|
|
79
|
-
}
|
|
80
|
-
let cancelled = false;
|
|
81
|
-
const {
|
|
82
|
-
args: currentArgs,
|
|
83
|
-
fn: currentFunction
|
|
84
|
-
} = subscribeRef.current;
|
|
85
|
-
const unsubscribe = createQuerySubscription(client, currentFunction, currentArgs, {
|
|
86
|
-
onData: (value) => {
|
|
87
|
-
if (cancelled) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
setState({
|
|
91
|
-
data: value,
|
|
92
|
-
error: void 0
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
onError: (error) => {
|
|
96
|
-
const normalized = new Error(error.message);
|
|
97
|
-
queueMicrotask(() => {
|
|
98
|
-
if (!cancelled) {
|
|
99
|
-
setState({
|
|
100
|
-
data: void 0,
|
|
101
|
-
error: normalized
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
}, {
|
|
107
|
-
shardKey: options.shardKey
|
|
108
|
-
});
|
|
109
|
-
return () => {
|
|
110
|
-
cancelled = true;
|
|
111
|
-
unsubscribe();
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
$[12] = client;
|
|
115
|
-
$[13] = options.shardKey;
|
|
116
|
-
$[14] = skipped;
|
|
117
|
-
$[15] = t6;
|
|
118
|
-
} else {
|
|
119
|
-
t6 = $[15];
|
|
120
|
-
}
|
|
121
|
-
let t7;
|
|
122
|
-
if ($[16] !== client || $[17] !== function_.__lunoraRef || $[18] !== options.shardKey || $[19] !== serialized || $[20] !== skipped) {
|
|
123
|
-
t7 = [client, function_.__lunoraRef, serialized, options.shardKey, skipped];
|
|
124
|
-
$[16] = client;
|
|
125
|
-
$[17] = function_.__lunoraRef;
|
|
126
|
-
$[18] = options.shardKey;
|
|
127
|
-
$[19] = serialized;
|
|
128
|
-
$[20] = skipped;
|
|
129
|
-
$[21] = t7;
|
|
130
|
-
} else {
|
|
131
|
-
t7 = $[21];
|
|
132
|
-
}
|
|
133
|
-
useEffect(t6, t7);
|
|
134
|
-
return state;
|
|
135
|
-
};
|
|
136
|
-
function _temp() {
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export { useSubscription as default };
|
|
@@ -1,416 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import { useState, useRef, useCallback, useEffect } from 'react';
|
|
3
|
-
import { useLunora } from './LunoraProvider-BsuiW4Lk.mjs';
|
|
4
|
-
|
|
5
|
-
const TARGET_SAMPLE_RATE = 16e3;
|
|
6
|
-
const blockRms = (samples) => {
|
|
7
|
-
if (samples.length === 0) {
|
|
8
|
-
return 0;
|
|
9
|
-
}
|
|
10
|
-
let sum = 0;
|
|
11
|
-
for (const sample of samples) {
|
|
12
|
-
sum += sample * sample;
|
|
13
|
-
}
|
|
14
|
-
return Math.sqrt(sum / samples.length);
|
|
15
|
-
};
|
|
16
|
-
const toPcm16 = (samples, inputSampleRate) => {
|
|
17
|
-
const ratio = inputSampleRate / TARGET_SAMPLE_RATE;
|
|
18
|
-
const outLength = ratio > 1 ? Math.floor(samples.length / ratio) : samples.length;
|
|
19
|
-
const buffer = new ArrayBuffer(outLength * 2);
|
|
20
|
-
const view = new DataView(buffer);
|
|
21
|
-
for (let index = 0; index < outLength; index += 1) {
|
|
22
|
-
const sample = samples[Math.floor(index * ratio)] ?? 0;
|
|
23
|
-
const clamped = Math.max(-1, Math.min(1, sample));
|
|
24
|
-
view.setInt16(index * 2, clamped < 0 ? clamped * 32768 : clamped * 32767, true);
|
|
25
|
-
}
|
|
26
|
-
return new Uint8Array(buffer);
|
|
27
|
-
};
|
|
28
|
-
const createBrowserMicrophone = async (config) => {
|
|
29
|
-
const media = globalThis;
|
|
30
|
-
const getUserMedia = media.navigator?.mediaDevices?.getUserMedia.bind(media.navigator.mediaDevices);
|
|
31
|
-
const AudioContextClass = media.AudioContext ?? media.webkitAudioContext;
|
|
32
|
-
if (!getUserMedia || !AudioContextClass) {
|
|
33
|
-
throw new Error("useVoiceAgent: microphone capture requires getUserMedia + AudioContext (no browser audio available)");
|
|
34
|
-
}
|
|
35
|
-
const stream = await getUserMedia({
|
|
36
|
-
audio: {
|
|
37
|
-
channelCount: 1,
|
|
38
|
-
echoCancellation: true,
|
|
39
|
-
noiseSuppression: true
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
const context = new AudioContextClass();
|
|
43
|
-
const source = context.createMediaStreamSource(stream);
|
|
44
|
-
const processor = context.createScriptProcessor(4096, 1, 1);
|
|
45
|
-
let muted = false;
|
|
46
|
-
let sawSpeech = false;
|
|
47
|
-
let silentFor = 0;
|
|
48
|
-
let loudChunks = 0;
|
|
49
|
-
processor.onaudioprocess = (event) => {
|
|
50
|
-
const samples = event.inputBuffer.getChannelData(0);
|
|
51
|
-
const rms = muted ? 0 : blockRms(samples);
|
|
52
|
-
config.onLevel(rms);
|
|
53
|
-
if (muted) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
config.onAudio(toPcm16(samples, context.sampleRate));
|
|
57
|
-
if (config.isSpeaking()) {
|
|
58
|
-
loudChunks = rms >= config.interruptThreshold ? loudChunks + 1 : 0;
|
|
59
|
-
if (loudChunks >= config.interruptChunks) {
|
|
60
|
-
loudChunks = 0;
|
|
61
|
-
config.onInterrupt();
|
|
62
|
-
}
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
loudChunks = 0;
|
|
66
|
-
const chunkMs = samples.length / context.sampleRate * 1e3;
|
|
67
|
-
if (rms >= config.silenceThreshold) {
|
|
68
|
-
sawSpeech = true;
|
|
69
|
-
silentFor = 0;
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
if (sawSpeech) {
|
|
73
|
-
silentFor += chunkMs;
|
|
74
|
-
if (silentFor >= config.silenceDurationMs) {
|
|
75
|
-
sawSpeech = false;
|
|
76
|
-
silentFor = 0;
|
|
77
|
-
config.onSilence();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
source.connect(processor);
|
|
82
|
-
processor.connect(context.destination);
|
|
83
|
-
return {
|
|
84
|
-
setMuted: (next) => {
|
|
85
|
-
muted = next;
|
|
86
|
-
},
|
|
87
|
-
stop: () => {
|
|
88
|
-
processor.disconnect();
|
|
89
|
-
source.disconnect();
|
|
90
|
-
for (const track of stream.getTracks()) {
|
|
91
|
-
track.stop();
|
|
92
|
-
}
|
|
93
|
-
void context.close();
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
const createBrowserSpeaker = () => {
|
|
98
|
-
const media = globalThis;
|
|
99
|
-
const AudioContextClass = media.AudioContext ?? media.webkitAudioContext;
|
|
100
|
-
if (!AudioContextClass) {
|
|
101
|
-
throw new Error("useVoiceAgent: audio playback requires AudioContext (no browser audio available)");
|
|
102
|
-
}
|
|
103
|
-
const context = new AudioContextClass();
|
|
104
|
-
const sources = /* @__PURE__ */ new Set();
|
|
105
|
-
let playHead = 0;
|
|
106
|
-
let chain = Promise.resolve();
|
|
107
|
-
let generation = 0;
|
|
108
|
-
const scheduleChunk = async (bytes, scheduledGeneration) => {
|
|
109
|
-
if (scheduledGeneration !== generation) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
let decoded;
|
|
113
|
-
try {
|
|
114
|
-
decoded = await context.decodeAudioData(bytes.buffer);
|
|
115
|
-
} catch {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (scheduledGeneration !== generation) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
const node = context.createBufferSource();
|
|
122
|
-
node.buffer = decoded;
|
|
123
|
-
node.connect(context.destination);
|
|
124
|
-
const startAt = Math.max(context.currentTime, playHead);
|
|
125
|
-
node.start(startAt);
|
|
126
|
-
playHead = startAt + decoded.duration;
|
|
127
|
-
sources.add(node);
|
|
128
|
-
node.onended = () => {
|
|
129
|
-
sources.delete(node);
|
|
130
|
-
};
|
|
131
|
-
};
|
|
132
|
-
const enqueue = (audio) => {
|
|
133
|
-
const bytes = Uint8Array.from(audio);
|
|
134
|
-
const scheduledGeneration = generation;
|
|
135
|
-
chain = chain.then(() => scheduleChunk(bytes, scheduledGeneration));
|
|
136
|
-
};
|
|
137
|
-
const interrupt = () => {
|
|
138
|
-
generation += 1;
|
|
139
|
-
for (const node of sources) {
|
|
140
|
-
try {
|
|
141
|
-
node.stop();
|
|
142
|
-
} catch {
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
sources.clear();
|
|
146
|
-
playHead = context.currentTime;
|
|
147
|
-
};
|
|
148
|
-
return {
|
|
149
|
-
enqueue,
|
|
150
|
-
interrupt,
|
|
151
|
-
stop: () => {
|
|
152
|
-
interrupt();
|
|
153
|
-
void context.close();
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const WS_OPEN = 1;
|
|
159
|
-
const DEFAULT_SILENCE_THRESHOLD = 0.01;
|
|
160
|
-
const DEFAULT_SILENCE_DURATION_MS = 1200;
|
|
161
|
-
const DEFAULT_INTERRUPT_THRESHOLD = 0.15;
|
|
162
|
-
const DEFAULT_INTERRUPT_CHUNKS = 3;
|
|
163
|
-
const deriveWebSocketUrl = (url) => {
|
|
164
|
-
if (url.startsWith("https://")) {
|
|
165
|
-
return `wss://${url.slice("https://".length)}`;
|
|
166
|
-
}
|
|
167
|
-
if (url.startsWith("http://")) {
|
|
168
|
-
return `ws://${url.slice("http://".length)}`;
|
|
169
|
-
}
|
|
170
|
-
return url;
|
|
171
|
-
};
|
|
172
|
-
const agentNameFromReference = (voice) => {
|
|
173
|
-
const reference = voice["__lunoraRef"];
|
|
174
|
-
const withoutNamespace = reference.startsWith("agents:") ? reference.slice("agents:".length) : reference;
|
|
175
|
-
return withoutNamespace.endsWith("Voice") ? withoutNamespace.slice(0, -"Voice".length) : withoutNamespace;
|
|
176
|
-
};
|
|
177
|
-
const voiceSocketUrl = (baseUrl, agent, threadKey) => {
|
|
178
|
-
const base = deriveWebSocketUrl(baseUrl);
|
|
179
|
-
const trimmed = base.endsWith("/") ? base.slice(0, -1) : base;
|
|
180
|
-
const search = new URLSearchParams({
|
|
181
|
-
threadKey
|
|
182
|
-
});
|
|
183
|
-
return `${trimmed}/_lunora/voice/${encodeURIComponent(agent)}?${search.toString()}`;
|
|
184
|
-
};
|
|
185
|
-
const useVoiceAgent = (options) => {
|
|
186
|
-
const {
|
|
187
|
-
createMicrophone = createBrowserMicrophone,
|
|
188
|
-
createSpeaker = createBrowserSpeaker,
|
|
189
|
-
createSocket,
|
|
190
|
-
interruptChunks = DEFAULT_INTERRUPT_CHUNKS,
|
|
191
|
-
interruptThreshold = DEFAULT_INTERRUPT_THRESHOLD,
|
|
192
|
-
silenceDurationMs = DEFAULT_SILENCE_DURATION_MS,
|
|
193
|
-
silenceThreshold = DEFAULT_SILENCE_THRESHOLD,
|
|
194
|
-
threadKey,
|
|
195
|
-
voice
|
|
196
|
-
} = options;
|
|
197
|
-
const client = useLunora();
|
|
198
|
-
const [status, setStatus] = useState("idle");
|
|
199
|
-
const [connected, setConnected] = useState(false);
|
|
200
|
-
const [transcript, setTranscript] = useState("");
|
|
201
|
-
const [interimTranscript, setInterimTranscript] = useState("");
|
|
202
|
-
const [audioLevel, setAudioLevel] = useState(0);
|
|
203
|
-
const [isMuted, setIsMuted] = useState(false);
|
|
204
|
-
const [error, setError] = useState(void 0);
|
|
205
|
-
const connectionRef = useRef(void 0);
|
|
206
|
-
const startingRef = useRef(false);
|
|
207
|
-
const sendFrame = useCallback((frame) => {
|
|
208
|
-
const socket = connectionRef.current?.socket;
|
|
209
|
-
if (socket?.readyState === WS_OPEN) {
|
|
210
|
-
socket.send(JSON.stringify(frame));
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
return false;
|
|
214
|
-
}, []);
|
|
215
|
-
const teardown = useCallback(() => {
|
|
216
|
-
const connection = connectionRef.current;
|
|
217
|
-
connectionRef.current = void 0;
|
|
218
|
-
if (connection) {
|
|
219
|
-
connection.microphone?.stop();
|
|
220
|
-
connection.speaker?.stop();
|
|
221
|
-
try {
|
|
222
|
-
connection.socket.close();
|
|
223
|
-
} catch {
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
startingRef.current = false;
|
|
227
|
-
setConnected(false);
|
|
228
|
-
setStatus("idle");
|
|
229
|
-
setAudioLevel(0);
|
|
230
|
-
}, []);
|
|
231
|
-
const endCall = useCallback(() => {
|
|
232
|
-
teardown();
|
|
233
|
-
}, [teardown]);
|
|
234
|
-
const handleServerFrame = useCallback((frame_0) => {
|
|
235
|
-
const connection_0 = connectionRef.current;
|
|
236
|
-
switch (frame_0.type) {
|
|
237
|
-
case "assistant_delta": {
|
|
238
|
-
if (connection_0) {
|
|
239
|
-
connection_0.speaking = true;
|
|
240
|
-
}
|
|
241
|
-
setStatus("speaking");
|
|
242
|
-
setInterimTranscript((previous) => previous + frame_0.text);
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
case "assistant_done": {
|
|
246
|
-
if (connection_0) {
|
|
247
|
-
connection_0.speaking = false;
|
|
248
|
-
}
|
|
249
|
-
setInterimTranscript(frame_0.text);
|
|
250
|
-
setStatus("listening");
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
case "error": {
|
|
254
|
-
if (connection_0) {
|
|
255
|
-
connection_0.speaking = false;
|
|
256
|
-
}
|
|
257
|
-
setError(new Error(frame_0.message));
|
|
258
|
-
setStatus("listening");
|
|
259
|
-
break;
|
|
260
|
-
}
|
|
261
|
-
case "interrupted": {
|
|
262
|
-
if (connection_0) {
|
|
263
|
-
connection_0.speaking = false;
|
|
264
|
-
connection_0.suppressAudio = false;
|
|
265
|
-
}
|
|
266
|
-
connection_0?.speaker?.interrupt();
|
|
267
|
-
setStatus("listening");
|
|
268
|
-
break;
|
|
269
|
-
}
|
|
270
|
-
case "ready": {
|
|
271
|
-
if (connection_0) {
|
|
272
|
-
connection_0.audioFormat = frame_0.audioFormat;
|
|
273
|
-
connection_0.suppressAudio = false;
|
|
274
|
-
}
|
|
275
|
-
setConnected(true);
|
|
276
|
-
setStatus("listening");
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
case "user_transcript": {
|
|
280
|
-
if (connection_0) {
|
|
281
|
-
connection_0.suppressAudio = false;
|
|
282
|
-
}
|
|
283
|
-
setTranscript(frame_0.text);
|
|
284
|
-
setInterimTranscript("");
|
|
285
|
-
setStatus("thinking");
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}, []);
|
|
290
|
-
const handleAudioChunk = useCallback((audio) => {
|
|
291
|
-
const connection_1 = connectionRef.current;
|
|
292
|
-
if (!connection_1 || connection_1.suppressAudio) {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
connection_1.speaker ??= createSpeaker({
|
|
296
|
-
audioFormat: connection_1.audioFormat
|
|
297
|
-
});
|
|
298
|
-
connection_1.speaking = true;
|
|
299
|
-
setStatus("speaking");
|
|
300
|
-
connection_1.speaker.enqueue(audio);
|
|
301
|
-
}, [createSpeaker]);
|
|
302
|
-
const startCall = useCallback(async () => {
|
|
303
|
-
if (connectionRef.current || startingRef.current) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
startingRef.current = true;
|
|
307
|
-
setError(void 0);
|
|
308
|
-
setTranscript("");
|
|
309
|
-
setInterimTranscript("");
|
|
310
|
-
try {
|
|
311
|
-
const url = voiceSocketUrl(client.url, agentNameFromReference(voice), threadKey);
|
|
312
|
-
const openSocket = createSocket ?? ((target) => new globalThis.WebSocket(target));
|
|
313
|
-
const socket_0 = openSocket(url);
|
|
314
|
-
socket_0.binaryType = "arraybuffer";
|
|
315
|
-
const connection_2 = {
|
|
316
|
-
audioFormat: "mp3",
|
|
317
|
-
microphone: void 0,
|
|
318
|
-
socket: socket_0,
|
|
319
|
-
speaker: void 0,
|
|
320
|
-
speaking: false,
|
|
321
|
-
suppressAudio: false
|
|
322
|
-
};
|
|
323
|
-
connectionRef.current = connection_2;
|
|
324
|
-
socket_0.onmessage = (event) => {
|
|
325
|
-
if (typeof event.data === "string") {
|
|
326
|
-
try {
|
|
327
|
-
handleServerFrame(JSON.parse(event.data));
|
|
328
|
-
} catch {
|
|
329
|
-
}
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
handleAudioChunk(new Uint8Array(event.data));
|
|
333
|
-
};
|
|
334
|
-
socket_0.onerror = () => {
|
|
335
|
-
setError(new Error("useVoiceAgent: voice socket error"));
|
|
336
|
-
};
|
|
337
|
-
socket_0.onclose = () => {
|
|
338
|
-
if (connectionRef.current === connection_2) {
|
|
339
|
-
teardown();
|
|
340
|
-
}
|
|
341
|
-
};
|
|
342
|
-
const microphone = await createMicrophone({
|
|
343
|
-
interruptChunks,
|
|
344
|
-
interruptThreshold,
|
|
345
|
-
isSpeaking: () => connectionRef.current?.speaking ?? false,
|
|
346
|
-
onAudio: (pcm) => {
|
|
347
|
-
if (socket_0.readyState === WS_OPEN) {
|
|
348
|
-
socket_0.send(pcm);
|
|
349
|
-
}
|
|
350
|
-
},
|
|
351
|
-
onInterrupt: () => {
|
|
352
|
-
sendFrame({
|
|
353
|
-
type: "interrupt"
|
|
354
|
-
});
|
|
355
|
-
connectionRef.current?.speaker?.interrupt();
|
|
356
|
-
if (connectionRef.current) {
|
|
357
|
-
connectionRef.current.speaking = false;
|
|
358
|
-
connectionRef.current.suppressAudio = true;
|
|
359
|
-
}
|
|
360
|
-
setStatus("listening");
|
|
361
|
-
},
|
|
362
|
-
onLevel: setAudioLevel,
|
|
363
|
-
onSilence: () => {
|
|
364
|
-
sendFrame({
|
|
365
|
-
type: "commit"
|
|
366
|
-
});
|
|
367
|
-
setStatus("thinking");
|
|
368
|
-
},
|
|
369
|
-
silenceDurationMs,
|
|
370
|
-
silenceThreshold
|
|
371
|
-
});
|
|
372
|
-
if (connectionRef.current === connection_2) {
|
|
373
|
-
connection_2.microphone = microphone;
|
|
374
|
-
setIsMuted(false);
|
|
375
|
-
setStatus("listening");
|
|
376
|
-
} else {
|
|
377
|
-
microphone.stop();
|
|
378
|
-
}
|
|
379
|
-
} catch (error_) {
|
|
380
|
-
setError(error_ instanceof Error ? error_ : new Error(String(error_)));
|
|
381
|
-
teardown();
|
|
382
|
-
} finally {
|
|
383
|
-
startingRef.current = false;
|
|
384
|
-
}
|
|
385
|
-
}, [client, createMicrophone, createSocket, handleAudioChunk, handleServerFrame, interruptChunks, interruptThreshold, sendFrame, silenceDurationMs, silenceThreshold, teardown, threadKey, voice]);
|
|
386
|
-
const toggleMute = useCallback(() => {
|
|
387
|
-
const next = !isMuted;
|
|
388
|
-
connectionRef.current?.microphone?.setMuted(next);
|
|
389
|
-
setIsMuted(next);
|
|
390
|
-
return next;
|
|
391
|
-
}, [isMuted]);
|
|
392
|
-
const sendText = useCallback((text) => {
|
|
393
|
-
if (sendFrame({
|
|
394
|
-
text,
|
|
395
|
-
type: "text"
|
|
396
|
-
})) {
|
|
397
|
-
setStatus("thinking");
|
|
398
|
-
}
|
|
399
|
-
}, [sendFrame]);
|
|
400
|
-
useEffect(() => teardown, [teardown]);
|
|
401
|
-
return {
|
|
402
|
-
audioLevel,
|
|
403
|
-
connected,
|
|
404
|
-
endCall,
|
|
405
|
-
error,
|
|
406
|
-
interimTranscript,
|
|
407
|
-
isMuted,
|
|
408
|
-
sendText,
|
|
409
|
-
startCall,
|
|
410
|
-
status,
|
|
411
|
-
toggleMute,
|
|
412
|
-
transcript
|
|
413
|
-
};
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
export { useVoiceAgent };
|