@tinyweb_dev/agent-framework-chat-ui 0.1.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 +223 -0
- package/package.json +61 -0
- package/src/components/Composer.tsx +267 -0
- package/src/components/Header.tsx +16 -0
- package/src/components/Hero.tsx +16 -0
- package/src/components/RealtimeCallOverlay.tsx +894 -0
- package/src/components/RealtimeOrb.tsx +388 -0
- package/src/components/Sidebar.tsx +15 -0
- package/src/components/TinywebChat.tsx +95 -0
- package/src/components/Transcript.tsx +146 -0
- package/src/components/WireOrbEffect.tsx +541 -0
- package/src/components/condense/CondensePill.tsx +77 -0
- package/src/components/messages/AssistantMessage.tsx +15 -0
- package/src/components/messages/ErrorMessage.tsx +12 -0
- package/src/components/messages/ToolMessage.tsx +49 -0
- package/src/components/messages/UserMessage.tsx +37 -0
- package/src/components/tool/ExecRenderer.tsx +71 -0
- package/src/components/tool/ImageRenderer.tsx +47 -0
- package/src/components/tool/JsonRenderer.tsx +24 -0
- package/src/components/tool/ToolCard.tsx +65 -0
- package/src/components/tool/builtinRenderers.tsx +456 -0
- package/src/hooks/useAgentChat.ts +454 -0
- package/src/hooks/useAnchorLatestUser.ts +99 -0
- package/src/hooks/useInitialScrollToEnd.ts +41 -0
- package/src/hooks/useNdjsonStream.ts +71 -0
- package/src/hooks/useRealtimeCall.ts +452 -0
- package/src/index.ts +45 -0
- package/src/markdown/render.ts +121 -0
- package/src/styles.css +1662 -0
- package/src/types.ts +241 -0
- package/src/util/attachments.ts +87 -0
- package/src/util/escape.ts +16 -0
- package/src/util/theme.ts +40 -0
- package/src/util/tool-detect.ts +65 -0
- package/src/util/tool-labels.ts +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tinyweb-agent-framework contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# @tinyweb-agent-framework/chat-ui
|
|
2
|
+
|
|
3
|
+
Drop-in React 19 chat component for [`tinyweb-agent-framework`](../../) agents.
|
|
4
|
+
Streams [`AgentEvent`](../protocol/src/events.ts) from any NDJSON endpoint,
|
|
5
|
+
renders assistant markdown, tool results (image / exec / generic JSON), and
|
|
6
|
+
context-condense pills — all with zero runtime CSS dependencies.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
bun add @tinyweb-agent-framework/chat-ui
|
|
12
|
+
# peer deps
|
|
13
|
+
bun add react react-dom @tinyweb-agent-framework/protocol
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Then import the stylesheet once (e.g. in your root `layout.tsx`):
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import "@tinyweb-agent-framework/chat-ui/styles.css"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The stylesheet is fully scoped under `.tinyweb-chat`, so it will not bleed
|
|
23
|
+
into the host app's styles.
|
|
24
|
+
|
|
25
|
+
## Quick start — 5 lines
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { TinywebChat } from "@tinyweb-agent-framework/chat-ui"
|
|
29
|
+
import "@tinyweb-agent-framework/chat-ui/styles.css"
|
|
30
|
+
|
|
31
|
+
export default function ChatPage() {
|
|
32
|
+
return <TinywebChat endpoint="/api/chat" />
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Your `/api/chat` POST handler must return NDJSON
|
|
37
|
+
`AgentEvent` objects (one JSON per `\n`-terminated line). See
|
|
38
|
+
[`packages/agent`](../agent) for the agent runtime that emits them.
|
|
39
|
+
|
|
40
|
+
## Custom tool renderer
|
|
41
|
+
|
|
42
|
+
`TinywebChat` ships with image, exec, and JSON fallback renderers. To add
|
|
43
|
+
your own — say, a chart preview for a `analytics.query` tool — pass
|
|
44
|
+
`toolRenderers`:
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { TinywebChat, type ToolRenderer } from "@tinyweb-agent-framework/chat-ui"
|
|
48
|
+
|
|
49
|
+
const ChartRenderer: ToolRenderer = {
|
|
50
|
+
match: (name) => name === "analytics.query",
|
|
51
|
+
render: ({ result }) => <ChartPreview data={result as ChartData} />,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default function Page() {
|
|
55
|
+
return (
|
|
56
|
+
<TinywebChat
|
|
57
|
+
endpoint="/api/chat"
|
|
58
|
+
toolRenderers={[ChartRenderer]}
|
|
59
|
+
/>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Renderers are evaluated **first-match-wins** in array order, and the
|
|
65
|
+
built-in image / exec / JSON renderers run AFTER your list — so you can
|
|
66
|
+
override them by matching the same shapes.
|
|
67
|
+
|
|
68
|
+
## Passing extra body / headers
|
|
69
|
+
|
|
70
|
+
Use `extraBody` (object or function) when the agent route needs more than
|
|
71
|
+
just `prompt` — e.g. session id, owner id, device kind:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
<TinywebChat
|
|
75
|
+
endpoint="/api/chat"
|
|
76
|
+
extraBody={() => ({
|
|
77
|
+
sessionId: useSessionStore.getState().id,
|
|
78
|
+
ownerId: useOwnerStore.getState().id,
|
|
79
|
+
})}
|
|
80
|
+
headers={() => ({
|
|
81
|
+
authorization: `Bearer ${useAuthStore.getState().token}`,
|
|
82
|
+
})}
|
|
83
|
+
/>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Both `extraBody` and `headers` accept a plain object OR a function. The
|
|
87
|
+
function form is invoked once per turn, so tokens stay fresh.
|
|
88
|
+
|
|
89
|
+
## Props reference
|
|
90
|
+
|
|
91
|
+
| Prop | Type | Default |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `endpoint` | `string` | — (required) |
|
|
94
|
+
| `extraBody` | `Record<string, unknown> \| (() => Record<string, unknown>)` | `undefined` |
|
|
95
|
+
| `headers` | `HeadersInit \| (() => HeadersInit)` | `undefined` |
|
|
96
|
+
| `title` | `string` | `"Tinyweb Agent"` |
|
|
97
|
+
| `hero` | `{ title?, subtitle? } \| null` | default copy, `null` to hide |
|
|
98
|
+
| `sidebar` | `ReactNode` | `undefined` (hidden) |
|
|
99
|
+
| `placeholder` | `string` | `"Ask me anything…"` |
|
|
100
|
+
| `theme` | `Partial<ChatTheme>` | dark preset |
|
|
101
|
+
| `toolRenderers` | `ToolRenderer[]` | `[]` |
|
|
102
|
+
| `onEvent` | `(event: AgentEvent) => void` | `undefined` |
|
|
103
|
+
| `onFinish` | `(event: FinishEvent) => void` | `undefined` |
|
|
104
|
+
| `disabled` | `boolean` | `false` |
|
|
105
|
+
| `initialMessages` | `ChatMessage[]` | `[]` |
|
|
106
|
+
|
|
107
|
+
## Advanced — bring your own shell
|
|
108
|
+
|
|
109
|
+
`useAgentChat` is exported so you can build a custom layout while reusing
|
|
110
|
+
the state machine. The hook returns `{ nodes, status, send, abort, reset }`.
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { useAgentChat } from "@tinyweb-agent-framework/chat-ui"
|
|
114
|
+
|
|
115
|
+
function MyChat() {
|
|
116
|
+
const { nodes, status, send } = useAgentChat({ endpoint: "/api/chat" })
|
|
117
|
+
return /* render however you like */
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`renderMarkdown(src)` is also exported if you want to reuse the same
|
|
122
|
+
XSS-safe markdown parser elsewhere.
|
|
123
|
+
|
|
124
|
+
## Compatibility
|
|
125
|
+
|
|
126
|
+
- **React**: `>= 19.0.0` (uses `useEffect`/`useLayoutEffect` only — no
|
|
127
|
+
server components / RSC features).
|
|
128
|
+
- **Next.js**: 15.x with App Router. `<TinywebChat />` is a Client
|
|
129
|
+
Component (`"use client"` directive baked in).
|
|
130
|
+
- **Bun / Node**: source ships as `.tsx`. Both Bun and Next 15 transpile
|
|
131
|
+
on the fly; no build step required for now.
|
|
132
|
+
|
|
133
|
+
## Voice call UI
|
|
134
|
+
|
|
135
|
+
When [`TinywebChatProps.realtime`](./src/types.ts:1) is provided, the
|
|
136
|
+
composer renders a Call button that opens [`RealtimeCallOverlay`](./src/components/RealtimeCallOverlay.tsx:1)
|
|
137
|
+
inside the same chat column. The voice panel features a **calm dark
|
|
138
|
+
navy stage** with a central black "void" disc surrounded by three
|
|
139
|
+
translucent purple halo rings that breathe and react in real time to
|
|
140
|
+
whoever is talking (mic input vs agent TTS output).
|
|
141
|
+
|
|
142
|
+
The animation is **CSS-only** (no Canvas, no WebGL). A single RAF loop
|
|
143
|
+
inside [`RealtimeOrb`](./src/components/RealtimeOrb.tsx:1) reads the
|
|
144
|
+
analyser nodes exposed by [`useRealtimeCall`](./src/hooks/useRealtimeCall.ts:1)
|
|
145
|
+
and writes one CSS custom property (`--rt-level`) plus a `data-speaker`
|
|
146
|
+
attribute on the orb root. Children react via pure CSS.
|
|
147
|
+
|
|
148
|
+
### Overlay props
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
<RealtimeCallOverlay
|
|
152
|
+
tokenEndpoint="/api/realtime/token"
|
|
153
|
+
onClose={() => setOpen(false)}
|
|
154
|
+
hero="How can I help you today?"
|
|
155
|
+
statusLabels={{
|
|
156
|
+
connecting: "Đang kết nối…",
|
|
157
|
+
"active-listening": "Đang lắng nghe",
|
|
158
|
+
"active-speaking": "Đang nói…",
|
|
159
|
+
"active-muted": "Đã tắt mic",
|
|
160
|
+
}}
|
|
161
|
+
/>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
| Prop | Type | Description |
|
|
165
|
+
|---|---|---|
|
|
166
|
+
| `hero` | `string` | Greeting line shown under the orb. Defaults to `"How can I help you today?"`. Pass `""` to hide. |
|
|
167
|
+
| `statusLabels` | `Partial<Record<RealtimeStatusLabelKey, string>>` | i18n / branding overrides for the top status pill. Beyond the lifecycle `CallStatus` keys, the `"active"` bucket exposes three richer sub-state keys: `active-listening`, `active-speaking`, `active-muted`. |
|
|
168
|
+
|
|
169
|
+
### Theming
|
|
170
|
+
|
|
171
|
+
The voice panel exposes a dedicated set of CSS custom properties under
|
|
172
|
+
the `.tinyweb-chat` root. Host apps can override them either directly
|
|
173
|
+
in their stylesheet or via `ChatTheme.realtime`:
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
<TinywebChat
|
|
177
|
+
theme={{
|
|
178
|
+
realtime: {
|
|
179
|
+
bgFrom: "#0a0e2a",
|
|
180
|
+
bgTo: "#050818",
|
|
181
|
+
halo1: "rgba(146, 130, 220, 0.34)",
|
|
182
|
+
halo2: "rgba(146, 130, 220, 0.22)",
|
|
183
|
+
halo3: "rgba(146, 130, 220, 0.14)",
|
|
184
|
+
core: "#06070d",
|
|
185
|
+
accent: "#b9aef0",
|
|
186
|
+
micBg: "#0d1024",
|
|
187
|
+
micFg: "#ffffff",
|
|
188
|
+
},
|
|
189
|
+
}}
|
|
190
|
+
/>
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
| Theme key | CSS variable | Purpose |
|
|
194
|
+
|---|---|---|
|
|
195
|
+
| `bgFrom` | `--rt-bg-from` | Top of the dark navy gradient. |
|
|
196
|
+
| `bgTo` | `--rt-bg-to` | Bottom of the gradient. |
|
|
197
|
+
| `halo1` | `--rt-halo-1` | Innermost halo ring color. |
|
|
198
|
+
| `halo2` | `--rt-halo-2` | Middle halo ring color. |
|
|
199
|
+
| `halo3` | `--rt-halo-3` | Outer halo ring color. |
|
|
200
|
+
| `core` | `--rt-core` | Inner black "void" disc color. |
|
|
201
|
+
| `accent` | `--rt-accent` | Text accent + ripple stroke color. |
|
|
202
|
+
| `micBg` | `--rt-mic-bg` | Mic FAB background. |
|
|
203
|
+
| `micFg` | `--rt-mic-fg` | Mic icon foreground. |
|
|
204
|
+
|
|
205
|
+
### Accessibility
|
|
206
|
+
|
|
207
|
+
- Status pill is an `aria-live="polite"` region.
|
|
208
|
+
- All icon buttons carry explicit `aria-label`s.
|
|
209
|
+
- The mic FAB uses `aria-pressed` to convey muted state.
|
|
210
|
+
- `prefers-reduced-motion: reduce` is honoured: the orb skips its RAF
|
|
211
|
+
loop and pins `--rt-level` at a small static value, and the mic
|
|
212
|
+
ripple animation is disabled.
|
|
213
|
+
|
|
214
|
+
See [plan 0019](../../plans/0019-voice-call-ui-animations.md) for the
|
|
215
|
+
full design and animation maths.
|
|
216
|
+
|
|
217
|
+
## Not included
|
|
218
|
+
|
|
219
|
+
This package is intentionally focused on the chat surface. The
|
|
220
|
+
[`device-pilot`](../../examples/device-pilot) demo's device pairing UI
|
|
221
|
+
(Owner / Pair / Devices panel) is **not** part of v0 — it lives in the
|
|
222
|
+
upcoming plan [`0012-next-chat-example`](../../manager/plans/0012-next-chat-example.md)
|
|
223
|
+
consumer.
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@tinyweb_dev/agent-framework-chat-ui",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": [
|
|
8
|
+
"**/*.css"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"typecheck": "tsc --noEmit",
|
|
12
|
+
"test": "bun test --timeout 30000"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./src/index.ts",
|
|
22
|
+
"default": "./src/index.ts"
|
|
23
|
+
},
|
|
24
|
+
"./styles.css": "./src/styles.css"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": ">=19.0.0",
|
|
28
|
+
"react-dom": ">=19.0.0",
|
|
29
|
+
"three": ">=0.160.0",
|
|
30
|
+
"@react-three/fiber": ">=8.16.0 || >=9.0.0",
|
|
31
|
+
"@tinyweb_dev/agent-framework-protocol": "0.1.0",
|
|
32
|
+
"@tinyweb_dev/agent-framework-realtime": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"@tinyweb-agent-framework/realtime": {
|
|
36
|
+
"optional": true
|
|
37
|
+
},
|
|
38
|
+
"three": {
|
|
39
|
+
"optional": true
|
|
40
|
+
},
|
|
41
|
+
"@react-three/fiber": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@react-three/fiber": "9.6.1",
|
|
47
|
+
"@testing-library/react": "16.1.0",
|
|
48
|
+
"@tsconfig/bun": "1.0.9",
|
|
49
|
+
"@types/bun": "1.3.12",
|
|
50
|
+
"@types/react": "19.0.0",
|
|
51
|
+
"@types/react-dom": "19.0.0",
|
|
52
|
+
"@types/three": "0.184.1",
|
|
53
|
+
"happy-dom": "16.0.0",
|
|
54
|
+
"react": "19.0.0",
|
|
55
|
+
"react-dom": "19.0.0",
|
|
56
|
+
"three": "0.184.0",
|
|
57
|
+
"typescript": "5.8.2",
|
|
58
|
+
"@tinyweb_dev/agent-framework-protocol": "0.1.0",
|
|
59
|
+
"@tinyweb_dev/agent-framework-realtime": "0.1.0"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type ChangeEvent, type ClipboardEvent, type DragEvent, type KeyboardEvent } from "react"
|
|
2
|
+
import type { AttachmentLimits, ImageAttachment } from "../types"
|
|
3
|
+
import { convertFile, revokePreview, DEFAULT_IMAGE_MIMES } from "../util/attachments"
|
|
4
|
+
|
|
5
|
+
export type ComposerProps = {
|
|
6
|
+
onSend: (prompt: string, options?: { images?: ReadonlyArray<ImageAttachment> }) => void
|
|
7
|
+
disabled?: boolean
|
|
8
|
+
placeholder?: string
|
|
9
|
+
attachmentLimits?: AttachmentLimits
|
|
10
|
+
/**
|
|
11
|
+
* When true the send button morphs into a stop button. Used while an
|
|
12
|
+
* agent turn is streaming so the user can force-abort a runaway tool
|
|
13
|
+
* loop. Clicking the stop button invokes [`onStop`](Composer.tsx:1)
|
|
14
|
+
* instead of [`onSend`](Composer.tsx:1).
|
|
15
|
+
*/
|
|
16
|
+
busy?: boolean
|
|
17
|
+
/** Force-stops the in-flight agent turn. Required when `busy === true`. */
|
|
18
|
+
onStop?: () => void
|
|
19
|
+
/**
|
|
20
|
+
* Optional click handler for the realtime Call button. When set the
|
|
21
|
+
* Composer renders a phone icon to the left of the textarea so the
|
|
22
|
+
* user can launch a voice call. When undefined the button is hidden.
|
|
23
|
+
* Wired by `<TinywebChat />` only when its `realtime` prop is set —
|
|
24
|
+
* see plan 0014 §9.
|
|
25
|
+
*/
|
|
26
|
+
onStartCall?: () => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Composer textarea + send button + image attachment strip.
|
|
31
|
+
*
|
|
32
|
+
* Image entry points:
|
|
33
|
+
* - paperclip button → hidden `<input type="file" accept="image/*" multiple>`
|
|
34
|
+
* - paste (Ctrl/⌘+V) of image blobs from clipboard
|
|
35
|
+
* - drag-and-drop image files onto the composer
|
|
36
|
+
*
|
|
37
|
+
* Enter sends; Shift+Enter inserts a newline.
|
|
38
|
+
*/
|
|
39
|
+
export const Composer = (props: ComposerProps) => {
|
|
40
|
+
const [value, setValue] = useState("")
|
|
41
|
+
const [images, setImages] = useState<ImageAttachment[]>([])
|
|
42
|
+
const [error, setError] = useState<string | null>(null)
|
|
43
|
+
const [dragOver, setDragOver] = useState(false)
|
|
44
|
+
const fileInputRef = useRef<HTMLInputElement | null>(null)
|
|
45
|
+
const textareaRef = useRef<HTMLTextAreaElement | null>(null)
|
|
46
|
+
|
|
47
|
+
// Revoke object URLs on unmount to avoid memory leaks.
|
|
48
|
+
useEffect(
|
|
49
|
+
() => () => {
|
|
50
|
+
images.forEach(revokePreview)
|
|
51
|
+
},
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
[],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
const limits = props.attachmentLimits
|
|
57
|
+
|
|
58
|
+
const addFiles = async (files: FileList | File[]) => {
|
|
59
|
+
setError(null)
|
|
60
|
+
let next = images
|
|
61
|
+
for (const file of Array.from(files)) {
|
|
62
|
+
const r = await convertFile(file, next, limits)
|
|
63
|
+
if (!r.ok) {
|
|
64
|
+
setError(r.error.message)
|
|
65
|
+
break
|
|
66
|
+
}
|
|
67
|
+
next = next.concat(r.image)
|
|
68
|
+
}
|
|
69
|
+
setImages(next)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const removeAt = (idx: number) => {
|
|
73
|
+
const removed = images[idx]
|
|
74
|
+
if (removed) revokePreview(removed)
|
|
75
|
+
setImages(images.filter((_, i) => i !== idx))
|
|
76
|
+
setError(null)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const submit = () => {
|
|
80
|
+
const text = value.trim()
|
|
81
|
+
if ((!text && images.length === 0) || props.disabled) return
|
|
82
|
+
const out = images
|
|
83
|
+
setValue("")
|
|
84
|
+
setImages([])
|
|
85
|
+
setError(null)
|
|
86
|
+
props.onSend(text, out.length > 0 ? { images: out } : undefined)
|
|
87
|
+
// Keep focus on the textarea so the user can keep typing the next
|
|
88
|
+
// message without re-clicking. Refocus on the next animation frame
|
|
89
|
+
// so React has finished committing the cleared value first.
|
|
90
|
+
const raf = (globalThis as { requestAnimationFrame?: typeof requestAnimationFrame }).requestAnimationFrame
|
|
91
|
+
if (raf) raf(() => textareaRef.current?.focus())
|
|
92
|
+
else textareaRef.current?.focus()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const onKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
96
|
+
if (e.key !== "Enter" || e.shiftKey) return
|
|
97
|
+
e.preventDefault()
|
|
98
|
+
submit()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const onPaste = (e: ClipboardEvent<HTMLTextAreaElement>) => {
|
|
102
|
+
const items = e.clipboardData?.items
|
|
103
|
+
if (!items) return
|
|
104
|
+
const files: File[] = []
|
|
105
|
+
for (let i = 0; i < items.length; i++) {
|
|
106
|
+
const it = items[i]
|
|
107
|
+
if (it && it.kind === "file") {
|
|
108
|
+
const f = it.getAsFile()
|
|
109
|
+
if (f && DEFAULT_IMAGE_MIMES.includes(f.type as (typeof DEFAULT_IMAGE_MIMES)[number])) files.push(f)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (files.length > 0) {
|
|
113
|
+
e.preventDefault()
|
|
114
|
+
void addFiles(files)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const onDrop = (e: DragEvent<HTMLDivElement>) => {
|
|
119
|
+
e.preventDefault()
|
|
120
|
+
setDragOver(false)
|
|
121
|
+
const files = e.dataTransfer?.files
|
|
122
|
+
if (files && files.length > 0) void addFiles(files)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const onPickFiles = (e: ChangeEvent<HTMLInputElement>) => {
|
|
126
|
+
const files = e.target.files
|
|
127
|
+
if (files && files.length > 0) void addFiles(files)
|
|
128
|
+
e.target.value = ""
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<div
|
|
133
|
+
className={`composer${dragOver ? " composer-drop" : ""}`}
|
|
134
|
+
onDragEnter={(e) => {
|
|
135
|
+
e.preventDefault()
|
|
136
|
+
setDragOver(true)
|
|
137
|
+
}}
|
|
138
|
+
onDragOver={(e) => e.preventDefault()}
|
|
139
|
+
onDragLeave={(e) => {
|
|
140
|
+
if (e.currentTarget === e.target) setDragOver(false)
|
|
141
|
+
}}
|
|
142
|
+
onDrop={onDrop}
|
|
143
|
+
>
|
|
144
|
+
{images.length > 0 && (
|
|
145
|
+
<div className="composer-attachments" data-testid="tinyweb-composer-attachments">
|
|
146
|
+
{images.map((img, i) => (
|
|
147
|
+
<div key={i} className="composer-thumb">
|
|
148
|
+
{img.previewUrl ? (
|
|
149
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
150
|
+
<img src={img.previewUrl} alt={img.filename ?? `image ${i + 1}`} />
|
|
151
|
+
) : (
|
|
152
|
+
<span className="composer-thumb-fallback">{img.filename ?? "image"}</span>
|
|
153
|
+
)}
|
|
154
|
+
<button
|
|
155
|
+
type="button"
|
|
156
|
+
className="composer-thumb-remove"
|
|
157
|
+
aria-label="Remove image"
|
|
158
|
+
onClick={() => removeAt(i)}
|
|
159
|
+
>
|
|
160
|
+
×
|
|
161
|
+
</button>
|
|
162
|
+
</div>
|
|
163
|
+
))}
|
|
164
|
+
</div>
|
|
165
|
+
)}
|
|
166
|
+
{error && (
|
|
167
|
+
<div className="composer-error" role="alert">
|
|
168
|
+
{error}
|
|
169
|
+
</div>
|
|
170
|
+
)}
|
|
171
|
+
<div className="composer-inner">
|
|
172
|
+
<button
|
|
173
|
+
type="button"
|
|
174
|
+
className="attach-btn"
|
|
175
|
+
aria-label="Attach image"
|
|
176
|
+
disabled={props.disabled}
|
|
177
|
+
onClick={() => fileInputRef.current?.click()}
|
|
178
|
+
data-testid="tinyweb-composer-attach"
|
|
179
|
+
>
|
|
180
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
|
|
181
|
+
<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" />
|
|
182
|
+
</svg>
|
|
183
|
+
</button>
|
|
184
|
+
{props.onStartCall && (
|
|
185
|
+
<button
|
|
186
|
+
type="button"
|
|
187
|
+
className="call-btn"
|
|
188
|
+
aria-label="Start voice call"
|
|
189
|
+
title="Start voice call"
|
|
190
|
+
disabled={props.disabled || props.busy}
|
|
191
|
+
onClick={props.onStartCall}
|
|
192
|
+
data-testid="tinyweb-composer-call"
|
|
193
|
+
>
|
|
194
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
|
|
195
|
+
<path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.35 1.85.59 2.81.72A2 2 0 0 1 22 16.92Z" />
|
|
196
|
+
</svg>
|
|
197
|
+
</button>
|
|
198
|
+
)}
|
|
199
|
+
<input
|
|
200
|
+
ref={fileInputRef}
|
|
201
|
+
type="file"
|
|
202
|
+
accept="image/*"
|
|
203
|
+
multiple
|
|
204
|
+
style={{ display: "none" }}
|
|
205
|
+
onChange={onPickFiles}
|
|
206
|
+
data-testid="tinyweb-composer-file"
|
|
207
|
+
/>
|
|
208
|
+
<textarea
|
|
209
|
+
ref={textareaRef}
|
|
210
|
+
rows={1}
|
|
211
|
+
placeholder={props.placeholder ?? "Ask me anything…"}
|
|
212
|
+
value={value}
|
|
213
|
+
// Intentionally NOT disabled when `busy`: disabling a focused
|
|
214
|
+
// element forces the browser to move focus to <body>, which
|
|
215
|
+
// breaks the type → enter → keep typing flow. The parent still
|
|
216
|
+
// queues the next submit (or shows the stop button) instead.
|
|
217
|
+
disabled={props.disabled}
|
|
218
|
+
onChange={(e) => setValue(e.target.value)}
|
|
219
|
+
onKeyDown={onKeyDown}
|
|
220
|
+
onPaste={onPaste}
|
|
221
|
+
data-testid="tinyweb-composer-input"
|
|
222
|
+
/>
|
|
223
|
+
{props.busy && props.onStop ? (
|
|
224
|
+
<button
|
|
225
|
+
type="button"
|
|
226
|
+
className="send-btn stop-btn"
|
|
227
|
+
aria-label="Stop"
|
|
228
|
+
title="Stop generating"
|
|
229
|
+
onClick={props.onStop}
|
|
230
|
+
data-testid="tinyweb-composer-stop"
|
|
231
|
+
>
|
|
232
|
+
<svg
|
|
233
|
+
viewBox="0 0 24 24"
|
|
234
|
+
fill="currentColor"
|
|
235
|
+
stroke="currentColor"
|
|
236
|
+
strokeWidth={1}
|
|
237
|
+
strokeLinejoin="round"
|
|
238
|
+
>
|
|
239
|
+
<rect x="6" y="6" width="12" height="12" rx="2" />
|
|
240
|
+
</svg>
|
|
241
|
+
</button>
|
|
242
|
+
) : (
|
|
243
|
+
<button
|
|
244
|
+
type="button"
|
|
245
|
+
className="send-btn"
|
|
246
|
+
aria-label="Send"
|
|
247
|
+
disabled={props.disabled || (!value.trim() && images.length === 0)}
|
|
248
|
+
onClick={submit}
|
|
249
|
+
data-testid="tinyweb-composer-send"
|
|
250
|
+
>
|
|
251
|
+
<svg
|
|
252
|
+
viewBox="0 0 24 24"
|
|
253
|
+
fill="none"
|
|
254
|
+
stroke="currentColor"
|
|
255
|
+
strokeWidth={2.2}
|
|
256
|
+
strokeLinecap="round"
|
|
257
|
+
strokeLinejoin="round"
|
|
258
|
+
>
|
|
259
|
+
<path d="M22 2 11 13" />
|
|
260
|
+
<path d="M22 2 15 22l-4-9-9-4 20-7Z" />
|
|
261
|
+
</svg>
|
|
262
|
+
</button>
|
|
263
|
+
)}
|
|
264
|
+
</div>
|
|
265
|
+
</div>
|
|
266
|
+
)
|
|
267
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
export type HeaderProps = {
|
|
4
|
+
title: string
|
|
5
|
+
right?: ReactNode
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const Header = (props: HeaderProps) => (
|
|
9
|
+
<header className="header">
|
|
10
|
+
<div className="brand">
|
|
11
|
+
<div className="brand-mark" aria-hidden="true" />
|
|
12
|
+
<h1>{props.title}</h1>
|
|
13
|
+
</div>
|
|
14
|
+
{props.right}
|
|
15
|
+
</header>
|
|
16
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WireOrbEffect } from "./WireOrbEffect"
|
|
2
|
+
|
|
3
|
+
export type HeroProps = {
|
|
4
|
+
title?: string
|
|
5
|
+
subtitle?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const Hero = (props: HeroProps) => (
|
|
9
|
+
<div className="hero">
|
|
10
|
+
<div className="hero-orb" aria-hidden="true">
|
|
11
|
+
<WireOrbEffect state="idle" />
|
|
12
|
+
</div>
|
|
13
|
+
<h2 className="hero-title">{props.title ?? "Good to see you."}</h2>
|
|
14
|
+
<p className="hero-sub">{props.subtitle ?? "Ask me anything."}</p>
|
|
15
|
+
</div>
|
|
16
|
+
)
|