@tinyweb_dev/agent-framework-chat-ui 0.1.1 → 0.3.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/dist/components/Composer.d.ts +38 -0
- package/dist/components/Composer.d.ts.map +1 -0
- package/dist/components/Header.d.ts +7 -0
- package/dist/components/Header.d.ts.map +1 -0
- package/dist/components/Hero.d.ts +6 -0
- package/dist/components/Hero.d.ts.map +1 -0
- package/dist/components/RealtimeCallOverlay.d.ts +68 -0
- package/dist/components/RealtimeCallOverlay.d.ts.map +1 -0
- package/dist/components/RealtimeOrb.d.ts +21 -0
- package/dist/components/RealtimeOrb.d.ts.map +1 -0
- package/{src/components/Sidebar.tsx → dist/components/Sidebar.d.ts} +5 -9
- package/dist/components/Sidebar.d.ts.map +1 -0
- package/dist/components/TinywebChat.d.ts +3 -0
- package/dist/components/TinywebChat.d.ts.map +1 -0
- package/dist/components/Transcript.d.ts +20 -0
- package/dist/components/Transcript.d.ts.map +1 -0
- package/dist/components/WireOrbEffect.d.ts +17 -0
- package/dist/components/WireOrbEffect.d.ts.map +1 -0
- package/dist/components/condense/CondensePill.d.ts +11 -0
- package/dist/components/condense/CondensePill.d.ts.map +1 -0
- package/dist/components/messages/AssistantMessage.d.ts +10 -0
- package/dist/components/messages/AssistantMessage.d.ts.map +1 -0
- package/dist/components/messages/ErrorMessage.d.ts +7 -0
- package/dist/components/messages/ErrorMessage.d.ts.map +1 -0
- package/dist/components/messages/ToolMessage.d.ts +17 -0
- package/dist/components/messages/ToolMessage.d.ts.map +1 -0
- package/dist/components/messages/UserMessage.d.ts +7 -0
- package/dist/components/messages/UserMessage.d.ts.map +1 -0
- package/dist/components/tool/ExecRenderer.d.ts +14 -0
- package/dist/components/tool/ExecRenderer.d.ts.map +1 -0
- package/dist/components/tool/ImageRenderer.d.ts +16 -0
- package/dist/components/tool/ImageRenderer.d.ts.map +1 -0
- package/dist/components/tool/JsonRenderer.d.ts +11 -0
- package/dist/components/tool/JsonRenderer.d.ts.map +1 -0
- package/dist/components/tool/ToolCard.d.ts +29 -0
- package/dist/components/tool/ToolCard.d.ts.map +1 -0
- package/dist/components/tool/builtinRenderers.d.ts +9 -0
- package/dist/components/tool/builtinRenderers.d.ts.map +1 -0
- package/dist/hooks/useAgentChat.d.ts +46 -0
- package/dist/hooks/useAgentChat.d.ts.map +1 -0
- package/dist/hooks/useAnchorLatestUser.d.ts +15 -0
- package/dist/hooks/useAnchorLatestUser.d.ts.map +1 -0
- package/dist/hooks/useInitialScrollToEnd.d.ts +15 -0
- package/dist/hooks/useInitialScrollToEnd.d.ts.map +1 -0
- package/dist/hooks/useNdjsonStream.d.ts +11 -0
- package/dist/hooks/useNdjsonStream.d.ts.map +1 -0
- package/dist/hooks/useRealtimeCall.d.ts +112 -0
- package/dist/hooks/useRealtimeCall.d.ts.map +1 -0
- package/dist/index.cjs +3160 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3126 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown/render.d.ts +2 -0
- package/dist/markdown/render.d.ts.map +1 -0
- package/dist/types.d.ts +257 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/util/attachments.d.ts +28 -0
- package/dist/util/attachments.d.ts.map +1 -0
- package/dist/util/escape.d.ts +2 -0
- package/dist/util/escape.d.ts.map +1 -0
- package/dist/util/theme.d.ts +13 -0
- package/dist/util/theme.d.ts.map +1 -0
- package/dist/util/tool-detect.d.ts +26 -0
- package/dist/util/tool-detect.d.ts.map +1 -0
- package/dist/util/tool-labels.d.ts +11 -0
- package/dist/util/tool-labels.d.ts.map +1 -0
- package/package.json +14 -14
- package/src/components/Composer.tsx +0 -267
- package/src/components/Header.tsx +0 -16
- package/src/components/Hero.tsx +0 -16
- package/src/components/RealtimeCallOverlay.tsx +0 -894
- package/src/components/RealtimeOrb.tsx +0 -388
- package/src/components/TinywebChat.tsx +0 -95
- package/src/components/Transcript.tsx +0 -146
- package/src/components/WireOrbEffect.tsx +0 -556
- package/src/components/condense/CondensePill.tsx +0 -77
- package/src/components/messages/AssistantMessage.tsx +0 -15
- package/src/components/messages/ErrorMessage.tsx +0 -12
- package/src/components/messages/ToolMessage.tsx +0 -49
- package/src/components/messages/UserMessage.tsx +0 -37
- package/src/components/tool/ExecRenderer.tsx +0 -71
- package/src/components/tool/ImageRenderer.tsx +0 -47
- package/src/components/tool/JsonRenderer.tsx +0 -24
- package/src/components/tool/ToolCard.tsx +0 -65
- package/src/components/tool/builtinRenderers.tsx +0 -456
- package/src/hooks/useAgentChat.ts +0 -454
- package/src/hooks/useAnchorLatestUser.ts +0 -99
- package/src/hooks/useInitialScrollToEnd.ts +0 -41
- package/src/hooks/useNdjsonStream.ts +0 -71
- package/src/hooks/useRealtimeCall.ts +0 -452
- package/src/index.ts +0 -45
- package/src/markdown/render.ts +0 -121
- package/src/types.ts +0 -241
- package/src/util/attachments.ts +0 -87
- package/src/util/escape.ts +0 -16
- package/src/util/theme.ts +0 -40
- package/src/util/tool-detect.ts +0 -65
- package/src/util/tool-labels.ts +0 -34
- /package/{src → dist}/styles.css +0 -0
package/src/types.ts
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
// Public types for the @tinyweb-agent-framework/chat-ui package.
|
|
2
|
-
//
|
|
3
|
-
// We deliberately re-export only the structural types developers need to
|
|
4
|
-
// drive `<TinywebChat />`. The runtime events come from
|
|
5
|
-
// [`@tinyweb-agent-framework/protocol`](../../protocol/src/events.ts) so
|
|
6
|
-
// consumers can install just `protocol` if they want to render their own
|
|
7
|
-
// shell.
|
|
8
|
-
|
|
9
|
-
import type { ReactNode } from "react"
|
|
10
|
-
import type { AgentEvent, FinishEvent } from "@tinyweb-agent-framework/protocol"
|
|
11
|
-
|
|
12
|
-
export type ChatTheme = {
|
|
13
|
-
bg: string
|
|
14
|
-
panel: string
|
|
15
|
-
border: string
|
|
16
|
-
text: string
|
|
17
|
-
muted: string
|
|
18
|
-
accent: string
|
|
19
|
-
accent2: string
|
|
20
|
-
warn: string
|
|
21
|
-
err: string
|
|
22
|
-
/**
|
|
23
|
-
* Optional theme tokens for the realtime / voice call panel
|
|
24
|
-
* (`RealtimeCallOverlay` + `RealtimeOrb`). Each key maps 1:1 to a
|
|
25
|
-
* `--rt-*` CSS custom property. Undefined keys keep the defaults.
|
|
26
|
-
*
|
|
27
|
-
* See [plan 0019](../../plans/0019-voice-call-ui-animations.md) §3.1.
|
|
28
|
-
*/
|
|
29
|
-
realtime?: {
|
|
30
|
-
/** Top of the dark navy gradient (`--rt-bg-from`). */
|
|
31
|
-
bgFrom?: string
|
|
32
|
-
/** Bottom of the dark navy gradient (`--rt-bg-to`). */
|
|
33
|
-
bgTo?: string
|
|
34
|
-
/** Innermost halo ring color (`--rt-halo-1`). */
|
|
35
|
-
halo1?: string
|
|
36
|
-
/** Middle halo ring color (`--rt-halo-2`). */
|
|
37
|
-
halo2?: string
|
|
38
|
-
/** Outer halo ring color (`--rt-halo-3`). */
|
|
39
|
-
halo3?: string
|
|
40
|
-
/** Inner black "void" disc color (`--rt-core`). */
|
|
41
|
-
core?: string
|
|
42
|
-
/** Text accent + ripple stroke color (`--rt-accent`). */
|
|
43
|
-
accent?: string
|
|
44
|
-
/** Mic FAB background (`--rt-mic-bg`). */
|
|
45
|
-
micBg?: string
|
|
46
|
-
/** Mic icon foreground (`--rt-mic-fg`). */
|
|
47
|
-
micFg?: string
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type ToolRendererContext = {
|
|
52
|
-
toolName: string
|
|
53
|
-
result: unknown
|
|
54
|
-
/** Raw `tool-result` event for renderers that need the full payload. */
|
|
55
|
-
raw: unknown
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export type ToolRenderer = {
|
|
59
|
-
/** Returns true if this renderer should render the given tool. */
|
|
60
|
-
match: (toolName: string, result: unknown) => boolean
|
|
61
|
-
/** React node — return null to fall back to subsequent renderers / default. */
|
|
62
|
-
render: (ctx: ToolRendererContext) => ReactNode | null
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Image attachment shape mirrored from `@tinyweb-agent-framework/agent`'s
|
|
67
|
-
* `AttachmentInput`. The UI carries an extra optional `previewUrl`
|
|
68
|
-
* (object URL) that is stripped before POSTing to the server.
|
|
69
|
-
*/
|
|
70
|
-
export type ImageAttachment = {
|
|
71
|
-
readonly type: "image"
|
|
72
|
-
readonly mediaType: string
|
|
73
|
-
/** Raw base64 body (no `data:` prefix). Either `data` OR `url` must be set. */
|
|
74
|
-
readonly data?: string
|
|
75
|
-
readonly url?: string
|
|
76
|
-
readonly filename?: string
|
|
77
|
-
/** Object URL used only by the Composer thumbnail strip — never POSTed. */
|
|
78
|
-
readonly previewUrl?: string
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Optional client-side validation knobs. Defaults mirror the server
|
|
83
|
-
* (4 files / 8MB per file / 16MB total / png|jpeg|webp|gif).
|
|
84
|
-
*/
|
|
85
|
-
export type AttachmentLimits = {
|
|
86
|
-
readonly maxCount?: number
|
|
87
|
-
readonly maxBytesPerFile?: number
|
|
88
|
-
readonly maxBytesTotal?: number
|
|
89
|
-
readonly imageMimes?: ReadonlyArray<string>
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export type ChatMessage =
|
|
93
|
-
| { role: "user"; content: string; images?: ReadonlyArray<ImageAttachment> }
|
|
94
|
-
| { role: "assistant"; content: string }
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Realtime / voice call config. When present, the Composer renders a
|
|
98
|
-
* Call button that opens [`<RealtimeCallOverlay />`](../components/RealtimeCallOverlay.tsx:1).
|
|
99
|
-
*
|
|
100
|
-
* The values are forwarded verbatim to the
|
|
101
|
-
* [`useRealtimeCall`](../hooks/useRealtimeCall.ts:1) hook. Plan 0014 §9.
|
|
102
|
-
*/
|
|
103
|
-
export type RealtimeProp = {
|
|
104
|
-
/**
|
|
105
|
-
* Host route that mints ephemeral Gemini tokens. The host MUST gate
|
|
106
|
-
* this endpoint with the same auth as the chat endpoint.
|
|
107
|
-
*/
|
|
108
|
-
readonly tokenEndpoint: string
|
|
109
|
-
/** Extra headers forwarded to the token endpoint (Bearer, cookie). */
|
|
110
|
-
readonly tokenHeaders?: HeadersInit
|
|
111
|
-
/**
|
|
112
|
-
* Extra body merged into the token POST. Function form runs once per
|
|
113
|
-
* call open so consumers can refresh ids (sessionId, agentId, …).
|
|
114
|
-
*/
|
|
115
|
-
readonly tokenBody?: Record<string, unknown> | (() => Record<string, unknown>)
|
|
116
|
-
/**
|
|
117
|
-
* Map of client-side tool handlers keyed by tool name. Each handler
|
|
118
|
-
* receives the JSON args Gemini supplies and resolves to a JSON-safe
|
|
119
|
-
* result. Handlers run in the browser — DO NOT put server secrets in
|
|
120
|
-
* them. Use this only for genuinely client-safe tools (e.g. pure JS
|
|
121
|
-
* `time_now`). For everything else use `toolProxyPath`. See plan
|
|
122
|
-
* 0014 §6.2.
|
|
123
|
-
*/
|
|
124
|
-
readonly toolHandlers?: Record<string, (args: Record<string, unknown>) => Promise<unknown>>
|
|
125
|
-
/**
|
|
126
|
-
* Optional base path for a server tool proxy (e.g. `/api/tools`). When
|
|
127
|
-
* set, any `tool-call` whose name is NOT in `toolHandlers` falls back
|
|
128
|
-
* to `POST {toolProxyPath}/{name}` with the call args as JSON. The
|
|
129
|
-
* server is expected to execute the framework tool and return the
|
|
130
|
-
* JSON result. Use
|
|
131
|
-
* [`serverExecuteTool`](../../../realtime/src/tools/server.ts:1) on
|
|
132
|
-
* the server side. This is the recommended path for tools that need
|
|
133
|
-
* API keys or CORS-restricted network access.
|
|
134
|
-
*/
|
|
135
|
-
readonly toolProxyPath?: string
|
|
136
|
-
/** Optional headers (auth cookie / Bearer) forwarded to the proxy. */
|
|
137
|
-
readonly toolProxyHeaders?: HeadersInit
|
|
138
|
-
/** Auto-hangup after N idle ms. Default 10 minutes. */
|
|
139
|
-
readonly idleTimeoutMs?: number
|
|
140
|
-
/** Optional human title shown on the overlay header. */
|
|
141
|
-
readonly title?: string
|
|
142
|
-
/**
|
|
143
|
-
* Fired when the user's voice turn finishes transcribing (Gemini emits
|
|
144
|
-
* `inputTranscription.finished`). The host can persist the line to its
|
|
145
|
-
* own message store so a page reload reconstructs the voice history.
|
|
146
|
-
* Skipped when the transcript is empty / whitespace.
|
|
147
|
-
*
|
|
148
|
-
* Async errors are swallowed by the overlay — host should handle its
|
|
149
|
-
* own retry/backoff.
|
|
150
|
-
*/
|
|
151
|
-
readonly onUserTurn?: (text: string) => void | Promise<void>
|
|
152
|
-
/**
|
|
153
|
-
* Fired when the agent's voice turn finishes (`turnComplete` or
|
|
154
|
-
* `agent-transcript-final`). The accumulated transcript for that turn
|
|
155
|
-
* is passed verbatim — no leading/trailing whitespace trim. Empty
|
|
156
|
-
* turns (audio-only with no transcription) are skipped.
|
|
157
|
-
*
|
|
158
|
-
* Async errors are swallowed by the overlay — host should handle its
|
|
159
|
-
* own retry/backoff.
|
|
160
|
-
*/
|
|
161
|
-
readonly onAssistantTurn?: (text: string) => void | Promise<void>
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export type TinywebChatProps = {
|
|
165
|
-
/**
|
|
166
|
-
* POST endpoint returning NDJSON stream of AgentEvent.
|
|
167
|
-
* Body shape: `{ prompt: string, ...extraBody }`.
|
|
168
|
-
*/
|
|
169
|
-
endpoint: string
|
|
170
|
-
/**
|
|
171
|
-
* Extra fields merged into the POST body. Function form is invoked once
|
|
172
|
-
* per turn so consumers can re-evaluate session/owner ids on every send.
|
|
173
|
-
*/
|
|
174
|
-
extraBody?: Record<string, unknown> | (() => Record<string, unknown>)
|
|
175
|
-
/** Extra headers (Authorization, X-Owner-Id, ...). Function form is fresh each turn. */
|
|
176
|
-
headers?: HeadersInit | (() => HeadersInit)
|
|
177
|
-
/** Header title. Default: "Tinyweb Agent". */
|
|
178
|
-
title?: string
|
|
179
|
-
/** Hero empty-state. Pass `null` to hide entirely. */
|
|
180
|
-
hero?: { title?: string; subtitle?: string } | null
|
|
181
|
-
/** Left sidebar slot. Pass undefined to render chat full-width. */
|
|
182
|
-
sidebar?: ReactNode
|
|
183
|
-
/** Placeholder text on the composer textarea. */
|
|
184
|
-
placeholder?: string
|
|
185
|
-
/** Override theme CSS custom properties. Merged with default dark theme. */
|
|
186
|
-
theme?: Partial<ChatTheme>
|
|
187
|
-
/** Custom tool renderers — first match wins; defaults run after. */
|
|
188
|
-
toolRenderers?: ToolRenderer[]
|
|
189
|
-
/** Callback fired for every parsed event — useful for analytics. */
|
|
190
|
-
onEvent?: (event: AgentEvent) => void
|
|
191
|
-
/** Callback fired when the stream emits a `finish` event. */
|
|
192
|
-
onFinish?: (event: FinishEvent) => void
|
|
193
|
-
/** Lock composer (e.g. while parent loads session). */
|
|
194
|
-
disabled?: boolean
|
|
195
|
-
/** Initial chat replay. Tool/condense events are NOT replayed (v0). */
|
|
196
|
-
initialMessages?: ChatMessage[]
|
|
197
|
-
/** Override client-side attachment limits. Server still enforces its own. */
|
|
198
|
-
attachmentLimits?: AttachmentLimits
|
|
199
|
-
/**
|
|
200
|
-
* Optional realtime call config. When present, Composer shows a
|
|
201
|
-
* Call button. See [`RealtimeProp`](./types.ts:1) and plan 0014.
|
|
202
|
-
*/
|
|
203
|
-
realtime?: RealtimeProp
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Internal transcript node — what the renderer walks. Not exported as
|
|
208
|
-
* `ChatMessage` because tool/condense rows aren't part of the public
|
|
209
|
-
* replay contract yet (see [plan 0011 §4.1](../../../manager/plans/0011-chat-ui-package.md)).
|
|
210
|
-
*/
|
|
211
|
-
export type TranscriptNode =
|
|
212
|
-
| { kind: "user"; id: string; content: string; images?: ReadonlyArray<ImageAttachment> }
|
|
213
|
-
| { kind: "assistant"; id: string; content: string }
|
|
214
|
-
| { kind: "tool-call"; id: string; callId: string; name: string; input: unknown }
|
|
215
|
-
| { kind: "tool-result"; id: string; name: string; result: unknown; raw: unknown }
|
|
216
|
-
| { kind: "tool-error"; id: string; name: string; message: string }
|
|
217
|
-
| { kind: "error"; id: string; message: string }
|
|
218
|
-
| { kind: "condense"; id: string; trigger: string; state: CondenseState }
|
|
219
|
-
|
|
220
|
-
export type CondenseState =
|
|
221
|
-
| {
|
|
222
|
-
phase: "start"
|
|
223
|
-
trigger: string
|
|
224
|
-
estimatedTokens?: number
|
|
225
|
-
budgetTokens?: number
|
|
226
|
-
}
|
|
227
|
-
| {
|
|
228
|
-
phase: "success"
|
|
229
|
-
trigger: string
|
|
230
|
-
summary: string
|
|
231
|
-
prevTokens?: number
|
|
232
|
-
newTokens?: number
|
|
233
|
-
cost?: number
|
|
234
|
-
modelId?: string
|
|
235
|
-
}
|
|
236
|
-
| {
|
|
237
|
-
phase: "error"
|
|
238
|
-
trigger: string
|
|
239
|
-
message: string
|
|
240
|
-
retryExhausted?: boolean
|
|
241
|
-
}
|
package/src/util/attachments.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
// Browser-side attachment helpers: File → base64 + size check.
|
|
2
|
-
// Mirrors the server validator at
|
|
3
|
-
// [`packages/agent/src/attachments.ts`](../../../agent/src/attachments.ts) so
|
|
4
|
-
// the user sees errors locally before the round-trip.
|
|
5
|
-
|
|
6
|
-
import type { AttachmentLimits, ImageAttachment } from "../types"
|
|
7
|
-
|
|
8
|
-
export const DEFAULT_IMAGE_MIMES = ["image/png", "image/jpeg", "image/webp", "image/gif"] as const
|
|
9
|
-
export const DEFAULT_MAX_BYTES_PER_FILE = 8 * 1024 * 1024
|
|
10
|
-
export const DEFAULT_MAX_BYTES_TOTAL = 16 * 1024 * 1024
|
|
11
|
-
export const DEFAULT_MAX_COUNT = 4
|
|
12
|
-
|
|
13
|
-
export type AttachmentError =
|
|
14
|
-
| { code: "attachment_too_large"; message: string }
|
|
15
|
-
| { code: "attachment_unsupported_type"; message: string }
|
|
16
|
-
| { code: "attachment_too_many"; message: string }
|
|
17
|
-
| { code: "attachment_invalid"; message: string }
|
|
18
|
-
|
|
19
|
-
const fileToBase64 = (file: File): Promise<string> =>
|
|
20
|
-
new Promise((resolve, reject) => {
|
|
21
|
-
const reader = new FileReader()
|
|
22
|
-
reader.onload = () => {
|
|
23
|
-
const result = reader.result
|
|
24
|
-
if (typeof result !== "string") return reject(new Error("FileReader: non-string result"))
|
|
25
|
-
const idx = result.indexOf("base64,")
|
|
26
|
-
resolve(idx >= 0 ? result.slice(idx + "base64,".length) : result)
|
|
27
|
-
}
|
|
28
|
-
reader.onerror = () => reject(reader.error ?? new Error("FileReader error"))
|
|
29
|
-
reader.readAsDataURL(file)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
export type ConvertResult =
|
|
33
|
-
| { ok: true; image: ImageAttachment }
|
|
34
|
-
| { ok: false; error: AttachmentError }
|
|
35
|
-
|
|
36
|
-
export const convertFile = async (
|
|
37
|
-
file: File,
|
|
38
|
-
current: ReadonlyArray<ImageAttachment>,
|
|
39
|
-
limits?: AttachmentLimits,
|
|
40
|
-
): Promise<ConvertResult> => {
|
|
41
|
-
const maxCount = limits?.maxCount ?? DEFAULT_MAX_COUNT
|
|
42
|
-
const maxBytesPerFile = limits?.maxBytesPerFile ?? DEFAULT_MAX_BYTES_PER_FILE
|
|
43
|
-
const maxBytesTotal = limits?.maxBytesTotal ?? DEFAULT_MAX_BYTES_TOTAL
|
|
44
|
-
const mimes = limits?.imageMimes ?? DEFAULT_IMAGE_MIMES
|
|
45
|
-
|
|
46
|
-
if (current.length >= maxCount)
|
|
47
|
-
return {
|
|
48
|
-
ok: false,
|
|
49
|
-
error: { code: "attachment_too_many", message: `Tối đa ${maxCount} ảnh` },
|
|
50
|
-
}
|
|
51
|
-
if (!mimes.includes(file.type))
|
|
52
|
-
return {
|
|
53
|
-
ok: false,
|
|
54
|
-
error: {
|
|
55
|
-
code: "attachment_unsupported_type",
|
|
56
|
-
message: `Định dạng không hỗ trợ: ${file.type || "(unknown)"}`,
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
if (file.size > maxBytesPerFile)
|
|
60
|
-
return {
|
|
61
|
-
ok: false,
|
|
62
|
-
error: {
|
|
63
|
-
code: "attachment_too_large",
|
|
64
|
-
message: `${file.name}: vượt quá ${Math.round(maxBytesPerFile / 1024 / 1024)}MB`,
|
|
65
|
-
},
|
|
66
|
-
}
|
|
67
|
-
const currentBytes = current.reduce((s, a) => s + (a.data ? Math.floor((a.data.length * 3) / 4) : 0), 0)
|
|
68
|
-
if (currentBytes + file.size > maxBytesTotal)
|
|
69
|
-
return {
|
|
70
|
-
ok: false,
|
|
71
|
-
error: {
|
|
72
|
-
code: "attachment_too_large",
|
|
73
|
-
message: `Tổng dung lượng vượt ${Math.round(maxBytesTotal / 1024 / 1024)}MB`,
|
|
74
|
-
},
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const data = await fileToBase64(file)
|
|
78
|
-
const previewUrl = URL.createObjectURL(file)
|
|
79
|
-
return {
|
|
80
|
-
ok: true,
|
|
81
|
-
image: { type: "image", mediaType: file.type, data, filename: file.name, previewUrl },
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const revokePreview = (img: ImageAttachment): void => {
|
|
86
|
-
if (img.previewUrl) URL.revokeObjectURL(img.previewUrl)
|
|
87
|
-
}
|
package/src/util/escape.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// HTML entity escaping for string interpolation into innerHTML.
|
|
2
|
-
//
|
|
3
|
-
// Adapted from [`examples/device-pilot/public/app.js`](../../../../examples/device-pilot/public/app.js:150)
|
|
4
|
-
// `escapeHtml`. Used by [`renderMarkdown`](../markdown/render.ts) before re-injecting
|
|
5
|
-
// the markup we generate ourselves, keeping the result XSS-safe by construction.
|
|
6
|
-
|
|
7
|
-
const REPLACEMENTS: Record<string, string> = {
|
|
8
|
-
"&": "&",
|
|
9
|
-
"<": "<",
|
|
10
|
-
">": ">",
|
|
11
|
-
'"': """,
|
|
12
|
-
"'": "'",
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const escapeHtml = (input: string): string =>
|
|
16
|
-
String(input).replace(/[&<>"']/g, (c) => REPLACEMENTS[c] ?? c)
|
package/src/util/theme.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { CSSProperties } from "react"
|
|
2
|
-
import type { ChatTheme } from "../types"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Maps the user-facing `theme` keys to the CSS custom properties consumed
|
|
6
|
-
* by [`styles.css`](../styles.css). Returns a `style` object the caller can
|
|
7
|
-
* spread onto the root `.tinyweb-chat` element so overrides cascade
|
|
8
|
-
* naturally.
|
|
9
|
-
*
|
|
10
|
-
* The optional `theme.realtime` map is bridged to the `--rt-*` tokens
|
|
11
|
-
* used by the voice-call panel (plan 0019).
|
|
12
|
-
*/
|
|
13
|
-
export const themeToStyle = (theme?: Partial<ChatTheme>): CSSProperties => {
|
|
14
|
-
if (!theme) return {}
|
|
15
|
-
const style: Record<string, string> = {}
|
|
16
|
-
if (theme.bg) style["--bg"] = theme.bg
|
|
17
|
-
if (theme.panel) style["--panel"] = theme.panel
|
|
18
|
-
if (theme.border) style["--border"] = theme.border
|
|
19
|
-
if (theme.text) style["--text"] = theme.text
|
|
20
|
-
if (theme.muted) style["--muted"] = theme.muted
|
|
21
|
-
if (theme.accent) style["--accent"] = theme.accent
|
|
22
|
-
if (theme.accent2) style["--accent-2"] = theme.accent2
|
|
23
|
-
if (theme.warn) style["--warn"] = theme.warn
|
|
24
|
-
if (theme.err) style["--err"] = theme.err
|
|
25
|
-
|
|
26
|
-
const rt = theme.realtime
|
|
27
|
-
if (rt) {
|
|
28
|
-
if (rt.bgFrom) style["--rt-bg-from"] = rt.bgFrom
|
|
29
|
-
if (rt.bgTo) style["--rt-bg-to"] = rt.bgTo
|
|
30
|
-
if (rt.halo1) style["--rt-halo-1"] = rt.halo1
|
|
31
|
-
if (rt.halo2) style["--rt-halo-2"] = rt.halo2
|
|
32
|
-
if (rt.halo3) style["--rt-halo-3"] = rt.halo3
|
|
33
|
-
if (rt.core) style["--rt-core"] = rt.core
|
|
34
|
-
if (rt.accent) style["--rt-accent"] = rt.accent
|
|
35
|
-
if (rt.micBg) style["--rt-mic-bg"] = rt.micBg
|
|
36
|
-
if (rt.micFg) style["--rt-mic-fg"] = rt.micFg
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return style as CSSProperties
|
|
40
|
-
}
|
package/src/util/tool-detect.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
// Tool-result classifiers.
|
|
2
|
-
//
|
|
3
|
-
// Adapted from [`examples/device-pilot/public/app.js`](../../../../examples/device-pilot/public/app.js:474)
|
|
4
|
-
// `extractToolImage` and the `isExecResult` predicate at line 491. Pure
|
|
5
|
-
// structural checks — we deliberately don't gate on tool *name*, since custom
|
|
6
|
-
// agents may emit the same payload shape from a different name.
|
|
7
|
-
|
|
8
|
-
export type ImagePayload = {
|
|
9
|
-
mime: string
|
|
10
|
-
base64: string
|
|
11
|
-
width?: number
|
|
12
|
-
height?: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type ExecPayload = {
|
|
16
|
-
exitCode: number
|
|
17
|
-
stdout: string
|
|
18
|
-
stderr: string
|
|
19
|
-
durationMs?: number
|
|
20
|
-
timedOut?: boolean
|
|
21
|
-
truncated?: boolean
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const extractToolImage = (result: unknown): ImagePayload | undefined => {
|
|
25
|
-
if (!result || typeof result !== "object") return undefined
|
|
26
|
-
const r = result as Record<string, unknown>
|
|
27
|
-
const mime = r.mimeType
|
|
28
|
-
if (typeof mime !== "string" || !mime.startsWith("image/")) return undefined
|
|
29
|
-
const b64Raw = r.imageBase64 ?? r.base64
|
|
30
|
-
if (typeof b64Raw !== "string" || b64Raw.length === 0) return undefined
|
|
31
|
-
const width = typeof r.width === "number" ? r.width : undefined
|
|
32
|
-
const height = typeof r.height === "number" ? r.height : undefined
|
|
33
|
-
return { mime, base64: b64Raw, width, height }
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const extractExec = (result: unknown): ExecPayload | undefined => {
|
|
37
|
-
if (!result || typeof result !== "object") return undefined
|
|
38
|
-
const r = result as Record<string, unknown>
|
|
39
|
-
if (typeof r.exitCode !== "number") return undefined
|
|
40
|
-
if (typeof r.stdout !== "string") return undefined
|
|
41
|
-
if (typeof r.stderr !== "string") return undefined
|
|
42
|
-
return {
|
|
43
|
-
exitCode: r.exitCode,
|
|
44
|
-
stdout: r.stdout,
|
|
45
|
-
stderr: r.stderr,
|
|
46
|
-
durationMs: typeof r.durationMs === "number" ? r.durationMs : undefined,
|
|
47
|
-
timedOut: typeof r.timedOut === "boolean" ? r.timedOut : undefined,
|
|
48
|
-
truncated: typeof r.truncated === "boolean" ? r.truncated : undefined,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Replaces long `imageBase64`/`base64` string values inside a pretty-printed
|
|
54
|
-
* JSON dump with a short `<N bytes base64 elided>` marker. Keeps the "View
|
|
55
|
-
* JSON" affordance readable when the underlying payload is an image
|
|
56
|
-
* (otherwise the dump is one giant base64 blob).
|
|
57
|
-
*
|
|
58
|
-
* Ported from [`app.js`](../../../../examples/device-pilot/public/app.js:662).
|
|
59
|
-
*/
|
|
60
|
-
export const truncateBase64InJson = (text: string): string =>
|
|
61
|
-
text.replace(
|
|
62
|
-
/"(imageBase64|base64)":\s*"([^"]{40,})"/g,
|
|
63
|
-
(_m, key: string, val: string) =>
|
|
64
|
-
'"' + key + '": "<' + val.length + " bytes base64 elided>\"",
|
|
65
|
-
)
|
package/src/util/tool-labels.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
// Friendly Vietnamese labels for the built-in tool catalogue. Used by the
|
|
2
|
-
// pending "tool-call" row so users see "Đang xem giá vàng…" instead of
|
|
3
|
-
// `vn_gold_price({"vendor":"all"})`.
|
|
4
|
-
//
|
|
5
|
-
// Consumers can fork this list or pass their own renderer for tool-call
|
|
6
|
-
// rows in a future API extension — for v1 the labels are hard-coded.
|
|
7
|
-
|
|
8
|
-
type Label = { icon: string; verb: string }
|
|
9
|
-
|
|
10
|
-
const LABELS: Record<string, Label> = {
|
|
11
|
-
time_now: { icon: "🕒", verb: "Đang xem giờ hiện tại" },
|
|
12
|
-
weather: { icon: "⛅", verb: "Đang xem thời tiết" },
|
|
13
|
-
crypto_price: { icon: "🪙", verb: "Đang xem giá crypto" },
|
|
14
|
-
vn_gold_price: { icon: "🥇", verb: "Đang xem giá vàng" },
|
|
15
|
-
vn_lottery: { icon: "🎫", verb: "Đang xem kết quả xổ số" },
|
|
16
|
-
perplexity_search: { icon: "🔎", verb: "Đang tìm kiếm với Perplexity" },
|
|
17
|
-
web_search: { icon: "🌐", verb: "Đang tìm kiếm web" },
|
|
18
|
-
web_fetch: { icon: "📄", verb: "Đang tải nội dung trang" },
|
|
19
|
-
image_generate: { icon: "🎨", verb: "Đang tạo ảnh" },
|
|
20
|
-
image_describe: { icon: "🖼️", verb: "Đang mô tả ảnh" },
|
|
21
|
-
image_edit: { icon: "✏️", verb: "Đang chỉnh sửa ảnh" },
|
|
22
|
-
file_read: { icon: "📂", verb: "Đang đọc file" },
|
|
23
|
-
file_write: { icon: "💾", verb: "Đang lưu file" },
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Pick a Vietnamese-friendly icon + verb for a tool name. Falls back to a
|
|
28
|
-
* generic "Đang gọi công cụ <name>" so unknown tools still render politely.
|
|
29
|
-
*/
|
|
30
|
-
export const friendlyToolLabel = (toolName: string): Label => {
|
|
31
|
-
const hit = LABELS[toolName]
|
|
32
|
-
if (hit) return hit
|
|
33
|
-
return { icon: "🔧", verb: `Đang gọi công cụ ${toolName}` }
|
|
34
|
-
}
|
/package/{src → dist}/styles.css
RENAMED
|
File without changes
|