@plaisolutions/react 0.1.1 → 0.2.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 +14 -0
- package/dist/components/index.d.ts +175 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +896 -0
- package/dist/components/index.js.map +1 -0
- package/dist/index.d.ts +173 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +894 -2
- package/dist/index.js.map +1 -1
- package/dist/styles.css +2 -0
- package/package.json +12 -4
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/components/internal/join-classes.ts
|
|
5
|
+
function joinClasses(...classes) {
|
|
6
|
+
return classes.filter(Boolean).join(" ");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/components/tool-result-email-send-card.tsx
|
|
11
|
+
function formatStatus(status) {
|
|
12
|
+
if (status === "pending") return "pending";
|
|
13
|
+
if (status === "error") return "error";
|
|
14
|
+
return "completed";
|
|
15
|
+
}
|
|
16
|
+
function asObject(value) {
|
|
17
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function toStringOrNull(value) {
|
|
21
|
+
return typeof value === "string" && value.trim().length > 0 ? value : null;
|
|
22
|
+
}
|
|
23
|
+
function toNumberOrNull(value) {
|
|
24
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
25
|
+
}
|
|
26
|
+
function parseEmailSendInput(part) {
|
|
27
|
+
const input = asObject(part.input);
|
|
28
|
+
if (!input) return {
|
|
29
|
+
to: [],
|
|
30
|
+
subject: null,
|
|
31
|
+
text: null
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
to: Array.isArray(input.to) ? input.to.filter((item) => typeof item === "string") : [],
|
|
35
|
+
subject: toStringOrNull(input.subject),
|
|
36
|
+
text: toStringOrNull(input.text)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function parseEmailSendResult(part) {
|
|
40
|
+
const initial = {
|
|
41
|
+
status: null,
|
|
42
|
+
recipientsCount: null,
|
|
43
|
+
subject: null,
|
|
44
|
+
receiptProvider: null,
|
|
45
|
+
receiptMessageId: null,
|
|
46
|
+
receiptRawStatus: null,
|
|
47
|
+
raw: null
|
|
48
|
+
};
|
|
49
|
+
if (part.result === void 0 || part.result === null) return initial;
|
|
50
|
+
let resultObject = asObject(part.result);
|
|
51
|
+
let raw = null;
|
|
52
|
+
if (!resultObject && typeof part.result === "string") try {
|
|
53
|
+
resultObject = asObject(JSON.parse(part.result));
|
|
54
|
+
} catch {
|
|
55
|
+
raw = part.result;
|
|
56
|
+
}
|
|
57
|
+
if (!resultObject) return {
|
|
58
|
+
...initial,
|
|
59
|
+
raw
|
|
60
|
+
};
|
|
61
|
+
const receipt = asObject(resultObject.receipt);
|
|
62
|
+
return {
|
|
63
|
+
status: toStringOrNull(resultObject.status),
|
|
64
|
+
recipientsCount: toNumberOrNull(resultObject.recipients_count),
|
|
65
|
+
subject: toStringOrNull(resultObject.subject),
|
|
66
|
+
receiptProvider: receipt ? toStringOrNull(receipt.provider) : null,
|
|
67
|
+
receiptMessageId: receipt ? toStringOrNull(receipt.message_id) : null,
|
|
68
|
+
receiptRawStatus: receipt ? toStringOrNull(receipt.raw_status) : null,
|
|
69
|
+
raw
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function ToolResultEmailSendCard({ part, className }) {
|
|
73
|
+
const [isExpanded, setIsExpanded] = useState(false);
|
|
74
|
+
const emailInput = parseEmailSendInput(part);
|
|
75
|
+
const emailResult = parseEmailSendResult(part);
|
|
76
|
+
const statusClass = part.state === "error" ? "border-rose-200 bg-rose-50 text-rose-800" : part.state === "pending" ? "border-amber-200 bg-amber-50 text-amber-800" : "border-emerald-200 bg-emerald-50 text-emerald-800";
|
|
77
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
78
|
+
className: joinClasses("space-y-4 rounded-lg border border-slate-200 bg-slate-50 p-3", className),
|
|
79
|
+
"data-tool-call-id": part.id,
|
|
80
|
+
"data-tool-call-state": part.state,
|
|
81
|
+
children: [
|
|
82
|
+
/* @__PURE__ */ jsxs("header", {
|
|
83
|
+
className: "flex items-center justify-between gap-2",
|
|
84
|
+
children: [/* @__PURE__ */ jsx("h4", {
|
|
85
|
+
className: "text-sm font-semibold text-slate-900",
|
|
86
|
+
children: "Email"
|
|
87
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
88
|
+
className: "flex items-center gap-2",
|
|
89
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
90
|
+
className: joinClasses("rounded-full border px-2 py-0.5 text-xs font-medium", statusClass),
|
|
91
|
+
children: formatStatus(part.state)
|
|
92
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
93
|
+
type: "button",
|
|
94
|
+
"aria-expanded": isExpanded,
|
|
95
|
+
"aria-controls": `tool-email-details-${part.id}`,
|
|
96
|
+
className: "inline-flex h-6 w-6 items-center justify-center rounded border border-slate-300 bg-white text-xs text-slate-700 hover:bg-slate-100",
|
|
97
|
+
onClick: () => setIsExpanded((previous) => !previous),
|
|
98
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
99
|
+
className: "inline-block",
|
|
100
|
+
children: isExpanded ? "^" : "v"
|
|
101
|
+
})
|
|
102
|
+
})]
|
|
103
|
+
})]
|
|
104
|
+
}),
|
|
105
|
+
emailInput.subject ? /* @__PURE__ */ jsxs("div", {
|
|
106
|
+
className: "space-y-1",
|
|
107
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
108
|
+
className: "text-xs font-semibold text-slate-600",
|
|
109
|
+
children: "Subject"
|
|
110
|
+
}), /* @__PURE__ */ jsx("p", {
|
|
111
|
+
className: "text-sm text-slate-900",
|
|
112
|
+
children: emailInput.subject
|
|
113
|
+
})]
|
|
114
|
+
}) : null,
|
|
115
|
+
/* @__PURE__ */ jsxs("div", {
|
|
116
|
+
className: "space-y-1",
|
|
117
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
118
|
+
className: "text-xs font-semibold text-slate-600",
|
|
119
|
+
children: "To"
|
|
120
|
+
}), emailInput.to.length > 0 ? /* @__PURE__ */ jsx("div", {
|
|
121
|
+
className: "flex flex-wrap gap-2",
|
|
122
|
+
children: emailInput.to.map((recipient) => /* @__PURE__ */ jsx("span", {
|
|
123
|
+
className: "rounded-full border border-slate-300 bg-slate-100 px-2 py-0.5 text-xs text-slate-700",
|
|
124
|
+
children: recipient
|
|
125
|
+
}, recipient))
|
|
126
|
+
}) : /* @__PURE__ */ jsx("p", {
|
|
127
|
+
className: "text-sm text-slate-500",
|
|
128
|
+
children: "No recipients provided."
|
|
129
|
+
})]
|
|
130
|
+
}),
|
|
131
|
+
/* @__PURE__ */ jsxs("div", {
|
|
132
|
+
id: `tool-email-details-${part.id}`,
|
|
133
|
+
"data-testid": "email-send-details",
|
|
134
|
+
className: joinClasses(isExpanded ? "block space-y-4" : "hidden"),
|
|
135
|
+
children: [emailInput.text ? /* @__PURE__ */ jsxs("div", {
|
|
136
|
+
className: "space-y-1",
|
|
137
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
138
|
+
className: "text-xs font-semibold text-slate-600",
|
|
139
|
+
children: "Body"
|
|
140
|
+
}), /* @__PURE__ */ jsx("p", {
|
|
141
|
+
className: "whitespace-pre-wrap rounded border border-slate-200 bg-slate-50 p-2 text-sm text-slate-700",
|
|
142
|
+
children: emailInput.text
|
|
143
|
+
})]
|
|
144
|
+
}) : null, part.result !== void 0 ? /* @__PURE__ */ jsxs("div", {
|
|
145
|
+
className: "space-y-2 rounded border border-slate-200 bg-white p-3",
|
|
146
|
+
children: [
|
|
147
|
+
/* @__PURE__ */ jsx("p", {
|
|
148
|
+
className: "text-xs font-semibold uppercase tracking-wide text-slate-500",
|
|
149
|
+
children: "Delivery"
|
|
150
|
+
}),
|
|
151
|
+
emailResult.status ? /* @__PURE__ */ jsxs("p", {
|
|
152
|
+
className: "text-sm text-slate-700",
|
|
153
|
+
children: [
|
|
154
|
+
/* @__PURE__ */ jsx("span", {
|
|
155
|
+
className: "font-semibold text-slate-900",
|
|
156
|
+
children: "Status:"
|
|
157
|
+
}),
|
|
158
|
+
" ",
|
|
159
|
+
emailResult.status
|
|
160
|
+
]
|
|
161
|
+
}) : null,
|
|
162
|
+
emailResult.recipientsCount !== null ? /* @__PURE__ */ jsxs("p", {
|
|
163
|
+
className: "text-sm text-slate-700",
|
|
164
|
+
children: [
|
|
165
|
+
/* @__PURE__ */ jsx("span", {
|
|
166
|
+
className: "font-semibold text-slate-900",
|
|
167
|
+
children: "Recipients:"
|
|
168
|
+
}),
|
|
169
|
+
" ",
|
|
170
|
+
emailResult.recipientsCount
|
|
171
|
+
]
|
|
172
|
+
}) : null,
|
|
173
|
+
emailResult.receiptProvider ? /* @__PURE__ */ jsxs("p", {
|
|
174
|
+
className: "text-sm text-slate-700",
|
|
175
|
+
children: [
|
|
176
|
+
/* @__PURE__ */ jsx("span", {
|
|
177
|
+
className: "font-semibold text-slate-900",
|
|
178
|
+
children: "Provider:"
|
|
179
|
+
}),
|
|
180
|
+
" ",
|
|
181
|
+
emailResult.receiptProvider
|
|
182
|
+
]
|
|
183
|
+
}) : null,
|
|
184
|
+
emailResult.receiptMessageId ? /* @__PURE__ */ jsxs("p", {
|
|
185
|
+
className: "break-all text-sm text-slate-700",
|
|
186
|
+
children: [
|
|
187
|
+
/* @__PURE__ */ jsx("span", {
|
|
188
|
+
className: "font-semibold text-slate-900",
|
|
189
|
+
children: "Message ID:"
|
|
190
|
+
}),
|
|
191
|
+
" ",
|
|
192
|
+
emailResult.receiptMessageId
|
|
193
|
+
]
|
|
194
|
+
}) : null,
|
|
195
|
+
emailResult.receiptRawStatus ? /* @__PURE__ */ jsxs("p", {
|
|
196
|
+
className: "text-sm text-slate-700",
|
|
197
|
+
children: [
|
|
198
|
+
/* @__PURE__ */ jsx("span", {
|
|
199
|
+
className: "font-semibold text-slate-900",
|
|
200
|
+
children: "Raw status:"
|
|
201
|
+
}),
|
|
202
|
+
" ",
|
|
203
|
+
emailResult.receiptRawStatus
|
|
204
|
+
]
|
|
205
|
+
}) : null,
|
|
206
|
+
emailResult.raw ? /* @__PURE__ */ jsx("p", {
|
|
207
|
+
className: "whitespace-pre-wrap text-sm text-slate-700",
|
|
208
|
+
children: emailResult.raw
|
|
209
|
+
}) : null
|
|
210
|
+
]
|
|
211
|
+
}) : null]
|
|
212
|
+
}),
|
|
213
|
+
part.errorDetails ? /* @__PURE__ */ jsxs("p", {
|
|
214
|
+
className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
|
|
215
|
+
children: [
|
|
216
|
+
/* @__PURE__ */ jsx("span", {
|
|
217
|
+
className: "font-semibold",
|
|
218
|
+
children: "Error details:"
|
|
219
|
+
}),
|
|
220
|
+
" ",
|
|
221
|
+
part.errorDetails
|
|
222
|
+
]
|
|
223
|
+
}) : null
|
|
224
|
+
]
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/components/tool-result-card.tsx
|
|
230
|
+
function formatJson(value) {
|
|
231
|
+
return JSON.stringify(value, null, 2);
|
|
232
|
+
}
|
|
233
|
+
function getOfficeDocumentMediaFiles(part) {
|
|
234
|
+
if (part.toolType !== "office_documents") return [];
|
|
235
|
+
if (!part.metadata || typeof part.metadata !== "object") return [];
|
|
236
|
+
const metadata = part.metadata;
|
|
237
|
+
if (!Array.isArray(metadata.media_files)) return [];
|
|
238
|
+
return metadata.media_files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string");
|
|
239
|
+
}
|
|
240
|
+
function ToolResultCard({ part, className, detailsOpen = true }) {
|
|
241
|
+
const officeMediaFiles = getOfficeDocumentMediaFiles(part);
|
|
242
|
+
if (part.toolType === "email_send") return /* @__PURE__ */ jsx(ToolResultEmailSendCard, {
|
|
243
|
+
part,
|
|
244
|
+
className
|
|
245
|
+
});
|
|
246
|
+
const statusClass = part.state === "error" ? "border-rose-200 bg-rose-50 text-rose-800" : part.state === "pending" ? "border-amber-200 bg-amber-50 text-amber-800" : "border-emerald-200 bg-emerald-50 text-emerald-800";
|
|
247
|
+
const statusLabel = part.state === "pending" ? "pending" : part.state === "error" ? "error" : "completed";
|
|
248
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
249
|
+
className: joinClasses("space-y-3 rounded-lg border border-slate-200 bg-slate-50 p-3", className),
|
|
250
|
+
"data-tool-call-id": part.id,
|
|
251
|
+
"data-tool-call-state": part.state,
|
|
252
|
+
children: [
|
|
253
|
+
/* @__PURE__ */ jsxs("header", {
|
|
254
|
+
className: "flex items-center justify-between gap-2",
|
|
255
|
+
children: [/* @__PURE__ */ jsx("h4", {
|
|
256
|
+
className: "text-sm font-semibold text-slate-900",
|
|
257
|
+
children: part.name
|
|
258
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
259
|
+
className: joinClasses("rounded-full border px-2 py-0.5 text-xs font-medium", statusClass),
|
|
260
|
+
children: statusLabel
|
|
261
|
+
})]
|
|
262
|
+
}),
|
|
263
|
+
part.toolType ? /* @__PURE__ */ jsxs("p", {
|
|
264
|
+
className: "text-xs text-slate-600",
|
|
265
|
+
children: [
|
|
266
|
+
/* @__PURE__ */ jsx("span", {
|
|
267
|
+
className: "font-semibold",
|
|
268
|
+
children: "Type:"
|
|
269
|
+
}),
|
|
270
|
+
" ",
|
|
271
|
+
part.toolType
|
|
272
|
+
]
|
|
273
|
+
}) : null,
|
|
274
|
+
/* @__PURE__ */ jsxs("details", {
|
|
275
|
+
open: detailsOpen,
|
|
276
|
+
className: "space-y-2",
|
|
277
|
+
children: [/* @__PURE__ */ jsx("summary", {
|
|
278
|
+
className: "cursor-pointer text-xs font-semibold text-slate-600",
|
|
279
|
+
children: "Input"
|
|
280
|
+
}), /* @__PURE__ */ jsx("pre", {
|
|
281
|
+
className: "overflow-x-auto rounded bg-slate-900 p-2 text-xs text-slate-100",
|
|
282
|
+
children: formatJson(part.input)
|
|
283
|
+
})]
|
|
284
|
+
}),
|
|
285
|
+
part.result !== void 0 ? /* @__PURE__ */ jsxs("details", {
|
|
286
|
+
open: detailsOpen,
|
|
287
|
+
className: "space-y-2",
|
|
288
|
+
children: [
|
|
289
|
+
/* @__PURE__ */ jsx("summary", {
|
|
290
|
+
className: "cursor-pointer text-xs font-semibold text-slate-600",
|
|
291
|
+
children: "Output"
|
|
292
|
+
}),
|
|
293
|
+
officeMediaFiles.length > 0 ? /* @__PURE__ */ jsxs("div", {
|
|
294
|
+
className: "space-y-2 rounded border border-slate-200 bg-white p-3",
|
|
295
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
296
|
+
className: "text-xs font-semibold text-slate-700",
|
|
297
|
+
children: "Generated files"
|
|
298
|
+
}), /* @__PURE__ */ jsx("ul", {
|
|
299
|
+
className: "space-y-1",
|
|
300
|
+
children: officeMediaFiles.map((file) => {
|
|
301
|
+
const label = file.name || file.pathname || file.id;
|
|
302
|
+
return /* @__PURE__ */ jsx("li", {
|
|
303
|
+
className: "text-xs text-slate-700",
|
|
304
|
+
children: file.url ? /* @__PURE__ */ jsx("a", {
|
|
305
|
+
href: file.url,
|
|
306
|
+
target: "_blank",
|
|
307
|
+
rel: "noreferrer noopener",
|
|
308
|
+
className: "hover:underline",
|
|
309
|
+
children: label
|
|
310
|
+
}) : /* @__PURE__ */ jsx("span", { children: label })
|
|
311
|
+
}, file.id);
|
|
312
|
+
})
|
|
313
|
+
})]
|
|
314
|
+
}) : null,
|
|
315
|
+
/* @__PURE__ */ jsx("pre", {
|
|
316
|
+
className: "overflow-x-auto rounded bg-slate-900 p-2 text-xs text-slate-100",
|
|
317
|
+
children: formatJson(part.result)
|
|
318
|
+
})
|
|
319
|
+
]
|
|
320
|
+
}) : null,
|
|
321
|
+
part.errorDetails ? /* @__PURE__ */ jsxs("p", {
|
|
322
|
+
className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
|
|
323
|
+
children: [
|
|
324
|
+
/* @__PURE__ */ jsx("span", {
|
|
325
|
+
className: "font-semibold",
|
|
326
|
+
children: "Error details:"
|
|
327
|
+
}),
|
|
328
|
+
" ",
|
|
329
|
+
part.errorDetails
|
|
330
|
+
]
|
|
331
|
+
}) : null
|
|
332
|
+
]
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/components/message.tsx
|
|
338
|
+
const DEFAULT_COLLAPSE_THRESHOLD = 600;
|
|
339
|
+
function getFilenameFromUrl(url) {
|
|
340
|
+
try {
|
|
341
|
+
const parsed = new URL(url);
|
|
342
|
+
return decodeURIComponent(parsed.pathname).split("/").pop()?.trim() || void 0;
|
|
343
|
+
} catch {
|
|
344
|
+
return (url.split("?")[0]?.split("#")[0] ?? "").split("/").pop()?.trim() || void 0;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
function getDisplayExtension(label) {
|
|
348
|
+
if (!label) return;
|
|
349
|
+
const ext = label.split(".").pop()?.trim().toUpperCase();
|
|
350
|
+
if (!ext || ext === label.toUpperCase()) return;
|
|
351
|
+
return ext;
|
|
352
|
+
}
|
|
353
|
+
function renderPart(part, index) {
|
|
354
|
+
if (part.type === "text") return /* @__PURE__ */ jsx("p", {
|
|
355
|
+
className: "whitespace-pre-wrap text-sm leading-6",
|
|
356
|
+
children: part.text
|
|
357
|
+
}, `text-${index}`);
|
|
358
|
+
if (part.type === "input_image") {
|
|
359
|
+
const title = part.metadata?.originalFileName || part.title || getFilenameFromUrl(part.url);
|
|
360
|
+
return /* @__PURE__ */ jsxs("a", {
|
|
361
|
+
href: part.url,
|
|
362
|
+
target: "_blank",
|
|
363
|
+
rel: "noreferrer noopener",
|
|
364
|
+
className: "block w-fit rounded-lg border border-slate-200 bg-white p-2",
|
|
365
|
+
children: [/* @__PURE__ */ jsx("img", {
|
|
366
|
+
src: part.url,
|
|
367
|
+
alt: title || "Uploaded image",
|
|
368
|
+
className: "max-h-56 rounded-md object-contain"
|
|
369
|
+
}), title ? /* @__PURE__ */ jsx("p", {
|
|
370
|
+
className: "mt-2 max-w-xs truncate text-xs font-medium text-slate-700",
|
|
371
|
+
children: title
|
|
372
|
+
}) : null]
|
|
373
|
+
}, `image-${index}`);
|
|
374
|
+
}
|
|
375
|
+
if (part.type === "input_file") {
|
|
376
|
+
const fileName = part.metadata?.originalFileName || part.title || getFilenameFromUrl(part.fileUrl) || "Document";
|
|
377
|
+
const extension = getDisplayExtension(fileName);
|
|
378
|
+
return /* @__PURE__ */ jsxs("a", {
|
|
379
|
+
href: part.fileUrl,
|
|
380
|
+
target: "_blank",
|
|
381
|
+
rel: "noreferrer noopener",
|
|
382
|
+
className: "flex w-fit items-center gap-3 rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-left",
|
|
383
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
384
|
+
className: "rounded bg-slate-900 px-2 py-1 text-[10px] font-semibold uppercase tracking-wide text-white",
|
|
385
|
+
children: extension ?? "File"
|
|
386
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
387
|
+
className: "min-w-0",
|
|
388
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
389
|
+
className: "max-w-xs truncate text-sm font-medium text-slate-900",
|
|
390
|
+
children: fileName
|
|
391
|
+
}), part.mimeType ? /* @__PURE__ */ jsx("p", {
|
|
392
|
+
className: "text-xs text-slate-500",
|
|
393
|
+
children: part.mimeType
|
|
394
|
+
}) : null]
|
|
395
|
+
})]
|
|
396
|
+
}, `file-${index}`);
|
|
397
|
+
}
|
|
398
|
+
if (part.type === "tool-call") return /* @__PURE__ */ jsx(ToolResultCard, { part }, `tool-${part.id}-${index}`);
|
|
399
|
+
if (part.type === "guardrail") return /* @__PURE__ */ jsx("p", {
|
|
400
|
+
className: "rounded-lg border border-amber-300 bg-amber-50 px-3 py-2 text-sm text-amber-900",
|
|
401
|
+
children: part.content
|
|
402
|
+
}, `guardrail-${index}`);
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
function Message({ message, className, showRole = true, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD }) {
|
|
406
|
+
const roleLabel = message.role.charAt(0).toUpperCase() + message.role.slice(1);
|
|
407
|
+
const [isExpanded, setIsExpanded] = useState(false);
|
|
408
|
+
const textContent = useMemo(() => {
|
|
409
|
+
return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
410
|
+
}, [message.parts]);
|
|
411
|
+
const canCollapse = message.role === "user" && message.parts.every((part) => part.type === "text") && textContent.length > collapseThreshold;
|
|
412
|
+
const previewText = canCollapse ? `${textContent.slice(0, collapseThreshold).trimEnd()}...` : textContent;
|
|
413
|
+
return /* @__PURE__ */ jsxs("article", {
|
|
414
|
+
className: joinClasses("space-y-3 rounded-xl border border-slate-200 bg-white p-4 shadow-sm", className),
|
|
415
|
+
"data-message-role": message.role,
|
|
416
|
+
children: [
|
|
417
|
+
showRole ? /* @__PURE__ */ jsx("header", {
|
|
418
|
+
className: "text-xs font-semibold uppercase tracking-wide text-slate-500",
|
|
419
|
+
children: roleLabel
|
|
420
|
+
}) : null,
|
|
421
|
+
/* @__PURE__ */ jsx("div", {
|
|
422
|
+
className: "space-y-2",
|
|
423
|
+
children: canCollapse && !isExpanded ? /* @__PURE__ */ jsx("p", {
|
|
424
|
+
className: "whitespace-pre-wrap text-sm leading-6",
|
|
425
|
+
children: previewText
|
|
426
|
+
}) : message.parts.map((part, index) => renderPart(part, index))
|
|
427
|
+
}),
|
|
428
|
+
canCollapse ? /* @__PURE__ */ jsx("button", {
|
|
429
|
+
type: "button",
|
|
430
|
+
className: "text-left text-xs font-medium text-slate-600 hover:text-slate-900",
|
|
431
|
+
onClick: () => setIsExpanded((previous) => !previous),
|
|
432
|
+
children: isExpanded ? "Read less" : "Read more"
|
|
433
|
+
}) : null
|
|
434
|
+
]
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
//#endregion
|
|
439
|
+
//#region src/components/icons/arrow.tsx
|
|
440
|
+
function ArrowUp(props) {
|
|
441
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
442
|
+
...props,
|
|
443
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
444
|
+
width: "24",
|
|
445
|
+
height: "24",
|
|
446
|
+
viewBox: "0 0 24 24",
|
|
447
|
+
fill: "none",
|
|
448
|
+
stroke: "currentColor",
|
|
449
|
+
strokeWidth: "2",
|
|
450
|
+
strokeLinecap: "round",
|
|
451
|
+
strokeLinejoin: "round",
|
|
452
|
+
"aria-hidden": "true",
|
|
453
|
+
children: [/* @__PURE__ */ jsx("path", { d: "m5 12 7-7 7 7" }), /* @__PURE__ */ jsx("path", { d: "M12 19V5" })]
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
//#endregion
|
|
458
|
+
//#region src/components/icons/loader.tsx
|
|
459
|
+
function Loader(props) {
|
|
460
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
461
|
+
...props,
|
|
462
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
463
|
+
width: "24",
|
|
464
|
+
height: "24",
|
|
465
|
+
viewBox: "0 0 24 24",
|
|
466
|
+
fill: "none",
|
|
467
|
+
stroke: "currentColor",
|
|
468
|
+
strokeWidth: "2",
|
|
469
|
+
strokeLinecap: "round",
|
|
470
|
+
strokeLinejoin: "round",
|
|
471
|
+
"aria-hidden": "true",
|
|
472
|
+
children: /* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/components/icons/microphone.tsx
|
|
478
|
+
function Microphone(props) {
|
|
479
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
480
|
+
...props,
|
|
481
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
482
|
+
width: "24",
|
|
483
|
+
height: "24",
|
|
484
|
+
viewBox: "0 0 24 24",
|
|
485
|
+
fill: "none",
|
|
486
|
+
stroke: "currentColor",
|
|
487
|
+
strokeWidth: "2",
|
|
488
|
+
strokeLinecap: "round",
|
|
489
|
+
strokeLinejoin: "round",
|
|
490
|
+
"aria-hidden": "true",
|
|
491
|
+
children: [
|
|
492
|
+
/* @__PURE__ */ jsx("path", { d: "M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z" }),
|
|
493
|
+
/* @__PURE__ */ jsx("path", { d: "M19 10v2a7 7 0 0 1-14 0v-2" }),
|
|
494
|
+
/* @__PURE__ */ jsx("path", { d: "M12 19v3" })
|
|
495
|
+
]
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
//#endregion
|
|
500
|
+
//#region src/components/icons/paperclip.tsx
|
|
501
|
+
function Paperclip(props) {
|
|
502
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
503
|
+
...props,
|
|
504
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
505
|
+
width: "24",
|
|
506
|
+
height: "24",
|
|
507
|
+
viewBox: "0 0 24 24",
|
|
508
|
+
fill: "none",
|
|
509
|
+
stroke: "currentColor",
|
|
510
|
+
strokeWidth: "2",
|
|
511
|
+
strokeLinecap: "round",
|
|
512
|
+
strokeLinejoin: "round",
|
|
513
|
+
"aria-hidden": "true",
|
|
514
|
+
children: /* @__PURE__ */ jsx("path", { d: "m21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48" })
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
//#endregion
|
|
519
|
+
//#region src/components/icons/stop.tsx
|
|
520
|
+
function Stop(props) {
|
|
521
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
522
|
+
...props,
|
|
523
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
524
|
+
width: "24",
|
|
525
|
+
height: "24",
|
|
526
|
+
viewBox: "0 0 24 24",
|
|
527
|
+
fill: "none",
|
|
528
|
+
stroke: "currentColor",
|
|
529
|
+
strokeWidth: "2",
|
|
530
|
+
strokeLinecap: "round",
|
|
531
|
+
strokeLinejoin: "round",
|
|
532
|
+
"aria-hidden": "true",
|
|
533
|
+
children: /* @__PURE__ */ jsx("rect", {
|
|
534
|
+
x: "6",
|
|
535
|
+
y: "6",
|
|
536
|
+
width: "12",
|
|
537
|
+
height: "12",
|
|
538
|
+
rx: "1"
|
|
539
|
+
})
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
//#endregion
|
|
544
|
+
//#region src/components/icons/x.tsx
|
|
545
|
+
function X(props) {
|
|
546
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
547
|
+
...props,
|
|
548
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
549
|
+
width: "24",
|
|
550
|
+
height: "24",
|
|
551
|
+
viewBox: "0 0 24 24",
|
|
552
|
+
fill: "none",
|
|
553
|
+
stroke: "currentColor",
|
|
554
|
+
strokeWidth: "2",
|
|
555
|
+
strokeLinecap: "round",
|
|
556
|
+
strokeLinejoin: "round",
|
|
557
|
+
"aria-hidden": "true",
|
|
558
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M18 6 6 18" }), /* @__PURE__ */ jsx("path", { d: "m6 6 12 12" })]
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
//#endregion
|
|
563
|
+
//#region src/components/prompt-form/file-attachments.ts
|
|
564
|
+
const ALLOWED_EXTENSIONS = new Set([
|
|
565
|
+
".pdf",
|
|
566
|
+
".jpg",
|
|
567
|
+
".jpeg",
|
|
568
|
+
".png",
|
|
569
|
+
".gif",
|
|
570
|
+
".webp",
|
|
571
|
+
".bmp",
|
|
572
|
+
".doc",
|
|
573
|
+
".docx",
|
|
574
|
+
".xls",
|
|
575
|
+
".xlsx",
|
|
576
|
+
".ppt",
|
|
577
|
+
".pptx"
|
|
578
|
+
]);
|
|
579
|
+
const PROMPT_FORM_FILE_ACCEPT = "image/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx";
|
|
580
|
+
function getFileExtension(filename) {
|
|
581
|
+
const lastDotIndex = filename.lastIndexOf(".");
|
|
582
|
+
if (lastDotIndex <= 0 || lastDotIndex === filename.length - 1) return;
|
|
583
|
+
return filename.slice(lastDotIndex).toLowerCase();
|
|
584
|
+
}
|
|
585
|
+
function isAllowedPromptFormFile(file) {
|
|
586
|
+
const extension = getFileExtension(file.name);
|
|
587
|
+
if (extension && ALLOWED_EXTENSIONS.has(extension)) return true;
|
|
588
|
+
return file.type.startsWith("image/");
|
|
589
|
+
}
|
|
590
|
+
function partitionPromptFormFiles(files) {
|
|
591
|
+
const validFiles = [];
|
|
592
|
+
const invalidFiles = [];
|
|
593
|
+
for (const file of files) {
|
|
594
|
+
if (isAllowedPromptFormFile(file)) {
|
|
595
|
+
validFiles.push(file);
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
invalidFiles.push({
|
|
599
|
+
file,
|
|
600
|
+
reason: `File type is not supported for "${file.name}".`
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
return {
|
|
604
|
+
validFiles,
|
|
605
|
+
invalidFiles
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
//#endregion
|
|
610
|
+
//#region src/components/prompt-form/prompt-form-attach-button.tsx
|
|
611
|
+
function PromptFormAttachButton({ onFilesSelected, onInvalidFiles, accept = PROMPT_FORM_FILE_ACCEPT, disabled = false, label = "Attach file",...props }) {
|
|
612
|
+
const fileInputRef = useRef(null);
|
|
613
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("input", {
|
|
614
|
+
ref: fileInputRef,
|
|
615
|
+
type: "file",
|
|
616
|
+
accept,
|
|
617
|
+
multiple: true,
|
|
618
|
+
className: "hidden",
|
|
619
|
+
onChange: (event) => {
|
|
620
|
+
const { validFiles, invalidFiles } = partitionPromptFormFiles(Array.from(event.target.files ?? []));
|
|
621
|
+
if (invalidFiles.length > 0) onInvalidFiles?.(invalidFiles);
|
|
622
|
+
if (validFiles.length > 0) onFilesSelected(validFiles);
|
|
623
|
+
event.currentTarget.value = "";
|
|
624
|
+
}
|
|
625
|
+
}), /* @__PURE__ */ jsx(PromptFormIconButton, {
|
|
626
|
+
type: "button",
|
|
627
|
+
"aria-label": label,
|
|
628
|
+
disabled,
|
|
629
|
+
onClick: () => fileInputRef.current?.click(),
|
|
630
|
+
...props,
|
|
631
|
+
children: /* @__PURE__ */ jsx(Paperclip, { className: "size-4" })
|
|
632
|
+
})] });
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
//#endregion
|
|
636
|
+
//#region src/components/prompt-form/prompt-form.tsx
|
|
637
|
+
const actionButtonClassName = "inline-flex size-8 shrink-0 items-center justify-center rounded-md bg-neutral-100 text-neutral-600 transition-colors hover:bg-neutral-200 disabled:cursor-not-allowed disabled:opacity-40";
|
|
638
|
+
const defaultMaxRows = 4;
|
|
639
|
+
function resizeTextarea(textarea, maxRows) {
|
|
640
|
+
const styles = getComputedStyle(textarea);
|
|
641
|
+
const lineHeight = Number.parseFloat(styles.lineHeight);
|
|
642
|
+
const paddingTop = Number.parseFloat(styles.paddingTop);
|
|
643
|
+
const paddingBottom = Number.parseFloat(styles.paddingBottom);
|
|
644
|
+
const maxHeight = lineHeight * maxRows + paddingTop + paddingBottom;
|
|
645
|
+
textarea.style.height = "auto";
|
|
646
|
+
textarea.style.height = `${Math.min(textarea.scrollHeight, maxHeight)}px`;
|
|
647
|
+
textarea.style.overflowY = textarea.scrollHeight > maxHeight ? "auto" : "hidden";
|
|
648
|
+
}
|
|
649
|
+
function PromptFormIconButton({ className, type = "button",...props }) {
|
|
650
|
+
return /* @__PURE__ */ jsx("button", {
|
|
651
|
+
type,
|
|
652
|
+
className: joinClasses(actionButtonClassName, className),
|
|
653
|
+
...props
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
function PromptForm({ value, onValueChange, onSubmit, status = "ready", onStop, disabled = false, clearOnSubmit = true, enabledTools, placeholder = "Type a message...", sendLabel = "Send", stopLabel = "Stop", attachLabel = "Attach file", removeFileLabel = "Remove file", enableAttachments = true, files: filesProp, onFilesChange, onInvalidFiles, accept = PROMPT_FORM_FILE_ACCEPT, className, textareaClassName, attachmentsClassName, maxRows = defaultMaxRows, submitButtonClassName, stopButtonClassName, leftSlot, rightSlot }) {
|
|
657
|
+
const textareaRef = useRef(null);
|
|
658
|
+
const [internalFiles, setInternalFiles] = useState([]);
|
|
659
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
660
|
+
const files = filesProp ?? internalFiles;
|
|
661
|
+
const setFiles = onFilesChange ?? setInternalFiles;
|
|
662
|
+
const isStreaming = status === "streaming" || status === "submitted";
|
|
663
|
+
const hasContent = value.trim().length > 0 || files.length > 0;
|
|
664
|
+
const canSubmit = !disabled && !isSubmitting && !isStreaming && hasContent;
|
|
665
|
+
const isInteractionDisabled = disabled || isSubmitting;
|
|
666
|
+
useLayoutEffect(() => {
|
|
667
|
+
const textarea = textareaRef.current;
|
|
668
|
+
if (textarea) resizeTextarea(textarea, maxRows);
|
|
669
|
+
}, [maxRows]);
|
|
670
|
+
function addFiles(nextFiles) {
|
|
671
|
+
const { validFiles, invalidFiles } = partitionPromptFormFiles(nextFiles);
|
|
672
|
+
if (invalidFiles.length > 0) onInvalidFiles?.(invalidFiles);
|
|
673
|
+
if (validFiles.length > 0) setFiles([...files, ...validFiles]);
|
|
674
|
+
}
|
|
675
|
+
function removeFile(index) {
|
|
676
|
+
setFiles(files.filter((_, fileIndex) => fileIndex !== index));
|
|
677
|
+
}
|
|
678
|
+
return /* @__PURE__ */ jsxs("form", {
|
|
679
|
+
className: joinClasses("flex w-full flex-col overflow-hidden rounded-2xl border border-neutral-200 bg-white", className),
|
|
680
|
+
onSubmit: async (event) => {
|
|
681
|
+
event.preventDefault();
|
|
682
|
+
if (!canSubmit) return;
|
|
683
|
+
setIsSubmitting(true);
|
|
684
|
+
try {
|
|
685
|
+
const text = value.trim();
|
|
686
|
+
const selectedFiles = [...files];
|
|
687
|
+
await onSubmit({
|
|
688
|
+
text,
|
|
689
|
+
...enabledTools ? { enabledTools } : {},
|
|
690
|
+
files: selectedFiles
|
|
691
|
+
});
|
|
692
|
+
if (clearOnSubmit) {
|
|
693
|
+
onValueChange("");
|
|
694
|
+
setFiles([]);
|
|
695
|
+
}
|
|
696
|
+
} finally {
|
|
697
|
+
setIsSubmitting(false);
|
|
698
|
+
queueMicrotask(() => {
|
|
699
|
+
const textarea = textareaRef.current;
|
|
700
|
+
if (textarea) resizeTextarea(textarea, maxRows);
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
},
|
|
704
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
705
|
+
className: "flex items-end gap-3 px-4 py-3",
|
|
706
|
+
children: [
|
|
707
|
+
leftSlot ?? null,
|
|
708
|
+
/* @__PURE__ */ jsx("textarea", {
|
|
709
|
+
ref: textareaRef,
|
|
710
|
+
rows: 1,
|
|
711
|
+
className: joinClasses("min-h-10 flex-1 resize-none overflow-hidden border-0 bg-transparent px-0 py-1.5 text-sm leading-6 text-neutral-900 outline-none placeholder:text-neutral-400", textareaClassName),
|
|
712
|
+
value,
|
|
713
|
+
onChange: (event) => {
|
|
714
|
+
onValueChange(event.target.value);
|
|
715
|
+
resizeTextarea(event.currentTarget, maxRows);
|
|
716
|
+
},
|
|
717
|
+
placeholder,
|
|
718
|
+
disabled: isInteractionDisabled
|
|
719
|
+
}),
|
|
720
|
+
/* @__PURE__ */ jsxs("div", {
|
|
721
|
+
className: "flex shrink-0 items-center gap-2",
|
|
722
|
+
children: [
|
|
723
|
+
enableAttachments ? /* @__PURE__ */ jsx(PromptFormAttachButton, {
|
|
724
|
+
accept,
|
|
725
|
+
disabled: isInteractionDisabled,
|
|
726
|
+
label: attachLabel,
|
|
727
|
+
onFilesSelected: addFiles,
|
|
728
|
+
onInvalidFiles
|
|
729
|
+
}) : null,
|
|
730
|
+
rightSlot ?? null,
|
|
731
|
+
isStreaming ? /* @__PURE__ */ jsx(PromptFormIconButton, {
|
|
732
|
+
"aria-label": stopLabel,
|
|
733
|
+
className: stopButtonClassName,
|
|
734
|
+
onClick: onStop,
|
|
735
|
+
disabled: disabled || !onStop,
|
|
736
|
+
children: /* @__PURE__ */ jsx(Stop, { className: "size-4" })
|
|
737
|
+
}) : /* @__PURE__ */ jsx(PromptFormIconButton, {
|
|
738
|
+
type: "submit",
|
|
739
|
+
"aria-label": sendLabel,
|
|
740
|
+
className: submitButtonClassName,
|
|
741
|
+
disabled: !canSubmit,
|
|
742
|
+
children: /* @__PURE__ */ jsx(ArrowUp, { className: "size-4" })
|
|
743
|
+
})
|
|
744
|
+
]
|
|
745
|
+
})
|
|
746
|
+
]
|
|
747
|
+
}), files.length > 0 ? /* @__PURE__ */ jsx("div", {
|
|
748
|
+
className: joinClasses("flex flex-wrap gap-2 border-t border-neutral-200 px-4 py-2", attachmentsClassName),
|
|
749
|
+
children: files.map((file, index) => /* @__PURE__ */ jsxs("div", {
|
|
750
|
+
className: "flex max-w-full items-center gap-1.5 rounded-md bg-neutral-100 px-2 py-1 text-xs text-neutral-700",
|
|
751
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
752
|
+
className: "max-w-[150px] truncate",
|
|
753
|
+
children: file.name
|
|
754
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
755
|
+
type: "button",
|
|
756
|
+
className: "text-neutral-500 transition-colors hover:text-neutral-800 disabled:cursor-not-allowed disabled:opacity-40",
|
|
757
|
+
"aria-label": `${removeFileLabel}: ${file.name}`,
|
|
758
|
+
disabled: isInteractionDisabled,
|
|
759
|
+
onClick: () => removeFile(index),
|
|
760
|
+
children: /* @__PURE__ */ jsx(X, { className: "size-3.5" })
|
|
761
|
+
})]
|
|
762
|
+
}, `${file.name}-${file.lastModified}-${index}`))
|
|
763
|
+
}) : null]
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
//#endregion
|
|
768
|
+
//#region src/components/speech-to-text-toggle/transcribe-audio.ts
|
|
769
|
+
const DUMMY_TRANSCRIPTION = "Hello, this is a demo transcription.";
|
|
770
|
+
async function dummyTranscribeAudio(_audio, signal) {
|
|
771
|
+
await new Promise((resolve, reject) => {
|
|
772
|
+
const timeoutId = setTimeout(resolve, 600);
|
|
773
|
+
signal?.addEventListener("abort", () => {
|
|
774
|
+
clearTimeout(timeoutId);
|
|
775
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
776
|
+
}, { once: true });
|
|
777
|
+
});
|
|
778
|
+
return DUMMY_TRANSCRIPTION;
|
|
779
|
+
}
|
|
780
|
+
async function transcribeAudioViaEndpoint(audio, endpoint, signal) {
|
|
781
|
+
const response = await fetch(endpoint, {
|
|
782
|
+
method: "POST",
|
|
783
|
+
body: audio,
|
|
784
|
+
signal
|
|
785
|
+
});
|
|
786
|
+
if (!response.ok) throw new Error(`Transcription request failed with status ${response.status}`);
|
|
787
|
+
const body = await response.json();
|
|
788
|
+
const text = body.text ?? body.data;
|
|
789
|
+
if (!text) throw new Error("Transcription response did not include text");
|
|
790
|
+
return text;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
//#endregion
|
|
794
|
+
//#region src/components/speech-to-text-toggle/use-voice-recording.ts
|
|
795
|
+
function useVoiceRecording() {
|
|
796
|
+
const mediaRecorderRef = useRef(null);
|
|
797
|
+
const streamRef = useRef(null);
|
|
798
|
+
const chunksRef = useRef([]);
|
|
799
|
+
const stopTracks = useCallback(() => {
|
|
800
|
+
for (const track of streamRef.current?.getTracks() ?? []) track.stop();
|
|
801
|
+
streamRef.current = null;
|
|
802
|
+
}, []);
|
|
803
|
+
return {
|
|
804
|
+
start: useCallback(async () => {
|
|
805
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
806
|
+
const recorder = new MediaRecorder(stream);
|
|
807
|
+
streamRef.current = stream;
|
|
808
|
+
chunksRef.current = [];
|
|
809
|
+
mediaRecorderRef.current = recorder;
|
|
810
|
+
recorder.addEventListener("dataavailable", (event) => {
|
|
811
|
+
if (event.data.size > 0) chunksRef.current.push(event.data);
|
|
812
|
+
});
|
|
813
|
+
recorder.start();
|
|
814
|
+
}, []),
|
|
815
|
+
stop: useCallback(async () => {
|
|
816
|
+
const recorder = mediaRecorderRef.current;
|
|
817
|
+
if (!recorder || recorder.state === "inactive") throw new Error("No active voice recording");
|
|
818
|
+
const audio = await new Promise((resolve, reject) => {
|
|
819
|
+
recorder.addEventListener("stop", () => {
|
|
820
|
+
stopTracks();
|
|
821
|
+
mediaRecorderRef.current = null;
|
|
822
|
+
resolve(new Blob(chunksRef.current, { type: recorder.mimeType || "audio/webm" }));
|
|
823
|
+
}, { once: true });
|
|
824
|
+
recorder.addEventListener("error", () => {
|
|
825
|
+
stopTracks();
|
|
826
|
+
mediaRecorderRef.current = null;
|
|
827
|
+
reject(/* @__PURE__ */ new Error("Failed to stop voice recording"));
|
|
828
|
+
}, { once: true });
|
|
829
|
+
recorder.stop();
|
|
830
|
+
});
|
|
831
|
+
chunksRef.current = [];
|
|
832
|
+
return audio;
|
|
833
|
+
}, [stopTracks]),
|
|
834
|
+
cancel: useCallback(() => {
|
|
835
|
+
const recorder = mediaRecorderRef.current;
|
|
836
|
+
if (recorder && recorder.state !== "inactive") recorder.stop();
|
|
837
|
+
stopTracks();
|
|
838
|
+
mediaRecorderRef.current = null;
|
|
839
|
+
chunksRef.current = [];
|
|
840
|
+
}, [stopTracks])
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
//#endregion
|
|
845
|
+
//#region src/components/speech-to-text-toggle/speech-to-text-toggle.tsx
|
|
846
|
+
function SpeechToTextToggle({ onTranscriptionComplete, onTranscriptionError, transcribe, transcriptionEndpoint, disabled = false, label = "Voice input", listeningLabel = "Stop recording", loadingLabel = "Transcribing...", className,...props }) {
|
|
847
|
+
const [isListening, setIsListening] = useState(false);
|
|
848
|
+
const [isTranscribing, setIsTranscribing] = useState(false);
|
|
849
|
+
const abortControllerRef = useRef(null);
|
|
850
|
+
const { start, stop } = useVoiceRecording();
|
|
851
|
+
const transcribeAudio = transcribe ?? (transcriptionEndpoint ? (audio, signal) => transcribeAudioViaEndpoint(audio, transcriptionEndpoint, signal) : dummyTranscribeAudio);
|
|
852
|
+
async function handleToggle() {
|
|
853
|
+
if (disabled || isTranscribing) return;
|
|
854
|
+
if (!isListening) {
|
|
855
|
+
try {
|
|
856
|
+
await start();
|
|
857
|
+
setIsListening(true);
|
|
858
|
+
} catch (error) {
|
|
859
|
+
onTranscriptionError?.(error instanceof Error ? error : /* @__PURE__ */ new Error("Microphone access was denied"));
|
|
860
|
+
}
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
setIsListening(false);
|
|
864
|
+
setIsTranscribing(true);
|
|
865
|
+
const abortController = new AbortController();
|
|
866
|
+
abortControllerRef.current = abortController;
|
|
867
|
+
try {
|
|
868
|
+
const text = await transcribeAudio(await stop(), abortController.signal);
|
|
869
|
+
if (text.trim()) onTranscriptionComplete(text);
|
|
870
|
+
} catch (error) {
|
|
871
|
+
if (error instanceof DOMException && error.name === "AbortError") return;
|
|
872
|
+
onTranscriptionError?.(error instanceof Error ? error : /* @__PURE__ */ new Error("Transcription failed"));
|
|
873
|
+
} finally {
|
|
874
|
+
abortControllerRef.current = null;
|
|
875
|
+
setIsTranscribing(false);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
const ariaLabel = isTranscribing ? loadingLabel : isListening ? listeningLabel : label;
|
|
879
|
+
return /* @__PURE__ */ jsx(PromptFormIconButton, {
|
|
880
|
+
type: "button",
|
|
881
|
+
"aria-label": ariaLabel,
|
|
882
|
+
"aria-pressed": isListening,
|
|
883
|
+
"aria-busy": isTranscribing,
|
|
884
|
+
disabled: disabled || isTranscribing,
|
|
885
|
+
className: joinClasses(isListening && !isTranscribing ? "text-red-600" : void 0, className),
|
|
886
|
+
onClick: () => {
|
|
887
|
+
handleToggle();
|
|
888
|
+
},
|
|
889
|
+
...props,
|
|
890
|
+
children: isTranscribing ? /* @__PURE__ */ jsx(Loader, { className: "size-4 animate-spin" }) : /* @__PURE__ */ jsx(Microphone, { className: "size-4" })
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
//#endregion
|
|
895
|
+
export { ArrowUp, Loader, Message, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PromptForm, PromptFormAttachButton, PromptFormIconButton, SpeechToTextToggle, Stop, ToolResultCard, X, dummyTranscribeAudio, isAllowedPromptFormFile, partitionPromptFormFiles, transcribeAudioViaEndpoint };
|
|
896
|
+
//# sourceMappingURL=index.js.map
|