@elabs-ai/components-ai 4.0.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/LICENSE +21 -0
- package/README.md +133 -0
- package/dist/_audio-player-media-chrome-KA5DY54G.js +81 -0
- package/dist/_audio-player-media-chrome-KA5DY54G.js.map +1 -0
- package/dist/_flow-boundary-D63PJ65S.js +186 -0
- package/dist/_flow-boundary-D63PJ65S.js.map +1 -0
- package/dist/_interactive-terminal-xterm-ATJ5EW3G.js +11 -0
- package/dist/_interactive-terminal-xterm-ATJ5EW3G.js.map +1 -0
- package/dist/_persona-rive-RFR2EUWP.js +164 -0
- package/dist/_persona-rive-RFR2EUWP.js.map +1 -0
- package/dist/index.d.ts +4079 -0
- package/dist/index.js +11770 -0
- package/dist/index.js.map +1 -0
- package/package.json +87 -0
- package/src/_audio-player-media-chrome.tsx +146 -0
- package/src/_chat-shell-rail.tsx +18 -0
- package/src/_code-block-theme.test.ts +134 -0
- package/src/_code-block-theme.ts +184 -0
- package/src/_flow-boundary.tsx +237 -0
- package/src/_flow-lazy.ts +34 -0
- package/src/_interactive-terminal-xterm.ts +32 -0
- package/src/_lazy-mermaid.test.ts +101 -0
- package/src/_lazy-mermaid.ts +117 -0
- package/src/_persona-rive.tsx +252 -0
- package/src/_streamdown-i18n.ts +119 -0
- package/src/agent-timeline.stories.tsx +66 -0
- package/src/agent-timeline.test.tsx +74 -0
- package/src/agent-timeline.tsx +106 -0
- package/src/agent.tsx +113 -0
- package/src/agentic-workspace.stories.tsx +1099 -0
- package/src/artifact.stories.tsx +62 -0
- package/src/artifact.test.tsx +112 -0
- package/src/artifact.tsx +167 -0
- package/src/asset-preview.stories.tsx +100 -0
- package/src/asset-preview.test.tsx +126 -0
- package/src/asset-preview.tsx +218 -0
- package/src/attachments.tsx +393 -0
- package/src/audio-player.stories.tsx +99 -0
- package/src/audio-player.tsx +170 -0
- package/src/blocks-ai-composer.stories.tsx +83 -0
- package/src/canvas.stories.tsx +142 -0
- package/src/canvas.tsx +29 -0
- package/src/chain-of-thought.tsx +193 -0
- package/src/chat-greeting.stories.tsx +96 -0
- package/src/chat-greeting.test.tsx +91 -0
- package/src/chat-greeting.tsx +91 -0
- package/src/chat-shell.stories.tsx +123 -0
- package/src/chat-shell.test.tsx +113 -0
- package/src/chat-shell.tsx +97 -0
- package/src/chat.stories.tsx +99 -0
- package/src/checkpoint.tsx +54 -0
- package/src/code-block.stories.tsx +81 -0
- package/src/code-block.test.tsx +191 -0
- package/src/code-block.tsx +673 -0
- package/src/commit.tsx +343 -0
- package/src/composer.stories.tsx +188 -0
- package/src/composer.test.tsx +182 -0
- package/src/composer.tsx +202 -0
- package/src/confirmation.stories.tsx +61 -0
- package/src/confirmation.test.tsx +136 -0
- package/src/confirmation.tsx +273 -0
- package/src/connection.tsx +23 -0
- package/src/context-panel.stories.tsx +137 -0
- package/src/context-panel.test.tsx +198 -0
- package/src/context-panel.tsx +609 -0
- package/src/context.stories.tsx +16 -0
- package/src/context.tsx +363 -0
- package/src/controls.tsx +20 -0
- package/src/conversation.stories.tsx +26 -0
- package/src/conversation.test.tsx +123 -0
- package/src/conversation.tsx +143 -0
- package/src/dark-theme-variant.test.ts +47 -0
- package/src/edge.tsx +33 -0
- package/src/environment-variables.tsx +304 -0
- package/src/file-tree.stories.tsx +55 -0
- package/src/file-tree.test.tsx +101 -0
- package/src/file-tree.tsx +390 -0
- package/src/gallery.stories.tsx +182 -0
- package/src/gallery.test.tsx +150 -0
- package/src/gallery.tsx +737 -0
- package/src/grouped-parts.stories.tsx +104 -0
- package/src/grouped-parts.test.tsx +167 -0
- package/src/grouped-parts.tsx +247 -0
- package/src/image.stories.tsx +67 -0
- package/src/image.test.tsx +105 -0
- package/src/image.tsx +115 -0
- package/src/index.ts +93 -0
- package/src/inline-citation.stories.tsx +91 -0
- package/src/inline-citation.test.tsx +96 -0
- package/src/inline-citation.tsx +314 -0
- package/src/interactive-terminal.stories.tsx +165 -0
- package/src/interactive-terminal.test.tsx +448 -0
- package/src/interactive-terminal.tsx +444 -0
- package/src/jsx-preview.stories.tsx +265 -0
- package/src/jsx-preview.test.tsx +75 -0
- package/src/jsx-preview.tsx +409 -0
- package/src/markdown-view.stories.tsx +57 -0
- package/src/markdown-view.test.tsx +36 -0
- package/src/markdown-view.tsx +113 -0
- package/src/message-edit.stories.tsx +150 -0
- package/src/message-edit.test.tsx +115 -0
- package/src/message-edit.tsx +336 -0
- package/src/message-feedback.stories.tsx +53 -0
- package/src/message-feedback.test.tsx +62 -0
- package/src/message-feedback.tsx +117 -0
- package/src/message-form-spec.ts +349 -0
- package/src/message-form.stories.tsx +182 -0
- package/src/message-form.test.tsx +227 -0
- package/src/message-form.tsx +845 -0
- package/src/message-table-spec.ts +281 -0
- package/src/message-table.stories.tsx +176 -0
- package/src/message-table.test.tsx +144 -0
- package/src/message-table.tsx +366 -0
- package/src/message.stories.tsx +262 -0
- package/src/message.test.tsx +508 -0
- package/src/message.tsx +619 -0
- package/src/mic-selector.tsx +339 -0
- package/src/microcopy.test.tsx +123 -0
- package/src/model-selector.stories.tsx +73 -0
- package/src/model-selector.test.tsx +55 -0
- package/src/model-selector.tsx +225 -0
- package/src/motion-config.tsx +49 -0
- package/src/node.tsx +71 -0
- package/src/open-in-chat.tsx +340 -0
- package/src/package-info.tsx +205 -0
- package/src/panel.tsx +20 -0
- package/src/part-groups.ts +258 -0
- package/src/persona-sources.ts +59 -0
- package/src/persona.stories.tsx +111 -0
- package/src/persona.test.tsx +67 -0
- package/src/persona.tsx +148 -0
- package/src/plan.tsx +135 -0
- package/src/prompt-input.stories.tsx +261 -0
- package/src/prompt-input.test.tsx +392 -0
- package/src/prompt-input.tsx +1668 -0
- package/src/queue.tsx +237 -0
- package/src/reasoning.stories.tsx +81 -0
- package/src/reasoning.test.tsx +50 -0
- package/src/reasoning.tsx +229 -0
- package/src/sandbox.stories.tsx +62 -0
- package/src/sandbox.test.tsx +42 -0
- package/src/sandbox.tsx +149 -0
- package/src/schema-display.tsx +412 -0
- package/src/selection-toolbar.stories.tsx +86 -0
- package/src/selection-toolbar.test.tsx +89 -0
- package/src/selection-toolbar.tsx +192 -0
- package/src/shimmer.stories.tsx +10 -0
- package/src/shimmer.tsx +81 -0
- package/src/snippet.stories.tsx +19 -0
- package/src/snippet.test.tsx +116 -0
- package/src/snippet.tsx +138 -0
- package/src/sources.stories.tsx +34 -0
- package/src/sources.test.tsx +129 -0
- package/src/sources.tsx +93 -0
- package/src/speech-input.tsx +312 -0
- package/src/stack-trace.tsx +479 -0
- package/src/streamdown-i18n.test.tsx +163 -0
- package/src/suggestion.stories.tsx +75 -0
- package/src/suggestion.test.tsx +72 -0
- package/src/suggestion.tsx +120 -0
- package/src/task.stories.tsx +22 -0
- package/src/task.test.tsx +36 -0
- package/src/task.tsx +89 -0
- package/src/templates-ai-assistant.stories.tsx +142 -0
- package/src/terminal.tsx +244 -0
- package/src/test-results.stories.tsx +177 -0
- package/src/test-results.test.tsx +241 -0
- package/src/test-results.tsx +408 -0
- package/src/tool-result-card.stories.tsx +143 -0
- package/src/tool-result-card.test.tsx +72 -0
- package/src/tool-result-card.tsx +86 -0
- package/src/tool.stories.tsx +60 -0
- package/src/tool.test.tsx +63 -0
- package/src/tool.tsx +235 -0
- package/src/toolbar.tsx +44 -0
- package/src/transcription.tsx +118 -0
- package/src/voice-selector.tsx +469 -0
- package/src/web-preview.stories.tsx +67 -0
- package/src/web-preview.test.tsx +117 -0
- package/src/web-preview.tsx +283 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sources.test.tsx — smoke + link-safety lock for the grounding footer (#59).
|
|
3
|
+
*
|
|
4
|
+
* `Sources` is how an answer shows what it was grounded in, so the assertions
|
|
5
|
+
* that matter are about the LINKS: every source is a real `<a href>` (not a
|
|
6
|
+
* div-as-link), and every one carries `rel="noopener noreferrer"` alongside
|
|
7
|
+
* `target="_blank"` — these open model-supplied URLs, so a missing `noopener`
|
|
8
|
+
* is a reverse-tabnabbing hole, not a lint nit.
|
|
9
|
+
*
|
|
10
|
+
* The collapsible starts closed, so each test opens it the way a user would.
|
|
11
|
+
*/
|
|
12
|
+
import { describe, expect, it } from "vitest";
|
|
13
|
+
import { render, screen, fireEvent } from "@testing-library/react";
|
|
14
|
+
import { Source, SourceList, Sources, SourcesContent, SourcesTrigger } from "./sources";
|
|
15
|
+
|
|
16
|
+
const sources = [
|
|
17
|
+
{ href: "https://example.com/a", title: "Deploy log" },
|
|
18
|
+
{ href: "https://example.com/b", title: "Incident report" },
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
/** Open the collapsible the way a user does. */
|
|
22
|
+
function open(name: RegExp) {
|
|
23
|
+
fireEvent.click(screen.getByRole("button", { name }));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe("Sources — trigger", () => {
|
|
27
|
+
it("summarises the count on the trigger", () => {
|
|
28
|
+
render(
|
|
29
|
+
<Sources>
|
|
30
|
+
<SourcesTrigger count={2} />
|
|
31
|
+
<SourcesContent>
|
|
32
|
+
<Source href="https://example.com/a" title="Deploy log" />
|
|
33
|
+
</SourcesContent>
|
|
34
|
+
</Sources>,
|
|
35
|
+
);
|
|
36
|
+
expect(screen.getByRole("button", { name: /Used 2 sources/ })).toBeInTheDocument();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("keeps the list collapsed until the trigger is used", () => {
|
|
40
|
+
render(
|
|
41
|
+
<Sources>
|
|
42
|
+
<SourcesTrigger count={1} />
|
|
43
|
+
<SourcesContent>
|
|
44
|
+
<Source href="https://example.com/a" title="Deploy log" />
|
|
45
|
+
</SourcesContent>
|
|
46
|
+
</Sources>,
|
|
47
|
+
);
|
|
48
|
+
expect(screen.queryByRole("link", { name: "Deploy log" })).toBeNull();
|
|
49
|
+
open(/Used 1 sources/);
|
|
50
|
+
expect(screen.getByRole("link", { name: "Deploy log" })).toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("Source — link safety", () => {
|
|
55
|
+
it("renders a real anchor carrying the href", () => {
|
|
56
|
+
render(<Source href="https://example.com/a" title="Deploy log" />);
|
|
57
|
+
expect(screen.getByRole("link", { name: "Deploy log" })).toHaveAttribute(
|
|
58
|
+
"href",
|
|
59
|
+
"https://example.com/a",
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("opens in a new tab WITH noopener noreferrer (these are model-supplied URLs)", () => {
|
|
64
|
+
render(<Source href="https://example.com/a" title="Deploy log" />);
|
|
65
|
+
const link = screen.getByRole("link", { name: "Deploy log" });
|
|
66
|
+
expect(link).toHaveAttribute("target", "_blank");
|
|
67
|
+
expect(link.getAttribute("rel")).toContain("noopener");
|
|
68
|
+
expect(link.getAttribute("rel")).toContain("noreferrer");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("lets children replace the default icon + title anatomy", () => {
|
|
72
|
+
render(
|
|
73
|
+
<Source href="https://example.com/a">
|
|
74
|
+
<span>Custom label</span>
|
|
75
|
+
</Source>,
|
|
76
|
+
);
|
|
77
|
+
expect(screen.getByRole("link", { name: "Custom label" })).toBeInTheDocument();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("Sources — semantic token", () => {
|
|
82
|
+
it("uses text-success-text for grounded concept and does NOT use text-primary", () => {
|
|
83
|
+
const { container } = render(
|
|
84
|
+
<Sources>
|
|
85
|
+
<SourcesTrigger count={1} />
|
|
86
|
+
<SourcesContent>
|
|
87
|
+
<Source href="https://example.com/a" title="Deploy log" />
|
|
88
|
+
</SourcesContent>
|
|
89
|
+
</Sources>,
|
|
90
|
+
);
|
|
91
|
+
const root =
|
|
92
|
+
container.querySelector('[data-slot="sources"]') ||
|
|
93
|
+
(container.firstElementChild as HTMLElement);
|
|
94
|
+
expect(root.className).toContain("text-success-text");
|
|
95
|
+
expect(root.className).not.toMatch(/text-primary/);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("SourceList — the per-answer grounding footer", () => {
|
|
100
|
+
it("pluralises the count in the trigger", () => {
|
|
101
|
+
const { unmount } = render(<SourceList sources={sources} />);
|
|
102
|
+
expect(screen.getByRole("button", { name: /Grounded in 2 sources/ })).toBeInTheDocument();
|
|
103
|
+
unmount();
|
|
104
|
+
render(<SourceList sources={[sources[0]!]} />);
|
|
105
|
+
expect(screen.getByRole("button", { name: /Grounded in 1 source$/ })).toBeInTheDocument();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("renders one accessible link per source once expanded", () => {
|
|
109
|
+
render(<SourceList sources={sources} />);
|
|
110
|
+
open(/Grounded in 2 sources/);
|
|
111
|
+
expect(screen.getByRole("link", { name: "Deploy log" })).toBeInTheDocument();
|
|
112
|
+
expect(screen.getByRole("link", { name: "Incident report" })).toBeInTheDocument();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("appends extra children after the named list (the escape hatch)", () => {
|
|
116
|
+
render(
|
|
117
|
+
<SourceList sources={sources}>
|
|
118
|
+
<span>and 3 internal documents</span>
|
|
119
|
+
</SourceList>,
|
|
120
|
+
);
|
|
121
|
+
open(/Grounded in 2 sources/);
|
|
122
|
+
expect(screen.getByText("and 3 internal documents")).toBeInTheDocument();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("still renders a usable trigger with zero sources (no broken UI for [])", () => {
|
|
126
|
+
render(<SourceList sources={[]} />);
|
|
127
|
+
expect(screen.getByRole("button", { name: /Grounded in 0 sources/ })).toBeInTheDocument();
|
|
128
|
+
});
|
|
129
|
+
});
|
package/src/sources.tsx
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@elabs-ai/components-ui";
|
|
4
|
+
import { cn } from "@elabs-ai/components-ui/lib/cn";
|
|
5
|
+
import { BookIcon, ChevronDownIcon } from "lucide-react";
|
|
6
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
7
|
+
|
|
8
|
+
export type SourcesProps = ComponentProps<"div">;
|
|
9
|
+
|
|
10
|
+
export const Sources = ({ className, ...props }: SourcesProps) => (
|
|
11
|
+
<Collapsible className={cn("not-prose mb-4 text-success-text text-meta", className)} {...props} />
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export type SourcesTriggerProps = ComponentProps<typeof CollapsibleTrigger> & {
|
|
15
|
+
count: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const SourcesTrigger = ({ className, count, children, ...props }: SourcesTriggerProps) => (
|
|
19
|
+
<CollapsibleTrigger className={cn("group flex items-center gap-2", className)} {...props}>
|
|
20
|
+
{children ?? (
|
|
21
|
+
<>
|
|
22
|
+
<p className="font-medium">Used {count} sources</p>
|
|
23
|
+
<ChevronDownIcon className="h-4 w-4 transition-transform group-data-[state=open]:rotate-180" />
|
|
24
|
+
</>
|
|
25
|
+
)}
|
|
26
|
+
</CollapsibleTrigger>
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export type SourcesContentProps = ComponentProps<typeof CollapsibleContent>;
|
|
30
|
+
|
|
31
|
+
export const SourcesContent = ({ className, ...props }: SourcesContentProps) => (
|
|
32
|
+
<CollapsibleContent
|
|
33
|
+
className={cn(
|
|
34
|
+
"mt-3 flex w-fit flex-col gap-2",
|
|
35
|
+
"data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 outline-none data-[state=closed]:animate-out data-[state=open]:animate-in",
|
|
36
|
+
className,
|
|
37
|
+
)}
|
|
38
|
+
{...props}
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
export type SourceProps = ComponentProps<"a">;
|
|
43
|
+
|
|
44
|
+
export const Source = ({ href, title, children, className, ...props }: SourceProps) => (
|
|
45
|
+
<a
|
|
46
|
+
className={cn("flex items-center gap-2", className)}
|
|
47
|
+
href={href}
|
|
48
|
+
rel="noopener noreferrer"
|
|
49
|
+
target="_blank"
|
|
50
|
+
{...props}
|
|
51
|
+
>
|
|
52
|
+
{children ?? (
|
|
53
|
+
<>
|
|
54
|
+
<BookIcon className="h-4 w-4" />
|
|
55
|
+
<span className="block text-caption font-medium">{title}</span>
|
|
56
|
+
</>
|
|
57
|
+
)}
|
|
58
|
+
</a>
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
export interface SourceListItem {
|
|
62
|
+
href: string;
|
|
63
|
+
title?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type SourceListProps = Omit<ComponentProps<typeof Sources>, "children"> & {
|
|
67
|
+
/** The grounding sources, rendered as the named list. */
|
|
68
|
+
sources: SourceListItem[];
|
|
69
|
+
/** Extra rows appended after the named list (escape hatch). */
|
|
70
|
+
children?: ReactNode;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* SourceList — the per-answer grounding footer (research 11 §B.4): a green
|
|
75
|
+
* "Grounded in N sources" header + the named source list. Sugar over the fixed
|
|
76
|
+
* `Sources`/`Source` primitives, reused by the context panel (research 09).
|
|
77
|
+
*/
|
|
78
|
+
export const SourceList = ({ sources, children, ...props }: SourceListProps) => (
|
|
79
|
+
<Sources {...props}>
|
|
80
|
+
<SourcesTrigger count={sources.length}>
|
|
81
|
+
<p className="font-medium">
|
|
82
|
+
Grounded in {sources.length} {sources.length === 1 ? "source" : "sources"}
|
|
83
|
+
</p>
|
|
84
|
+
<ChevronDownIcon className="h-4 w-4 transition-transform group-data-[state=open]:rotate-180" />
|
|
85
|
+
</SourcesTrigger>
|
|
86
|
+
<SourcesContent>
|
|
87
|
+
{sources.map((source) => (
|
|
88
|
+
<Source href={source.href} key={source.href} title={source.title} />
|
|
89
|
+
))}
|
|
90
|
+
{children}
|
|
91
|
+
</SourcesContent>
|
|
92
|
+
</Sources>
|
|
93
|
+
);
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Button } from "@elabs-ai/components-ui";
|
|
4
|
+
import { Spinner } from "@elabs-ai/components-ui";
|
|
5
|
+
import { cn } from "@elabs-ai/components-ui/lib/cn";
|
|
6
|
+
import { MicIcon, SquareIcon } from "lucide-react";
|
|
7
|
+
import type { ComponentProps } from "react";
|
|
8
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
9
|
+
|
|
10
|
+
interface SpeechRecognition extends EventTarget {
|
|
11
|
+
continuous: boolean;
|
|
12
|
+
interimResults: boolean;
|
|
13
|
+
lang: string;
|
|
14
|
+
start(): void;
|
|
15
|
+
stop(): void;
|
|
16
|
+
onstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
17
|
+
onend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
18
|
+
onresult: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void) | null;
|
|
19
|
+
onerror: ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => void) | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface SpeechRecognitionEvent extends Event {
|
|
23
|
+
results: SpeechRecognitionResultList;
|
|
24
|
+
resultIndex: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface SpeechRecognitionResultList {
|
|
28
|
+
readonly length: number;
|
|
29
|
+
item(index: number): SpeechRecognitionResult;
|
|
30
|
+
[index: number]: SpeechRecognitionResult;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface SpeechRecognitionResult {
|
|
34
|
+
readonly length: number;
|
|
35
|
+
item(index: number): SpeechRecognitionAlternative;
|
|
36
|
+
[index: number]: SpeechRecognitionAlternative;
|
|
37
|
+
isFinal: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface SpeechRecognitionAlternative {
|
|
41
|
+
transcript: string;
|
|
42
|
+
confidence: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface SpeechRecognitionErrorEvent extends Event {
|
|
46
|
+
error: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare global {
|
|
50
|
+
interface Window {
|
|
51
|
+
SpeechRecognition: new () => SpeechRecognition;
|
|
52
|
+
webkitSpeechRecognition: new () => SpeechRecognition;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type SpeechInputMode = "speech-recognition" | "media-recorder" | "none";
|
|
57
|
+
|
|
58
|
+
export type SpeechInputProps = ComponentProps<typeof Button> & {
|
|
59
|
+
onTranscriptionChange?: (text: string) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Callback for when audio is recorded using MediaRecorder fallback.
|
|
62
|
+
* This is called in browsers that don't support the Web Speech API (Firefox, Safari).
|
|
63
|
+
* The callback receives an audio Blob that should be sent to a transcription service.
|
|
64
|
+
* Return the transcribed text, which will be passed to onTranscriptionChange.
|
|
65
|
+
*/
|
|
66
|
+
onAudioRecorded?: (audioBlob: Blob) => Promise<string>;
|
|
67
|
+
lang?: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const detectSpeechInputMode = (): SpeechInputMode => {
|
|
71
|
+
if (typeof window === "undefined") {
|
|
72
|
+
return "none";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if ("SpeechRecognition" in window || "webkitSpeechRecognition" in window) {
|
|
76
|
+
return "speech-recognition";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if ("MediaRecorder" in window && "mediaDevices" in navigator) {
|
|
80
|
+
return "media-recorder";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return "none";
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const SpeechInput = ({
|
|
87
|
+
className,
|
|
88
|
+
onTranscriptionChange,
|
|
89
|
+
onAudioRecorded,
|
|
90
|
+
lang = "en-US",
|
|
91
|
+
...props
|
|
92
|
+
}: SpeechInputProps) => {
|
|
93
|
+
const [isListening, setIsListening] = useState(false);
|
|
94
|
+
const [isProcessing, setIsProcessing] = useState(false);
|
|
95
|
+
const [mode] = useState<SpeechInputMode>(detectSpeechInputMode);
|
|
96
|
+
const [isRecognitionReady, setIsRecognitionReady] = useState(false);
|
|
97
|
+
const recognitionRef = useRef<SpeechRecognition | null>(null);
|
|
98
|
+
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
|
99
|
+
const streamRef = useRef<MediaStream | null>(null);
|
|
100
|
+
const audioChunksRef = useRef<Blob[]>([]);
|
|
101
|
+
const onTranscriptionChangeRef =
|
|
102
|
+
useRef<SpeechInputProps["onTranscriptionChange"]>(onTranscriptionChange);
|
|
103
|
+
const onAudioRecordedRef = useRef<SpeechInputProps["onAudioRecorded"]>(onAudioRecorded);
|
|
104
|
+
|
|
105
|
+
// Keep refs in sync
|
|
106
|
+
onTranscriptionChangeRef.current = onTranscriptionChange;
|
|
107
|
+
onAudioRecordedRef.current = onAudioRecorded;
|
|
108
|
+
|
|
109
|
+
// Initialize Speech Recognition when mode is speech-recognition
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
if (mode !== "speech-recognition") {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
116
|
+
const speechRecognition = new SpeechRecognition();
|
|
117
|
+
|
|
118
|
+
speechRecognition.continuous = true;
|
|
119
|
+
speechRecognition.interimResults = true;
|
|
120
|
+
speechRecognition.lang = lang;
|
|
121
|
+
|
|
122
|
+
const handleStart = () => {
|
|
123
|
+
setIsListening(true);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const handleEnd = () => {
|
|
127
|
+
setIsListening(false);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const handleResult = (event: Event) => {
|
|
131
|
+
const speechEvent = event as SpeechRecognitionEvent;
|
|
132
|
+
let finalTranscript = "";
|
|
133
|
+
|
|
134
|
+
for (let i = speechEvent.resultIndex; i < speechEvent.results.length; i += 1) {
|
|
135
|
+
const result = speechEvent.results[i];
|
|
136
|
+
if (result?.isFinal) {
|
|
137
|
+
finalTranscript += result[0]?.transcript ?? "";
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (finalTranscript) {
|
|
142
|
+
onTranscriptionChangeRef.current?.(finalTranscript);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const handleError = () => {
|
|
147
|
+
setIsListening(false);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
speechRecognition.addEventListener("start", handleStart);
|
|
151
|
+
speechRecognition.addEventListener("end", handleEnd);
|
|
152
|
+
speechRecognition.addEventListener("result", handleResult);
|
|
153
|
+
speechRecognition.addEventListener("error", handleError);
|
|
154
|
+
|
|
155
|
+
recognitionRef.current = speechRecognition;
|
|
156
|
+
setIsRecognitionReady(true);
|
|
157
|
+
|
|
158
|
+
return () => {
|
|
159
|
+
speechRecognition.removeEventListener("start", handleStart);
|
|
160
|
+
speechRecognition.removeEventListener("end", handleEnd);
|
|
161
|
+
speechRecognition.removeEventListener("result", handleResult);
|
|
162
|
+
speechRecognition.removeEventListener("error", handleError);
|
|
163
|
+
speechRecognition.stop();
|
|
164
|
+
recognitionRef.current = null;
|
|
165
|
+
setIsRecognitionReady(false);
|
|
166
|
+
};
|
|
167
|
+
}, [mode, lang]);
|
|
168
|
+
|
|
169
|
+
// Cleanup MediaRecorder and stream on unmount
|
|
170
|
+
useEffect(
|
|
171
|
+
() => () => {
|
|
172
|
+
if (mediaRecorderRef.current?.state === "recording") {
|
|
173
|
+
mediaRecorderRef.current.stop();
|
|
174
|
+
}
|
|
175
|
+
if (streamRef.current) {
|
|
176
|
+
for (const track of streamRef.current.getTracks()) {
|
|
177
|
+
track.stop();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
[],
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// Start MediaRecorder recording
|
|
185
|
+
const startMediaRecorder = useCallback(async () => {
|
|
186
|
+
if (!onAudioRecordedRef.current) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
192
|
+
streamRef.current = stream;
|
|
193
|
+
const mediaRecorder = new MediaRecorder(stream);
|
|
194
|
+
audioChunksRef.current = [];
|
|
195
|
+
|
|
196
|
+
const handleDataAvailable = (event: BlobEvent) => {
|
|
197
|
+
if (event.data.size > 0) {
|
|
198
|
+
audioChunksRef.current.push(event.data);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const handleStop = async () => {
|
|
203
|
+
for (const track of stream.getTracks()) {
|
|
204
|
+
track.stop();
|
|
205
|
+
}
|
|
206
|
+
streamRef.current = null;
|
|
207
|
+
|
|
208
|
+
const audioBlob = new Blob(audioChunksRef.current, {
|
|
209
|
+
type: "audio/webm",
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (audioBlob.size > 0 && onAudioRecordedRef.current) {
|
|
213
|
+
setIsProcessing(true);
|
|
214
|
+
try {
|
|
215
|
+
const transcript = await onAudioRecordedRef.current(audioBlob);
|
|
216
|
+
if (transcript) {
|
|
217
|
+
onTranscriptionChangeRef.current?.(transcript);
|
|
218
|
+
}
|
|
219
|
+
} catch {
|
|
220
|
+
// Error handling delegated to the onAudioRecorded caller
|
|
221
|
+
} finally {
|
|
222
|
+
setIsProcessing(false);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const handleError = () => {
|
|
228
|
+
setIsListening(false);
|
|
229
|
+
for (const track of stream.getTracks()) {
|
|
230
|
+
track.stop();
|
|
231
|
+
}
|
|
232
|
+
streamRef.current = null;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
mediaRecorder.addEventListener("dataavailable", handleDataAvailable);
|
|
236
|
+
mediaRecorder.addEventListener("stop", handleStop);
|
|
237
|
+
mediaRecorder.addEventListener("error", handleError);
|
|
238
|
+
|
|
239
|
+
mediaRecorderRef.current = mediaRecorder;
|
|
240
|
+
mediaRecorder.start();
|
|
241
|
+
setIsListening(true);
|
|
242
|
+
} catch {
|
|
243
|
+
setIsListening(false);
|
|
244
|
+
}
|
|
245
|
+
}, []);
|
|
246
|
+
|
|
247
|
+
// Stop MediaRecorder recording
|
|
248
|
+
const stopMediaRecorder = useCallback(() => {
|
|
249
|
+
if (mediaRecorderRef.current?.state === "recording") {
|
|
250
|
+
mediaRecorderRef.current.stop();
|
|
251
|
+
}
|
|
252
|
+
setIsListening(false);
|
|
253
|
+
}, []);
|
|
254
|
+
|
|
255
|
+
const toggleListening = useCallback(() => {
|
|
256
|
+
if (mode === "speech-recognition" && recognitionRef.current) {
|
|
257
|
+
if (isListening) {
|
|
258
|
+
recognitionRef.current.stop();
|
|
259
|
+
} else {
|
|
260
|
+
recognitionRef.current.start();
|
|
261
|
+
}
|
|
262
|
+
} else if (mode === "media-recorder") {
|
|
263
|
+
if (isListening) {
|
|
264
|
+
stopMediaRecorder();
|
|
265
|
+
} else {
|
|
266
|
+
startMediaRecorder();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}, [mode, isListening, startMediaRecorder, stopMediaRecorder]);
|
|
270
|
+
|
|
271
|
+
// Determine if button should be disabled
|
|
272
|
+
const isDisabled =
|
|
273
|
+
mode === "none" ||
|
|
274
|
+
(mode === "speech-recognition" && !isRecognitionReady) ||
|
|
275
|
+
(mode === "media-recorder" && !onAudioRecorded) ||
|
|
276
|
+
isProcessing;
|
|
277
|
+
|
|
278
|
+
return (
|
|
279
|
+
<div className="relative inline-flex items-center justify-center">
|
|
280
|
+
{/* Animated pulse rings */}
|
|
281
|
+
{isListening &&
|
|
282
|
+
[0, 1, 2].map((index) => (
|
|
283
|
+
<div
|
|
284
|
+
className="absolute inset-0 animate-ping rounded-full border-2 border-red-400/30"
|
|
285
|
+
key={index}
|
|
286
|
+
style={{
|
|
287
|
+
animationDelay: `${index * 0.3}s`,
|
|
288
|
+
animationDuration: "2s",
|
|
289
|
+
}}
|
|
290
|
+
/>
|
|
291
|
+
))}
|
|
292
|
+
|
|
293
|
+
{/* Main record button */}
|
|
294
|
+
<Button
|
|
295
|
+
className={cn(
|
|
296
|
+
"relative z-10 rounded-full transition-colors duration-slow ease-standard motion-reduce:transition-none",
|
|
297
|
+
isListening
|
|
298
|
+
? "bg-destructive text-white hover:bg-destructive/80 hover:text-white"
|
|
299
|
+
: "bg-primary text-primary-foreground hover:bg-primary/80 hover:text-primary-foreground",
|
|
300
|
+
className,
|
|
301
|
+
)}
|
|
302
|
+
disabled={isDisabled}
|
|
303
|
+
onClick={toggleListening}
|
|
304
|
+
{...props}
|
|
305
|
+
>
|
|
306
|
+
{isProcessing && <Spinner />}
|
|
307
|
+
{!isProcessing && isListening && <SquareIcon className="size-4" />}
|
|
308
|
+
{!(isProcessing || isListening) && <MicIcon className="size-4" />}
|
|
309
|
+
</Button>
|
|
310
|
+
</div>
|
|
311
|
+
);
|
|
312
|
+
};
|