cencori 1.4.0 → 1.5.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/README.md +25 -0
- package/dist/index.d.mts +173 -1
- package/dist/index.d.ts +173 -1
- package/dist/index.js +537 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +531 -0
- package/dist/index.mjs.map +1 -1
- package/dist/memory/index.d.mts +368 -1
- package/dist/memory/index.d.ts +368 -1
- package/dist/memory/index.js +238 -0
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/index.mjs +237 -0
- package/dist/memory/index.mjs.map +1 -1
- package/dist/react/index.d.mts +254 -1
- package/dist/react/index.d.ts +254 -1
- package/dist/react/index.js +729 -51
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +729 -50
- package/dist/react/index.mjs.map +1 -1
- package/dist/voice/index.d.mts +130 -0
- package/dist/voice/index.d.ts +130 -0
- package/dist/voice/index.js +141 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/voice/index.mjs +116 -0
- package/dist/voice/index.mjs.map +1 -0
- package/dist/web/index.d.mts +169 -0
- package/dist/web/index.d.ts +169 -0
- package/dist/web/index.js +115 -0
- package/dist/web/index.js.map +1 -0
- package/dist/web/index.mjs +88 -0
- package/dist/web/index.mjs.map +1 -0
- package/package.json +12 -2
package/dist/react/index.mjs
CHANGED
|
@@ -1,6 +1,345 @@
|
|
|
1
|
+
// src/react/chat/chat.tsx
|
|
2
|
+
import {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState
|
|
8
|
+
} from "react";
|
|
9
|
+
import { Brain, Loader2, Send } from "lucide-react";
|
|
10
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
var DEFAULT_BASE_URL = "https://cencori.com";
|
|
12
|
+
function Chat({
|
|
13
|
+
model,
|
|
14
|
+
apiKey,
|
|
15
|
+
memory,
|
|
16
|
+
system,
|
|
17
|
+
initialMessages,
|
|
18
|
+
temperature,
|
|
19
|
+
maxTokens,
|
|
20
|
+
stream = true,
|
|
21
|
+
baseUrl = DEFAULT_BASE_URL,
|
|
22
|
+
placeholder = "Send a message\u2026",
|
|
23
|
+
onMemoryRetrieved,
|
|
24
|
+
onError,
|
|
25
|
+
emptyState,
|
|
26
|
+
className,
|
|
27
|
+
hideMemoryIndicator = false
|
|
28
|
+
}) {
|
|
29
|
+
const [messages, setMessages] = useState(initialMessages ?? []);
|
|
30
|
+
const [input, setInput] = useState("");
|
|
31
|
+
const [busy, setBusy] = useState(false);
|
|
32
|
+
const [error, setError] = useState(null);
|
|
33
|
+
const [recalled, setRecalled] = useState(null);
|
|
34
|
+
const scrollRef = useRef(null);
|
|
35
|
+
const endpoint = useMemo(
|
|
36
|
+
() => `${baseUrl.replace(/\/$/, "")}/api/v1/chat/completions`,
|
|
37
|
+
[baseUrl]
|
|
38
|
+
);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: "smooth" });
|
|
41
|
+
}, [messages, busy]);
|
|
42
|
+
const send = useCallback(
|
|
43
|
+
async (text) => {
|
|
44
|
+
const trimmed = text.trim();
|
|
45
|
+
if (!trimmed || busy) return;
|
|
46
|
+
setError(null);
|
|
47
|
+
setBusy(true);
|
|
48
|
+
const userMsg = { role: "user", content: trimmed };
|
|
49
|
+
const history = [...messages, userMsg];
|
|
50
|
+
setMessages(history);
|
|
51
|
+
setInput("");
|
|
52
|
+
const outbound = system ? [{ role: "system", content: system }, ...history] : history;
|
|
53
|
+
const body = {
|
|
54
|
+
model,
|
|
55
|
+
messages: outbound,
|
|
56
|
+
memory,
|
|
57
|
+
temperature,
|
|
58
|
+
max_tokens: maxTokens,
|
|
59
|
+
stream
|
|
60
|
+
};
|
|
61
|
+
const headers = { "Content-Type": "application/json" };
|
|
62
|
+
if (apiKey) headers["CENCORI_API_KEY"] = apiKey;
|
|
63
|
+
try {
|
|
64
|
+
const res = await fetch(endpoint, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers,
|
|
67
|
+
body: JSON.stringify(body)
|
|
68
|
+
});
|
|
69
|
+
if (!res.ok || stream && !res.body) {
|
|
70
|
+
const data = await res.json().catch(() => ({}));
|
|
71
|
+
const msg = typeof data.error === "object" ? data.error?.message : data.error;
|
|
72
|
+
throw new Error(msg || `Cencori API error (${res.status})`);
|
|
73
|
+
}
|
|
74
|
+
if (memory && !hideMemoryIndicator) {
|
|
75
|
+
const count = Number(res.headers.get("X-Cencori-Memory-Retrieved") || 0);
|
|
76
|
+
setRecalled(count);
|
|
77
|
+
onMemoryRetrieved?.(count);
|
|
78
|
+
}
|
|
79
|
+
if (stream && res.body) {
|
|
80
|
+
setMessages((prev) => [...prev, { role: "assistant", content: "" }]);
|
|
81
|
+
const reader = res.body.getReader();
|
|
82
|
+
const decoder = new TextDecoder();
|
|
83
|
+
let buffer = "";
|
|
84
|
+
for (; ; ) {
|
|
85
|
+
const { done, value } = await reader.read();
|
|
86
|
+
if (done) break;
|
|
87
|
+
buffer += decoder.decode(value, { stream: true });
|
|
88
|
+
const lines = buffer.split("\n");
|
|
89
|
+
buffer = lines.pop() ?? "";
|
|
90
|
+
for (const line of lines) {
|
|
91
|
+
const t = line.trim();
|
|
92
|
+
if (!t.startsWith("data: ")) continue;
|
|
93
|
+
const payload = t.slice(6);
|
|
94
|
+
if (payload === "[DONE]") break;
|
|
95
|
+
try {
|
|
96
|
+
const parsed = JSON.parse(payload);
|
|
97
|
+
const delta = parsed.choices?.[0]?.delta?.content ?? "";
|
|
98
|
+
if (!delta) continue;
|
|
99
|
+
setMessages((prev) => {
|
|
100
|
+
const next = [...prev];
|
|
101
|
+
const last = next[next.length - 1];
|
|
102
|
+
if (last?.role === "assistant") {
|
|
103
|
+
next[next.length - 1] = {
|
|
104
|
+
...last,
|
|
105
|
+
content: last.content + delta
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return next;
|
|
109
|
+
});
|
|
110
|
+
} catch {
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
const data = await res.json();
|
|
116
|
+
const reply = data.choices?.[0]?.message?.content ?? "";
|
|
117
|
+
setMessages((prev) => [...prev, { role: "assistant", content: reply }]);
|
|
118
|
+
}
|
|
119
|
+
} catch (err) {
|
|
120
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
121
|
+
setError(e.message);
|
|
122
|
+
onError?.(e);
|
|
123
|
+
setMessages(
|
|
124
|
+
(prev) => prev.filter((m, i) => !(i === prev.length - 1 && m.role === "user" && m.content === trimmed))
|
|
125
|
+
);
|
|
126
|
+
setInput(trimmed);
|
|
127
|
+
} finally {
|
|
128
|
+
setBusy(false);
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
[
|
|
132
|
+
apiKey,
|
|
133
|
+
busy,
|
|
134
|
+
endpoint,
|
|
135
|
+
hideMemoryIndicator,
|
|
136
|
+
maxTokens,
|
|
137
|
+
memory,
|
|
138
|
+
messages,
|
|
139
|
+
model,
|
|
140
|
+
onError,
|
|
141
|
+
onMemoryRetrieved,
|
|
142
|
+
stream,
|
|
143
|
+
system,
|
|
144
|
+
temperature
|
|
145
|
+
]
|
|
146
|
+
);
|
|
147
|
+
const onSubmit = (e) => {
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
void send(input);
|
|
150
|
+
};
|
|
151
|
+
const visible = messages.filter((m) => m.role !== "system");
|
|
152
|
+
return /* @__PURE__ */ jsxs(
|
|
153
|
+
"div",
|
|
154
|
+
{
|
|
155
|
+
className: `flex flex-col h-full min-h-[24rem] rounded-xl border border-neutral-200 bg-white dark:border-neutral-800 dark:bg-neutral-950 ${className ?? ""}`,
|
|
156
|
+
children: [
|
|
157
|
+
/* @__PURE__ */ jsxs("div", { ref: scrollRef, className: "flex-1 overflow-y-auto p-4 space-y-3", children: [
|
|
158
|
+
visible.length === 0 && /* @__PURE__ */ jsx("div", { className: "h-full flex items-center justify-center text-sm text-neutral-400", children: emptyState ?? "Start the conversation." }),
|
|
159
|
+
visible.map((m, i) => /* @__PURE__ */ jsx(
|
|
160
|
+
"div",
|
|
161
|
+
{
|
|
162
|
+
className: `flex ${m.role === "user" ? "justify-end" : "justify-start"}`,
|
|
163
|
+
children: /* @__PURE__ */ jsx(
|
|
164
|
+
"div",
|
|
165
|
+
{
|
|
166
|
+
className: `max-w-[85%] whitespace-pre-wrap rounded-2xl px-3.5 py-2 text-sm ${m.role === "user" ? "bg-neutral-900 text-white dark:bg-white dark:text-neutral-900" : "bg-neutral-100 text-neutral-900 dark:bg-neutral-900 dark:text-neutral-100"}`,
|
|
167
|
+
children: m.content || (busy && i === visible.length - 1 ? "\u2026" : "")
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
},
|
|
171
|
+
i
|
|
172
|
+
))
|
|
173
|
+
] }),
|
|
174
|
+
memory && !hideMemoryIndicator && recalled !== null && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 px-4 py-1.5 text-xs text-neutral-500 border-t border-neutral-100 dark:border-neutral-900", children: [
|
|
175
|
+
/* @__PURE__ */ jsx(Brain, { className: "h-3.5 w-3.5" }),
|
|
176
|
+
recalled > 0 ? `Recalled ${recalled} ${recalled === 1 ? "memory" : "memories"} about this user` : "No prior memories yet \u2014 learning"
|
|
177
|
+
] }),
|
|
178
|
+
error && /* @__PURE__ */ jsx("div", { className: "px-4 py-2 text-xs text-red-600 dark:text-red-400", children: error }),
|
|
179
|
+
/* @__PURE__ */ jsxs(
|
|
180
|
+
"form",
|
|
181
|
+
{
|
|
182
|
+
onSubmit,
|
|
183
|
+
className: "flex items-center gap-2 border-t border-neutral-200 p-3 dark:border-neutral-800",
|
|
184
|
+
children: [
|
|
185
|
+
/* @__PURE__ */ jsx(
|
|
186
|
+
"input",
|
|
187
|
+
{
|
|
188
|
+
value: input,
|
|
189
|
+
onChange: (e) => setInput(e.target.value),
|
|
190
|
+
placeholder,
|
|
191
|
+
disabled: busy,
|
|
192
|
+
className: "flex-1 rounded-lg bg-neutral-100 px-3 py-2 text-sm outline-none placeholder:text-neutral-400 disabled:opacity-60 dark:bg-neutral-900"
|
|
193
|
+
}
|
|
194
|
+
),
|
|
195
|
+
/* @__PURE__ */ jsx(
|
|
196
|
+
"button",
|
|
197
|
+
{
|
|
198
|
+
type: "submit",
|
|
199
|
+
disabled: busy || !input.trim(),
|
|
200
|
+
className: "inline-flex h-9 w-9 items-center justify-center rounded-lg bg-neutral-900 text-white transition disabled:opacity-40 dark:bg-white dark:text-neutral-900",
|
|
201
|
+
"aria-label": "Send",
|
|
202
|
+
children: busy ? /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin" }) : /* @__PURE__ */ jsx(Send, { className: "h-4 w-4" })
|
|
203
|
+
}
|
|
204
|
+
)
|
|
205
|
+
]
|
|
206
|
+
}
|
|
207
|
+
)
|
|
208
|
+
]
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/react/memory/use-memory.ts
|
|
214
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
|
|
215
|
+
var DEFAULT_BASE_URL2 = "https://cencori.com";
|
|
216
|
+
function useMemory(options) {
|
|
217
|
+
const {
|
|
218
|
+
userId,
|
|
219
|
+
sessionId,
|
|
220
|
+
scope = "user",
|
|
221
|
+
namespace,
|
|
222
|
+
apiKey,
|
|
223
|
+
baseUrl = DEFAULT_BASE_URL2,
|
|
224
|
+
limit = 50,
|
|
225
|
+
manual = false
|
|
226
|
+
} = options;
|
|
227
|
+
const [memories, setMemories] = useState2([]);
|
|
228
|
+
const [loading, setLoading] = useState2(!manual);
|
|
229
|
+
const [error, setError] = useState2(null);
|
|
230
|
+
const base = useMemo2(() => baseUrl.replace(/\/$/, ""), [baseUrl]);
|
|
231
|
+
const headers = useMemo2(() => {
|
|
232
|
+
const h = { "Content-Type": "application/json" };
|
|
233
|
+
if (apiKey) h["CENCORI_API_KEY"] = apiKey;
|
|
234
|
+
return h;
|
|
235
|
+
}, [apiKey]);
|
|
236
|
+
const scopeBody = useCallback2(
|
|
237
|
+
() => ({ userId, sessionId, scope, namespace }),
|
|
238
|
+
[userId, sessionId, scope, namespace]
|
|
239
|
+
);
|
|
240
|
+
const request = useCallback2(
|
|
241
|
+
async (path, init) => {
|
|
242
|
+
const res = await fetch(`${base}${path}`, { ...init, headers });
|
|
243
|
+
if (!res.ok) {
|
|
244
|
+
const data = await res.json().catch(() => ({}));
|
|
245
|
+
throw new Error(data.error?.message || data.message || `Request failed (${res.status})`);
|
|
246
|
+
}
|
|
247
|
+
return res.json();
|
|
248
|
+
},
|
|
249
|
+
[base, headers]
|
|
250
|
+
);
|
|
251
|
+
const refresh = useCallback2(async () => {
|
|
252
|
+
if (!userId && !sessionId) return;
|
|
253
|
+
setLoading(true);
|
|
254
|
+
setError(null);
|
|
255
|
+
try {
|
|
256
|
+
const params = new URLSearchParams();
|
|
257
|
+
if (userId) params.set("userId", userId);
|
|
258
|
+
if (sessionId) params.set("sessionId", sessionId);
|
|
259
|
+
params.set("scope", scope);
|
|
260
|
+
if (namespace) params.set("namespace", namespace);
|
|
261
|
+
params.set("limit", String(limit));
|
|
262
|
+
const data = await request(
|
|
263
|
+
`/v1/memory/list?${params.toString()}`,
|
|
264
|
+
{ method: "GET" }
|
|
265
|
+
);
|
|
266
|
+
setMemories(data.memories ?? []);
|
|
267
|
+
} catch (err) {
|
|
268
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
269
|
+
} finally {
|
|
270
|
+
setLoading(false);
|
|
271
|
+
}
|
|
272
|
+
}, [userId, sessionId, scope, namespace, limit, request]);
|
|
273
|
+
useEffect2(() => {
|
|
274
|
+
if (!manual) void refresh();
|
|
275
|
+
}, [manual, refresh]);
|
|
276
|
+
const write = useCallback2(
|
|
277
|
+
async (content, opts) => {
|
|
278
|
+
setError(null);
|
|
279
|
+
try {
|
|
280
|
+
await request("/v1/memory/write", {
|
|
281
|
+
method: "POST",
|
|
282
|
+
body: JSON.stringify({ ...scopeBody(), content, ...opts })
|
|
283
|
+
});
|
|
284
|
+
await refresh();
|
|
285
|
+
} catch (err) {
|
|
286
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
[request, scopeBody, refresh]
|
|
291
|
+
);
|
|
292
|
+
const search = useCallback2(
|
|
293
|
+
async (query, topK = 5) => {
|
|
294
|
+
const data = await request("/v1/memory/search", {
|
|
295
|
+
method: "POST",
|
|
296
|
+
body: JSON.stringify({ ...scopeBody(), query, topK })
|
|
297
|
+
});
|
|
298
|
+
return data.results ?? [];
|
|
299
|
+
},
|
|
300
|
+
[request, scopeBody]
|
|
301
|
+
);
|
|
302
|
+
const forget = useCallback2(
|
|
303
|
+
async (id) => {
|
|
304
|
+
setError(null);
|
|
305
|
+
setMemories((prev) => prev.filter((m) => m.id !== id));
|
|
306
|
+
try {
|
|
307
|
+
await request(`/v1/memory/${id}`, { method: "DELETE" });
|
|
308
|
+
} catch (err) {
|
|
309
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
310
|
+
await refresh();
|
|
311
|
+
throw err;
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
[request, refresh]
|
|
315
|
+
);
|
|
316
|
+
const exportAll = useCallback2(async () => {
|
|
317
|
+
const params = new URLSearchParams();
|
|
318
|
+
if (userId) params.set("userId", userId);
|
|
319
|
+
if (sessionId) params.set("sessionId", sessionId);
|
|
320
|
+
params.set("scope", scope);
|
|
321
|
+
if (namespace) params.set("namespace", namespace);
|
|
322
|
+
params.set("limit", "1000");
|
|
323
|
+
const data = await request(
|
|
324
|
+
`/v1/memory/list?${params.toString()}`,
|
|
325
|
+
{ method: "GET" }
|
|
326
|
+
);
|
|
327
|
+
const blob = new Blob([JSON.stringify(data.memories ?? [], null, 2)], {
|
|
328
|
+
type: "application/json"
|
|
329
|
+
});
|
|
330
|
+
const url = URL.createObjectURL(blob);
|
|
331
|
+
const a = document.createElement("a");
|
|
332
|
+
a.href = url;
|
|
333
|
+
a.download = `cencori-memory-${userId ?? sessionId ?? "export"}.json`;
|
|
334
|
+
a.click();
|
|
335
|
+
URL.revokeObjectURL(url);
|
|
336
|
+
}, [request, userId, sessionId, scope, namespace]);
|
|
337
|
+
return { memories, loading, error, refresh, write, search, forget, exportAll };
|
|
338
|
+
}
|
|
339
|
+
|
|
1
340
|
// src/react/vision/vision-format-banner.tsx
|
|
2
341
|
import { ImageIcon, Info } from "lucide-react";
|
|
3
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
342
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
4
343
|
var UNIVERSAL_FORMATS = ["JPEG", "PNG", "WEBP", "GIF"];
|
|
5
344
|
var PROVIDER_LIMITS = {
|
|
6
345
|
openai: { formats: ["JPEG", "PNG", "WEBP", "GIF"], maxMB: 20, notes: "Non-animated GIFs only." },
|
|
@@ -19,7 +358,7 @@ function VisionFormatBanner({
|
|
|
19
358
|
const info = provider ? PROVIDER_LIMITS[provider] : { formats: UNIVERSAL_FORMATS, maxMB: 5, notes: "These formats work across every supported provider." };
|
|
20
359
|
const heading = title ?? (provider ? `Supported for ${providerLabel(provider)}` : "Supported image formats");
|
|
21
360
|
const base = variant === "info" ? "bg-blue-50 border-blue-200 text-blue-900 dark:bg-blue-950/40 dark:border-blue-900 dark:text-blue-100" : "bg-neutral-50 border-neutral-200 text-neutral-800 dark:bg-neutral-900 dark:border-neutral-800 dark:text-neutral-100";
|
|
22
|
-
return /* @__PURE__ */
|
|
361
|
+
return /* @__PURE__ */ jsxs2(
|
|
23
362
|
"div",
|
|
24
363
|
{
|
|
25
364
|
role: "note",
|
|
@@ -29,23 +368,23 @@ function VisionFormatBanner({
|
|
|
29
368
|
className
|
|
30
369
|
),
|
|
31
370
|
children: [
|
|
32
|
-
/* @__PURE__ */
|
|
33
|
-
/* @__PURE__ */
|
|
34
|
-
/* @__PURE__ */
|
|
35
|
-
/* @__PURE__ */
|
|
371
|
+
/* @__PURE__ */ jsx2("div", { className: "flex-shrink-0 pt-0.5", children: /* @__PURE__ */ jsx2(Info, { className: "h-5 w-5 opacity-70", "aria-hidden": true }) }),
|
|
372
|
+
/* @__PURE__ */ jsxs2("div", { className: "flex flex-col gap-2", children: [
|
|
373
|
+
/* @__PURE__ */ jsx2("p", { className: "font-medium", children: heading }),
|
|
374
|
+
/* @__PURE__ */ jsx2("div", { className: "flex flex-wrap gap-1.5", children: info.formats.map((f) => /* @__PURE__ */ jsxs2(
|
|
36
375
|
"span",
|
|
37
376
|
{
|
|
38
377
|
className: "inline-flex items-center gap-1 rounded-md border border-current/20 bg-white/50 px-2 py-0.5 text-xs font-medium dark:bg-black/20",
|
|
39
378
|
children: [
|
|
40
|
-
/* @__PURE__ */
|
|
379
|
+
/* @__PURE__ */ jsx2(ImageIcon, { className: "h-3 w-3 opacity-70", "aria-hidden": true }),
|
|
41
380
|
f
|
|
42
381
|
]
|
|
43
382
|
},
|
|
44
383
|
f
|
|
45
384
|
)) }),
|
|
46
|
-
/* @__PURE__ */
|
|
385
|
+
/* @__PURE__ */ jsxs2("p", { className: "text-xs opacity-80", children: [
|
|
47
386
|
"Up to ",
|
|
48
|
-
/* @__PURE__ */
|
|
387
|
+
/* @__PURE__ */ jsxs2("strong", { children: [
|
|
49
388
|
info.maxMB,
|
|
50
389
|
"MB"
|
|
51
390
|
] }),
|
|
@@ -64,8 +403,8 @@ function providerLabel(p) {
|
|
|
64
403
|
}
|
|
65
404
|
|
|
66
405
|
// src/react/vision/vision-uploader.tsx
|
|
67
|
-
import { useCallback, useMemo, useRef, useState } from "react";
|
|
68
|
-
import { AlertCircle, Loader2, Upload, X } from "lucide-react";
|
|
406
|
+
import { useCallback as useCallback3, useMemo as useMemo3, useRef as useRef2, useState as useState3 } from "react";
|
|
407
|
+
import { AlertCircle, Loader2 as Loader22, Upload, X } from "lucide-react";
|
|
69
408
|
|
|
70
409
|
// src/react/vision/downscale.ts
|
|
71
410
|
var DOWNSCALABLE_TYPES = /* @__PURE__ */ new Set(["image/jpeg", "image/png", "image/webp"]);
|
|
@@ -126,7 +465,7 @@ function renameToJpeg(name) {
|
|
|
126
465
|
}
|
|
127
466
|
|
|
128
467
|
// src/react/vision/vision-uploader.tsx
|
|
129
|
-
import { Fragment, jsx as
|
|
468
|
+
import { Fragment, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
130
469
|
var UNIVERSAL_FORMATS2 = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
|
131
470
|
var PROVIDER_RULES = {
|
|
132
471
|
openai: { formats: UNIVERSAL_FORMATS2, maxBytes: 20 * 1024 * 1024 },
|
|
@@ -161,17 +500,17 @@ function VisionUploader({
|
|
|
161
500
|
headers,
|
|
162
501
|
className
|
|
163
502
|
}) {
|
|
164
|
-
const [file, setFile] =
|
|
165
|
-
const [preview, setPreview] =
|
|
166
|
-
const [error, setError] =
|
|
167
|
-
const [loading, setLoading] =
|
|
168
|
-
const [result, setResult] =
|
|
169
|
-
const [dragging, setDragging] =
|
|
170
|
-
const [notice, setNotice] =
|
|
171
|
-
const inputRef =
|
|
172
|
-
const rules =
|
|
503
|
+
const [file, setFile] = useState3(null);
|
|
504
|
+
const [preview, setPreview] = useState3(null);
|
|
505
|
+
const [error, setError] = useState3(null);
|
|
506
|
+
const [loading, setLoading] = useState3(false);
|
|
507
|
+
const [result, setResult] = useState3(null);
|
|
508
|
+
const [dragging, setDragging] = useState3(false);
|
|
509
|
+
const [notice, setNotice] = useState3(null);
|
|
510
|
+
const inputRef = useRef2(null);
|
|
511
|
+
const rules = useMemo3(() => rulesFor(provider), [provider]);
|
|
173
512
|
const targetUrl = task === "analyze" ? endpoint : `${endpoint.replace(/\/$/, "")}/${task}`;
|
|
174
|
-
const acceptFile =
|
|
513
|
+
const acceptFile = useCallback3(async (f) => {
|
|
175
514
|
setResult(null);
|
|
176
515
|
setNotice(null);
|
|
177
516
|
let effective = f;
|
|
@@ -222,13 +561,13 @@ function VisionUploader({
|
|
|
222
561
|
setFile(effective);
|
|
223
562
|
setPreview(URL.createObjectURL(effective));
|
|
224
563
|
}, [rules, provider, autoCompress, onError]);
|
|
225
|
-
const handleDrop =
|
|
564
|
+
const handleDrop = useCallback3((e) => {
|
|
226
565
|
e.preventDefault();
|
|
227
566
|
setDragging(false);
|
|
228
567
|
const f = e.dataTransfer.files?.[0];
|
|
229
568
|
if (f) acceptFile(f);
|
|
230
569
|
}, [acceptFile]);
|
|
231
|
-
const handleSubmit =
|
|
570
|
+
const handleSubmit = useCallback3(async () => {
|
|
232
571
|
if (!file) return;
|
|
233
572
|
setLoading(true);
|
|
234
573
|
setError(null);
|
|
@@ -265,7 +604,7 @@ function VisionUploader({
|
|
|
265
604
|
setLoading(false);
|
|
266
605
|
}
|
|
267
606
|
}, [file, prompt, model, task, targetUrl, apiKey, headers, onResult, onError]);
|
|
268
|
-
const clear =
|
|
607
|
+
const clear = useCallback3(() => {
|
|
269
608
|
setFile(null);
|
|
270
609
|
setPreview(null);
|
|
271
610
|
setResult(null);
|
|
@@ -273,9 +612,9 @@ function VisionUploader({
|
|
|
273
612
|
setNotice(null);
|
|
274
613
|
if (inputRef.current) inputRef.current.value = "";
|
|
275
614
|
}, []);
|
|
276
|
-
return /* @__PURE__ */
|
|
277
|
-
!hideBanner && /* @__PURE__ */
|
|
278
|
-
/* @__PURE__ */
|
|
615
|
+
return /* @__PURE__ */ jsxs3("div", { className: joinClasses2("flex flex-col gap-3", className), children: [
|
|
616
|
+
!hideBanner && /* @__PURE__ */ jsx3(VisionFormatBanner, { provider }),
|
|
617
|
+
/* @__PURE__ */ jsxs3(
|
|
279
618
|
"div",
|
|
280
619
|
{
|
|
281
620
|
onDragOver: (e) => {
|
|
@@ -293,9 +632,9 @@ function VisionUploader({
|
|
|
293
632
|
dragging ? "border-blue-500 bg-blue-50 dark:bg-blue-950/30" : "border-neutral-300 hover:border-neutral-400 dark:border-neutral-700 dark:hover:border-neutral-600"
|
|
294
633
|
),
|
|
295
634
|
children: [
|
|
296
|
-
preview ? /* @__PURE__ */
|
|
297
|
-
/* @__PURE__ */
|
|
298
|
-
/* @__PURE__ */
|
|
635
|
+
preview ? /* @__PURE__ */ jsxs3("div", { className: "relative", children: [
|
|
636
|
+
/* @__PURE__ */ jsx3("img", { src: preview, alt: "Preview", className: "max-h-64 rounded-lg object-contain" }),
|
|
637
|
+
/* @__PURE__ */ jsx3(
|
|
299
638
|
"button",
|
|
300
639
|
{
|
|
301
640
|
type: "button",
|
|
@@ -305,19 +644,19 @@ function VisionUploader({
|
|
|
305
644
|
},
|
|
306
645
|
className: "absolute -right-2 -top-2 rounded-full bg-white p-1 shadow ring-1 ring-neutral-200 hover:bg-neutral-100 dark:bg-neutral-900 dark:ring-neutral-700",
|
|
307
646
|
"aria-label": "Remove image",
|
|
308
|
-
children: /* @__PURE__ */
|
|
647
|
+
children: /* @__PURE__ */ jsx3(X, { className: "h-4 w-4" })
|
|
309
648
|
}
|
|
310
649
|
)
|
|
311
|
-
] }) : /* @__PURE__ */
|
|
312
|
-
/* @__PURE__ */
|
|
313
|
-
/* @__PURE__ */
|
|
314
|
-
/* @__PURE__ */
|
|
650
|
+
] }) : /* @__PURE__ */ jsxs3(Fragment, { children: [
|
|
651
|
+
/* @__PURE__ */ jsx3(Upload, { className: "h-8 w-8 text-neutral-400", "aria-hidden": true }),
|
|
652
|
+
/* @__PURE__ */ jsx3("p", { className: "text-sm font-medium", children: "Drop an image or click to upload" }),
|
|
653
|
+
/* @__PURE__ */ jsxs3("p", { className: "text-xs text-neutral-500", children: [
|
|
315
654
|
friendlyFormats(rules.formats),
|
|
316
655
|
" \xB7 up to ",
|
|
317
656
|
formatBytes(rules.maxBytes)
|
|
318
657
|
] })
|
|
319
658
|
] }),
|
|
320
|
-
/* @__PURE__ */
|
|
659
|
+
/* @__PURE__ */ jsx3(
|
|
321
660
|
"input",
|
|
322
661
|
{
|
|
323
662
|
ref: inputRef,
|
|
@@ -333,15 +672,15 @@ function VisionUploader({
|
|
|
333
672
|
]
|
|
334
673
|
}
|
|
335
674
|
),
|
|
336
|
-
error && /* @__PURE__ */
|
|
337
|
-
/* @__PURE__ */
|
|
338
|
-
/* @__PURE__ */
|
|
339
|
-
/* @__PURE__ */
|
|
340
|
-
/* @__PURE__ */
|
|
675
|
+
error && /* @__PURE__ */ jsxs3("div", { role: "alert", className: "flex items-start gap-2 rounded-lg border border-red-200 bg-red-50 p-3 text-sm text-red-900 dark:border-red-900 dark:bg-red-950/40 dark:text-red-100", children: [
|
|
676
|
+
/* @__PURE__ */ jsx3(AlertCircle, { className: "mt-0.5 h-4 w-4 flex-shrink-0", "aria-hidden": true }),
|
|
677
|
+
/* @__PURE__ */ jsxs3("div", { children: [
|
|
678
|
+
/* @__PURE__ */ jsx3("p", { className: "font-medium", children: "Can't process this image" }),
|
|
679
|
+
/* @__PURE__ */ jsx3("p", { className: "text-xs opacity-90", children: error.message })
|
|
341
680
|
] })
|
|
342
681
|
] }),
|
|
343
|
-
notice && !error && /* @__PURE__ */
|
|
344
|
-
file && !loading && !result && /* @__PURE__ */
|
|
682
|
+
notice && !error && /* @__PURE__ */ jsx3("p", { className: "text-xs text-neutral-500 dark:text-neutral-400", children: notice }),
|
|
683
|
+
file && !loading && !result && /* @__PURE__ */ jsx3(
|
|
345
684
|
"button",
|
|
346
685
|
{
|
|
347
686
|
type: "button",
|
|
@@ -350,18 +689,358 @@ function VisionUploader({
|
|
|
350
689
|
children: "Analyze image"
|
|
351
690
|
}
|
|
352
691
|
),
|
|
353
|
-
loading && /* @__PURE__ */
|
|
354
|
-
/* @__PURE__ */
|
|
692
|
+
loading && /* @__PURE__ */ jsxs3("div", { className: "inline-flex items-center gap-2 text-sm text-neutral-600 dark:text-neutral-400", children: [
|
|
693
|
+
/* @__PURE__ */ jsx3(Loader22, { className: "h-4 w-4 animate-spin", "aria-hidden": true }),
|
|
355
694
|
"Analyzing\u2026"
|
|
356
695
|
] }),
|
|
357
|
-
result !== null && /* @__PURE__ */
|
|
358
|
-
/* @__PURE__ */
|
|
359
|
-
/* @__PURE__ */
|
|
696
|
+
result !== null && /* @__PURE__ */ jsxs3("div", { className: "rounded-lg border border-neutral-200 bg-neutral-50 p-3 text-sm dark:border-neutral-800 dark:bg-neutral-900", children: [
|
|
697
|
+
/* @__PURE__ */ jsx3("p", { className: "mb-1 text-xs font-medium uppercase text-neutral-500", children: "Result" }),
|
|
698
|
+
/* @__PURE__ */ jsx3("pre", { className: "overflow-auto whitespace-pre-wrap break-words text-xs", children: typeof result === "string" ? result : JSON.stringify(result, null, 2) })
|
|
699
|
+
] })
|
|
700
|
+
] });
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// src/react/voice/voice-recorder.tsx
|
|
704
|
+
import { useCallback as useCallback5, useState as useState5 } from "react";
|
|
705
|
+
import { AlertCircle as AlertCircle2, Loader2 as Loader23, Mic, Square } from "lucide-react";
|
|
706
|
+
|
|
707
|
+
// src/react/voice/use-voice-recorder.ts
|
|
708
|
+
import { useCallback as useCallback4, useEffect as useEffect3, useRef as useRef3, useState as useState4 } from "react";
|
|
709
|
+
function pickMimeType() {
|
|
710
|
+
if (typeof MediaRecorder === "undefined") return void 0;
|
|
711
|
+
const candidates = ["audio/webm;codecs=opus", "audio/webm", "audio/mp4", "audio/ogg"];
|
|
712
|
+
return candidates.find((t) => MediaRecorder.isTypeSupported(t));
|
|
713
|
+
}
|
|
714
|
+
function useVoiceRecorder() {
|
|
715
|
+
const [state, setState] = useState4("idle");
|
|
716
|
+
const [seconds, setSeconds] = useState4(0);
|
|
717
|
+
const [mimeType, setMimeType] = useState4(null);
|
|
718
|
+
const [error, setError] = useState4(null);
|
|
719
|
+
const [blob, setBlob] = useState4(null);
|
|
720
|
+
const recorderRef = useRef3(null);
|
|
721
|
+
const streamRef = useRef3(null);
|
|
722
|
+
const chunksRef = useRef3([]);
|
|
723
|
+
const timerRef = useRef3(null);
|
|
724
|
+
const cleanupStream = useCallback4(() => {
|
|
725
|
+
if (timerRef.current) {
|
|
726
|
+
clearInterval(timerRef.current);
|
|
727
|
+
timerRef.current = null;
|
|
728
|
+
}
|
|
729
|
+
streamRef.current?.getTracks().forEach((t) => t.stop());
|
|
730
|
+
streamRef.current = null;
|
|
731
|
+
}, []);
|
|
732
|
+
useEffect3(() => cleanupStream, [cleanupStream]);
|
|
733
|
+
const start = useCallback4(async () => {
|
|
734
|
+
setError(null);
|
|
735
|
+
setBlob(null);
|
|
736
|
+
setSeconds(0);
|
|
737
|
+
if (typeof navigator === "undefined" || !navigator.mediaDevices?.getUserMedia) {
|
|
738
|
+
setState("error");
|
|
739
|
+
setError("Microphone recording is not supported in this browser.");
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
try {
|
|
743
|
+
setState("requesting");
|
|
744
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
745
|
+
streamRef.current = stream;
|
|
746
|
+
const type = pickMimeType();
|
|
747
|
+
const recorder = new MediaRecorder(stream, type ? { mimeType: type } : void 0);
|
|
748
|
+
chunksRef.current = [];
|
|
749
|
+
recorder.ondataavailable = (e) => {
|
|
750
|
+
if (e.data.size > 0) chunksRef.current.push(e.data);
|
|
751
|
+
};
|
|
752
|
+
recorder.start();
|
|
753
|
+
recorderRef.current = recorder;
|
|
754
|
+
setMimeType(recorder.mimeType || type || "audio/webm");
|
|
755
|
+
setState("recording");
|
|
756
|
+
timerRef.current = setInterval(() => setSeconds((s) => s + 1), 1e3);
|
|
757
|
+
} catch (e) {
|
|
758
|
+
cleanupStream();
|
|
759
|
+
setState("error");
|
|
760
|
+
setError(e instanceof Error ? e.message : "Could not access the microphone.");
|
|
761
|
+
}
|
|
762
|
+
}, [cleanupStream]);
|
|
763
|
+
const stop = useCallback4(() => {
|
|
764
|
+
const recorder = recorderRef.current;
|
|
765
|
+
if (!recorder || recorder.state === "inactive") {
|
|
766
|
+
return Promise.resolve(null);
|
|
767
|
+
}
|
|
768
|
+
return new Promise((resolve) => {
|
|
769
|
+
recorder.onstop = () => {
|
|
770
|
+
const type = recorder.mimeType || "audio/webm";
|
|
771
|
+
const recorded = new Blob(chunksRef.current, { type });
|
|
772
|
+
cleanupStream();
|
|
773
|
+
recorderRef.current = null;
|
|
774
|
+
setBlob(recorded);
|
|
775
|
+
setState("stopped");
|
|
776
|
+
resolve(recorded);
|
|
777
|
+
};
|
|
778
|
+
recorder.stop();
|
|
779
|
+
});
|
|
780
|
+
}, [cleanupStream]);
|
|
781
|
+
const reset = useCallback4(() => {
|
|
782
|
+
cleanupStream();
|
|
783
|
+
recorderRef.current = null;
|
|
784
|
+
chunksRef.current = [];
|
|
785
|
+
setBlob(null);
|
|
786
|
+
setSeconds(0);
|
|
787
|
+
setError(null);
|
|
788
|
+
setState("idle");
|
|
789
|
+
}, [cleanupStream]);
|
|
790
|
+
return { state, seconds, mimeType, error, blob, start, stop, reset };
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// src/react/voice/voice-recorder.tsx
|
|
794
|
+
import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
795
|
+
function joinClasses3(...parts) {
|
|
796
|
+
return parts.filter(Boolean).join(" ");
|
|
797
|
+
}
|
|
798
|
+
function formatClock(total) {
|
|
799
|
+
const m = Math.floor(total / 60);
|
|
800
|
+
const s = total % 60;
|
|
801
|
+
return `${m}:${String(s).padStart(2, "0")}`;
|
|
802
|
+
}
|
|
803
|
+
function VoiceRecorder({
|
|
804
|
+
endpoint = "/api/ai/audio/transcriptions",
|
|
805
|
+
apiKey,
|
|
806
|
+
model = "whisper-1",
|
|
807
|
+
language,
|
|
808
|
+
diarize = false,
|
|
809
|
+
onTranscript,
|
|
810
|
+
onError,
|
|
811
|
+
headers,
|
|
812
|
+
className
|
|
813
|
+
}) {
|
|
814
|
+
const rec = useVoiceRecorder();
|
|
815
|
+
const [uploading, setUploading] = useState5(false);
|
|
816
|
+
const [transcript, setTranscript] = useState5(null);
|
|
817
|
+
const [error, setError] = useState5(null);
|
|
818
|
+
const transcribe = useCallback5(async (blob) => {
|
|
819
|
+
setUploading(true);
|
|
820
|
+
setError(null);
|
|
821
|
+
try {
|
|
822
|
+
const form = new FormData();
|
|
823
|
+
const ext = (blob.type.split("/")[1] || "webm").split(";")[0];
|
|
824
|
+
form.append("file", blob, `recording.${ext}`);
|
|
825
|
+
form.append("model", model);
|
|
826
|
+
form.append("response_format", diarize ? "verbose_json" : "json");
|
|
827
|
+
if (language) form.append("language", language);
|
|
828
|
+
if (diarize) form.append("diarize", "true");
|
|
829
|
+
const res = await fetch(endpoint, {
|
|
830
|
+
method: "POST",
|
|
831
|
+
headers: { ...apiKey ? { Authorization: `Bearer ${apiKey}` } : {}, ...headers },
|
|
832
|
+
body: form
|
|
833
|
+
});
|
|
834
|
+
const data = await res.json().catch(() => ({}));
|
|
835
|
+
if (!res.ok) {
|
|
836
|
+
const err = {
|
|
837
|
+
code: typeof data.error === "string" ? data.error : "request_failed",
|
|
838
|
+
message: typeof data.message === "string" ? data.message : `Transcription failed (${res.status})`
|
|
839
|
+
};
|
|
840
|
+
setError(err);
|
|
841
|
+
onError?.(err);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
const full = data;
|
|
845
|
+
setTranscript(full);
|
|
846
|
+
onTranscript?.(full.text ?? "", full);
|
|
847
|
+
} catch (e) {
|
|
848
|
+
const err = { code: "network_error", message: e instanceof Error ? e.message : "Network error" };
|
|
849
|
+
setError(err);
|
|
850
|
+
onError?.(err);
|
|
851
|
+
} finally {
|
|
852
|
+
setUploading(false);
|
|
853
|
+
}
|
|
854
|
+
}, [endpoint, apiKey, model, language, diarize, headers, onTranscript, onError]);
|
|
855
|
+
const toggle = useCallback5(async () => {
|
|
856
|
+
if (rec.state === "recording") {
|
|
857
|
+
const blob = await rec.stop();
|
|
858
|
+
if (blob) await transcribe(blob);
|
|
859
|
+
} else {
|
|
860
|
+
setTranscript(null);
|
|
861
|
+
await rec.start();
|
|
862
|
+
}
|
|
863
|
+
}, [rec, transcribe]);
|
|
864
|
+
const recording = rec.state === "recording";
|
|
865
|
+
const busy = rec.state === "requesting" || uploading;
|
|
866
|
+
const activeError = error || (rec.error ? { code: "mic_error", message: rec.error } : null);
|
|
867
|
+
return /* @__PURE__ */ jsxs4("div", { className: joinClasses3("cencori-voice-recorder", className), style: { display: "flex", flexDirection: "column", gap: 12 }, children: [
|
|
868
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "flex", alignItems: "center", gap: 12 }, children: [
|
|
869
|
+
/* @__PURE__ */ jsx4(
|
|
870
|
+
"button",
|
|
871
|
+
{
|
|
872
|
+
type: "button",
|
|
873
|
+
onClick: toggle,
|
|
874
|
+
disabled: busy,
|
|
875
|
+
"aria-label": recording ? "Stop recording" : "Start recording",
|
|
876
|
+
className: joinClasses3("cencori-voice-btn", recording && "is-recording"),
|
|
877
|
+
style: {
|
|
878
|
+
display: "inline-flex",
|
|
879
|
+
alignItems: "center",
|
|
880
|
+
justifyContent: "center",
|
|
881
|
+
width: 56,
|
|
882
|
+
height: 56,
|
|
883
|
+
borderRadius: "9999px",
|
|
884
|
+
border: "none",
|
|
885
|
+
cursor: busy ? "default" : "pointer",
|
|
886
|
+
background: recording ? "#dc2626" : "#111827",
|
|
887
|
+
color: "#fff",
|
|
888
|
+
opacity: busy ? 0.6 : 1,
|
|
889
|
+
transition: "background 0.15s"
|
|
890
|
+
},
|
|
891
|
+
children: busy ? /* @__PURE__ */ jsx4(Loader23, { size: 22, className: "animate-spin" }) : recording ? /* @__PURE__ */ jsx4(Square, { size: 20 }) : /* @__PURE__ */ jsx4(Mic, { size: 22 })
|
|
892
|
+
}
|
|
893
|
+
),
|
|
894
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "flex", flexDirection: "column" }, children: [
|
|
895
|
+
/* @__PURE__ */ jsx4("span", { style: { fontVariantNumeric: "tabular-nums", fontWeight: 600 }, children: recording ? formatClock(rec.seconds) : uploading ? "Transcribing\u2026" : "Tap to record" }),
|
|
896
|
+
recording && /* @__PURE__ */ jsxs4("span", { style: { fontSize: 12, color: "#dc2626", display: "inline-flex", alignItems: "center", gap: 6 }, children: [
|
|
897
|
+
/* @__PURE__ */ jsx4("span", { style: { width: 8, height: 8, borderRadius: "9999px", background: "#dc2626", display: "inline-block" } }),
|
|
898
|
+
"recording"
|
|
899
|
+
] })
|
|
900
|
+
] })
|
|
901
|
+
] }),
|
|
902
|
+
activeError && /* @__PURE__ */ jsxs4("div", { role: "alert", style: { display: "flex", gap: 8, alignItems: "flex-start", color: "#b91c1c", fontSize: 13 }, children: [
|
|
903
|
+
/* @__PURE__ */ jsx4(AlertCircle2, { size: 16, style: { flexShrink: 0, marginTop: 2 } }),
|
|
904
|
+
/* @__PURE__ */ jsx4("span", { children: activeError.message })
|
|
905
|
+
] }),
|
|
906
|
+
transcript && /* @__PURE__ */ jsx4("div", { className: "cencori-voice-result", style: { fontSize: 14, lineHeight: 1.5 }, children: transcript.segments && transcript.segments.some((s) => s.speaker) ? transcript.segments.map((s, i) => /* @__PURE__ */ jsxs4("p", { style: { margin: "4px 0" }, children: [
|
|
907
|
+
s.speaker && /* @__PURE__ */ jsxs4("strong", { style: { marginRight: 6 }, children: [
|
|
908
|
+
s.speaker,
|
|
909
|
+
":"
|
|
910
|
+
] }),
|
|
911
|
+
s.text
|
|
912
|
+
] }, i)) : /* @__PURE__ */ jsx4("p", { style: { margin: 0 }, children: transcript.text }) })
|
|
913
|
+
] });
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// src/react/voice/speak-button.tsx
|
|
917
|
+
import { useCallback as useCallback6, useEffect as useEffect4, useRef as useRef4, useState as useState6 } from "react";
|
|
918
|
+
import { AlertCircle as AlertCircle3, Loader2 as Loader24, Pause, Volume2 } from "lucide-react";
|
|
919
|
+
import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
920
|
+
function joinClasses4(...parts) {
|
|
921
|
+
return parts.filter(Boolean).join(" ");
|
|
922
|
+
}
|
|
923
|
+
function SpeakButton({
|
|
924
|
+
text,
|
|
925
|
+
endpoint = "/api/ai/audio/speech",
|
|
926
|
+
apiKey,
|
|
927
|
+
model = "tts-1",
|
|
928
|
+
voice,
|
|
929
|
+
language,
|
|
930
|
+
label = "Listen",
|
|
931
|
+
headers,
|
|
932
|
+
onError,
|
|
933
|
+
className
|
|
934
|
+
}) {
|
|
935
|
+
const [state, setState] = useState6("idle");
|
|
936
|
+
const [error, setError] = useState6(null);
|
|
937
|
+
const audioRef = useRef4(null);
|
|
938
|
+
const urlRef = useRef4(null);
|
|
939
|
+
const cleanup = useCallback6(() => {
|
|
940
|
+
if (audioRef.current) {
|
|
941
|
+
audioRef.current.pause();
|
|
942
|
+
audioRef.current = null;
|
|
943
|
+
}
|
|
944
|
+
if (urlRef.current) {
|
|
945
|
+
URL.revokeObjectURL(urlRef.current);
|
|
946
|
+
urlRef.current = null;
|
|
947
|
+
}
|
|
948
|
+
}, []);
|
|
949
|
+
useEffect4(() => cleanup, [cleanup]);
|
|
950
|
+
const play = useCallback6(async () => {
|
|
951
|
+
if (state === "playing") {
|
|
952
|
+
cleanup();
|
|
953
|
+
setState("idle");
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
if (!text.trim()) return;
|
|
957
|
+
setState("loading");
|
|
958
|
+
setError(null);
|
|
959
|
+
try {
|
|
960
|
+
const res = await fetch(endpoint, {
|
|
961
|
+
method: "POST",
|
|
962
|
+
headers: {
|
|
963
|
+
"Content-Type": "application/json",
|
|
964
|
+
...apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
|
|
965
|
+
...headers
|
|
966
|
+
},
|
|
967
|
+
body: JSON.stringify({ input: text, model, voice, language })
|
|
968
|
+
});
|
|
969
|
+
if (!res.ok) {
|
|
970
|
+
const data = await res.json().catch(() => ({}));
|
|
971
|
+
const err = {
|
|
972
|
+
code: typeof data.error === "string" ? data.error : "request_failed",
|
|
973
|
+
message: typeof data.message === "string" ? data.message : `Speech failed (${res.status})`
|
|
974
|
+
};
|
|
975
|
+
setError(err);
|
|
976
|
+
onError?.(err);
|
|
977
|
+
setState("idle");
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
const blob = await res.blob();
|
|
981
|
+
cleanup();
|
|
982
|
+
const url = URL.createObjectURL(blob);
|
|
983
|
+
urlRef.current = url;
|
|
984
|
+
const audio = new Audio(url);
|
|
985
|
+
audioRef.current = audio;
|
|
986
|
+
audio.onended = () => {
|
|
987
|
+
setState("idle");
|
|
988
|
+
cleanup();
|
|
989
|
+
};
|
|
990
|
+
audio.onerror = () => {
|
|
991
|
+
setState("idle");
|
|
992
|
+
cleanup();
|
|
993
|
+
};
|
|
994
|
+
await audio.play();
|
|
995
|
+
setState("playing");
|
|
996
|
+
} catch (e) {
|
|
997
|
+
const err = { code: "network_error", message: e instanceof Error ? e.message : "Network error" };
|
|
998
|
+
setError(err);
|
|
999
|
+
onError?.(err);
|
|
1000
|
+
setState("idle");
|
|
1001
|
+
}
|
|
1002
|
+
}, [state, text, endpoint, apiKey, model, voice, language, headers, onError, cleanup]);
|
|
1003
|
+
return /* @__PURE__ */ jsxs5("span", { style: { display: "inline-flex", flexDirection: "column", gap: 4 }, children: [
|
|
1004
|
+
/* @__PURE__ */ jsxs5(
|
|
1005
|
+
"button",
|
|
1006
|
+
{
|
|
1007
|
+
type: "button",
|
|
1008
|
+
onClick: play,
|
|
1009
|
+
disabled: state === "loading" || !text.trim(),
|
|
1010
|
+
"aria-label": label ?? (state === "playing" ? "Pause" : "Listen"),
|
|
1011
|
+
className: joinClasses4("cencori-speak-btn", className),
|
|
1012
|
+
style: {
|
|
1013
|
+
display: "inline-flex",
|
|
1014
|
+
alignItems: "center",
|
|
1015
|
+
gap: 6,
|
|
1016
|
+
padding: label ? "6px 12px" : 8,
|
|
1017
|
+
borderRadius: 8,
|
|
1018
|
+
border: "1px solid #e5e7eb",
|
|
1019
|
+
background: "#fff",
|
|
1020
|
+
color: "#111827",
|
|
1021
|
+
cursor: state === "loading" ? "default" : "pointer",
|
|
1022
|
+
fontSize: 14,
|
|
1023
|
+
fontWeight: 500
|
|
1024
|
+
},
|
|
1025
|
+
children: [
|
|
1026
|
+
state === "loading" ? /* @__PURE__ */ jsx5(Loader24, { size: 16, className: "animate-spin" }) : state === "playing" ? /* @__PURE__ */ jsx5(Pause, { size: 16 }) : /* @__PURE__ */ jsx5(Volume2, { size: 16 }),
|
|
1027
|
+
label && /* @__PURE__ */ jsx5("span", { children: label })
|
|
1028
|
+
]
|
|
1029
|
+
}
|
|
1030
|
+
),
|
|
1031
|
+
error && /* @__PURE__ */ jsxs5("span", { role: "alert", style: { display: "inline-flex", gap: 4, alignItems: "center", color: "#b91c1c", fontSize: 12 }, children: [
|
|
1032
|
+
/* @__PURE__ */ jsx5(AlertCircle3, { size: 13 }),
|
|
1033
|
+
error.message
|
|
360
1034
|
] })
|
|
361
1035
|
] });
|
|
362
1036
|
}
|
|
363
1037
|
export {
|
|
1038
|
+
Chat,
|
|
1039
|
+
SpeakButton,
|
|
364
1040
|
VisionFormatBanner,
|
|
365
|
-
VisionUploader
|
|
1041
|
+
VisionUploader,
|
|
1042
|
+
VoiceRecorder,
|
|
1043
|
+
useMemory,
|
|
1044
|
+
useVoiceRecorder
|
|
366
1045
|
};
|
|
367
1046
|
//# sourceMappingURL=index.mjs.map
|