@tinyweb_dev/agent-framework-chat-ui 0.2.0 → 0.4.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 +3243 -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 +3209 -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/{src → dist}/styles.css +29 -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
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
// Friendly renderers for the built-in tool catalog exposed by
|
|
2
|
-
// `@tinyweb-agent-framework/tool` (see [`packages/tool/src/builtins.ts`](../../../../tool/src/builtins.ts)).
|
|
3
|
-
//
|
|
4
|
-
// Each renderer matches by `toolName` (the default name the bundle ships
|
|
5
|
-
// with) and falls back to JSON if the payload doesn't have the shape the
|
|
6
|
-
// renderer expects. Consumers can pass their own `toolRenderers` to
|
|
7
|
-
// override or extend this set — see [`Transcript.tsx`](../Transcript.tsx).
|
|
8
|
-
|
|
9
|
-
import type { ReactNode } from "react"
|
|
10
|
-
import type { ToolRenderer, ToolRendererContext } from "../../types"
|
|
11
|
-
import { renderMarkdown } from "../../markdown/render"
|
|
12
|
-
import { ToolCard } from "./ToolCard"
|
|
13
|
-
|
|
14
|
-
const safeStringify = (v: unknown): string => {
|
|
15
|
-
try {
|
|
16
|
-
return JSON.stringify(v, null, 2)
|
|
17
|
-
} catch {
|
|
18
|
-
return String(v)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
|
23
|
-
v !== null && typeof v === "object" && !Array.isArray(v)
|
|
24
|
-
|
|
25
|
-
const asString = (v: unknown): string | undefined => (typeof v === "string" ? v : undefined)
|
|
26
|
-
const asNumber = (v: unknown): number | undefined => (typeof v === "number" ? v : undefined)
|
|
27
|
-
const asArray = (v: unknown): unknown[] | undefined => (Array.isArray(v) ? v : undefined)
|
|
28
|
-
|
|
29
|
-
const formatVnd = (n: number): string => {
|
|
30
|
-
if (!Number.isFinite(n)) return "—"
|
|
31
|
-
return n.toLocaleString("vi-VN") + " ₫"
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const formatNumber = (n: number, digits = 2): string => {
|
|
35
|
-
if (!Number.isFinite(n)) return "—"
|
|
36
|
-
return n.toLocaleString("en-US", { maximumFractionDigits: digits })
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
40
|
-
// time_now
|
|
41
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
42
|
-
|
|
43
|
-
const renderTimeNow = (ctx: ToolRendererContext): ReactNode | null => {
|
|
44
|
-
if (!isRecord(ctx.result)) return null
|
|
45
|
-
const formatted = asString(ctx.result.formatted)
|
|
46
|
-
const tz = asString(ctx.result.timezone)
|
|
47
|
-
const weekday = asString(ctx.result.weekday)
|
|
48
|
-
if (!formatted) return null
|
|
49
|
-
return (
|
|
50
|
-
<ToolCard
|
|
51
|
-
icon="🕒"
|
|
52
|
-
title="Thời gian hiện tại"
|
|
53
|
-
subtitle={tz}
|
|
54
|
-
source="System"
|
|
55
|
-
jsonText={safeStringify(ctx.result)}
|
|
56
|
-
>
|
|
57
|
-
<div className="tool-hero-line">{formatted}</div>
|
|
58
|
-
{weekday ? <div className="tool-meta-line">{weekday}</div> : null}
|
|
59
|
-
</ToolCard>
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
64
|
-
// weather
|
|
65
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
66
|
-
|
|
67
|
-
const renderWeather = (ctx: ToolRendererContext): ReactNode | null => {
|
|
68
|
-
if (!isRecord(ctx.result)) return null
|
|
69
|
-
const location = asString(ctx.result.location)
|
|
70
|
-
const temp = asNumber(ctx.result.temperature_c)
|
|
71
|
-
const condition = asString(ctx.result.condition) ?? "—"
|
|
72
|
-
if (!location || temp === undefined) return null
|
|
73
|
-
const feels = asNumber(ctx.result.feels_like_c)
|
|
74
|
-
const humidity = asNumber(ctx.result.humidity_percent)
|
|
75
|
-
const wind = asNumber(ctx.result.wind_kmh)
|
|
76
|
-
const windDir = asString(ctx.result.wind_direction)
|
|
77
|
-
const uv = asNumber(ctx.result.uv_index)
|
|
78
|
-
const region = asString(ctx.result.region)
|
|
79
|
-
const country = asString(ctx.result.country)
|
|
80
|
-
const place = [location, region, country].filter((s): s is string => Boolean(s)).join(", ")
|
|
81
|
-
return (
|
|
82
|
-
<ToolCard
|
|
83
|
-
icon="⛅"
|
|
84
|
-
title={`Thời tiết · ${location}`}
|
|
85
|
-
subtitle={place !== location ? place : undefined}
|
|
86
|
-
source="wttr.in"
|
|
87
|
-
jsonText={safeStringify(ctx.result)}
|
|
88
|
-
>
|
|
89
|
-
<div className="tool-hero-line">
|
|
90
|
-
{formatNumber(temp, 0)}°C — {condition}
|
|
91
|
-
</div>
|
|
92
|
-
<div className="tool-chip-row">
|
|
93
|
-
{feels !== undefined ? <span className="tool-chip">Cảm giác {formatNumber(feels, 0)}°C</span> : null}
|
|
94
|
-
{humidity !== undefined ? <span className="tool-chip">Độ ẩm {formatNumber(humidity, 0)}%</span> : null}
|
|
95
|
-
{wind !== undefined ? (
|
|
96
|
-
<span className="tool-chip">Gió {formatNumber(wind, 0)} km/h{windDir ? " " + windDir : ""}</span>
|
|
97
|
-
) : null}
|
|
98
|
-
{uv !== undefined ? <span className="tool-chip">UV {formatNumber(uv, 0)}</span> : null}
|
|
99
|
-
</div>
|
|
100
|
-
</ToolCard>
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
105
|
-
// crypto_price
|
|
106
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
107
|
-
|
|
108
|
-
type PriceRow = { symbol: string; quote: string; price: number; pair: string }
|
|
109
|
-
|
|
110
|
-
const renderCryptoPrice = (ctx: ToolRendererContext): ReactNode | null => {
|
|
111
|
-
if (!isRecord(ctx.result)) return null
|
|
112
|
-
const prices = asArray(ctx.result.prices)
|
|
113
|
-
if (!prices) return null
|
|
114
|
-
const rows: PriceRow[] = []
|
|
115
|
-
for (const p of prices) {
|
|
116
|
-
if (!isRecord(p)) continue
|
|
117
|
-
const symbol = asString(p.symbol)
|
|
118
|
-
const quote = asString(p.quote)
|
|
119
|
-
const price = asNumber(p.price)
|
|
120
|
-
const pair = asString(p.pair)
|
|
121
|
-
if (!symbol || !quote || price === undefined) continue
|
|
122
|
-
rows.push({ symbol, quote, price, pair: pair ?? `${symbol}${quote}` })
|
|
123
|
-
}
|
|
124
|
-
if (rows.length === 0) return null
|
|
125
|
-
return (
|
|
126
|
-
<ToolCard
|
|
127
|
-
icon="🪙"
|
|
128
|
-
title={rows.length === 1 ? `Giá crypto · ${rows[0]!.symbol}` : `Giá crypto (${rows.length})`}
|
|
129
|
-
source="Binance"
|
|
130
|
-
jsonText={safeStringify(ctx.result)}
|
|
131
|
-
>
|
|
132
|
-
<div className="tool-list">
|
|
133
|
-
{rows.map((r) => (
|
|
134
|
-
<div key={r.pair} className="tool-row">
|
|
135
|
-
<span className="tool-row-label">{r.symbol}</span>
|
|
136
|
-
<span className="tool-row-value">
|
|
137
|
-
{formatNumber(r.price, r.price >= 1 ? 2 : 6)} {r.quote}
|
|
138
|
-
</span>
|
|
139
|
-
</div>
|
|
140
|
-
))}
|
|
141
|
-
</div>
|
|
142
|
-
</ToolCard>
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
147
|
-
// vn_gold_price
|
|
148
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
149
|
-
|
|
150
|
-
type GoldRow = {
|
|
151
|
-
vendor: string
|
|
152
|
-
vendor_full: string
|
|
153
|
-
product: string
|
|
154
|
-
buy_vnd: number
|
|
155
|
-
sell_vnd: number
|
|
156
|
-
unit: string
|
|
157
|
-
source_url: string
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const renderVnGold = (ctx: ToolRendererContext): ReactNode | null => {
|
|
161
|
-
if (!isRecord(ctx.result)) return null
|
|
162
|
-
const items = asArray(ctx.result.items)
|
|
163
|
-
if (!items) return null
|
|
164
|
-
const warnings = (asArray(ctx.result.warnings) ?? []).filter(
|
|
165
|
-
(w): w is string => typeof w === "string",
|
|
166
|
-
)
|
|
167
|
-
const rows: GoldRow[] = []
|
|
168
|
-
for (const it of items) {
|
|
169
|
-
if (!isRecord(it)) continue
|
|
170
|
-
const vendor = asString(it.vendor)
|
|
171
|
-
const vendorFull = asString(it.vendor_full)
|
|
172
|
-
const product = asString(it.product)
|
|
173
|
-
const buy = asNumber(it.buy_vnd)
|
|
174
|
-
const sell = asNumber(it.sell_vnd)
|
|
175
|
-
const unit = asString(it.unit)
|
|
176
|
-
const url = asString(it.source_url)
|
|
177
|
-
if (!vendor || buy === undefined || sell === undefined) continue
|
|
178
|
-
rows.push({
|
|
179
|
-
vendor,
|
|
180
|
-
vendor_full: vendorFull ?? vendor,
|
|
181
|
-
product: product ?? "—",
|
|
182
|
-
buy_vnd: buy,
|
|
183
|
-
sell_vnd: sell,
|
|
184
|
-
unit: unit ?? "",
|
|
185
|
-
source_url: url ?? "",
|
|
186
|
-
})
|
|
187
|
-
}
|
|
188
|
-
if (rows.length === 0 && warnings.length === 0) return null
|
|
189
|
-
return (
|
|
190
|
-
<ToolCard
|
|
191
|
-
icon="🥇"
|
|
192
|
-
title="Giá vàng trong nước"
|
|
193
|
-
subtitle="Tham khảo — biến động theo phút"
|
|
194
|
-
source="BTMC / BTMH"
|
|
195
|
-
warnings={warnings}
|
|
196
|
-
jsonText={safeStringify(ctx.result)}
|
|
197
|
-
>
|
|
198
|
-
{rows.length === 0 ? (
|
|
199
|
-
<div className="tool-meta-line">Không lấy được giá. Vui lòng thử lại.</div>
|
|
200
|
-
) : (
|
|
201
|
-
<div className="tool-list">
|
|
202
|
-
{rows.map((r, i) => (
|
|
203
|
-
<div key={i} className="tool-row tool-row-stacked">
|
|
204
|
-
<div className="tool-row-head">
|
|
205
|
-
<span className="tool-row-label">{r.vendor_full}</span>
|
|
206
|
-
{r.unit ? <span className="tool-chip subtle">{r.unit}</span> : null}
|
|
207
|
-
</div>
|
|
208
|
-
<div className="tool-row-sub">{r.product}</div>
|
|
209
|
-
<div className="tool-row-prices">
|
|
210
|
-
<span className="tool-price"><em>Mua</em> {formatVnd(r.buy_vnd)}</span>
|
|
211
|
-
<span className="tool-price"><em>Bán</em> {formatVnd(r.sell_vnd)}</span>
|
|
212
|
-
</div>
|
|
213
|
-
</div>
|
|
214
|
-
))}
|
|
215
|
-
</div>
|
|
216
|
-
)}
|
|
217
|
-
</ToolCard>
|
|
218
|
-
)
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
222
|
-
// vn_lottery
|
|
223
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
224
|
-
|
|
225
|
-
type LotteryPrize = { name: string; numbers: string[] }
|
|
226
|
-
|
|
227
|
-
const renderVnLottery = (ctx: ToolRendererContext): ReactNode | null => {
|
|
228
|
-
if (!isRecord(ctx.result)) return null
|
|
229
|
-
const regionLabel = asString(ctx.result.region_label) ?? asString(ctx.result.region)
|
|
230
|
-
const date = asString(ctx.result.date)
|
|
231
|
-
const prizesRaw = asArray(ctx.result.prizes)
|
|
232
|
-
const warnings = (asArray(ctx.result.warnings) ?? []).filter(
|
|
233
|
-
(w): w is string => typeof w === "string",
|
|
234
|
-
)
|
|
235
|
-
if (!regionLabel || !prizesRaw) return null
|
|
236
|
-
const prizes: LotteryPrize[] = []
|
|
237
|
-
for (const p of prizesRaw) {
|
|
238
|
-
if (!isRecord(p)) continue
|
|
239
|
-
const name = asString(p.name)
|
|
240
|
-
const numbers = asArray(p.numbers)
|
|
241
|
-
if (!name || !numbers) continue
|
|
242
|
-
prizes.push({
|
|
243
|
-
name,
|
|
244
|
-
numbers: numbers.filter((n): n is string => typeof n === "string"),
|
|
245
|
-
})
|
|
246
|
-
}
|
|
247
|
-
return (
|
|
248
|
-
<ToolCard
|
|
249
|
-
icon="🎫"
|
|
250
|
-
title={`Kết quả xổ số · ${regionLabel}`}
|
|
251
|
-
subtitle={date ? `Ngày ${date}` : undefined}
|
|
252
|
-
source="xoso.com.vn"
|
|
253
|
-
warnings={warnings}
|
|
254
|
-
jsonText={safeStringify(ctx.result)}
|
|
255
|
-
>
|
|
256
|
-
{prizes.length === 0 ? (
|
|
257
|
-
<div className="tool-meta-line">Chưa có kết quả.</div>
|
|
258
|
-
) : (
|
|
259
|
-
<div className="tool-list">
|
|
260
|
-
{prizes.map((p, i) => (
|
|
261
|
-
<div key={i} className="tool-row tool-row-stacked">
|
|
262
|
-
<div className="tool-row-head">
|
|
263
|
-
<span className="tool-row-label">{p.name}</span>
|
|
264
|
-
</div>
|
|
265
|
-
<div className="tool-lottery-numbers">
|
|
266
|
-
{p.numbers.map((n, j) => (
|
|
267
|
-
<span key={j} className="tool-lottery-num">{n}</span>
|
|
268
|
-
))}
|
|
269
|
-
</div>
|
|
270
|
-
</div>
|
|
271
|
-
))}
|
|
272
|
-
</div>
|
|
273
|
-
)}
|
|
274
|
-
</ToolCard>
|
|
275
|
-
)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
279
|
-
// perplexity_search
|
|
280
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
281
|
-
|
|
282
|
-
type Citation = { url: string; title?: string }
|
|
283
|
-
|
|
284
|
-
const hostOf = (url: string): string => {
|
|
285
|
-
try {
|
|
286
|
-
return new URL(url).hostname.replace(/^www\./, "")
|
|
287
|
-
} catch {
|
|
288
|
-
return url
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const renderPerplexity = (ctx: ToolRendererContext): ReactNode | null => {
|
|
293
|
-
if (!isRecord(ctx.result)) return null
|
|
294
|
-
const query = asString(ctx.result.query)
|
|
295
|
-
const answer = asString(ctx.result.answer)
|
|
296
|
-
const citationsRaw = asArray(ctx.result.citations)
|
|
297
|
-
if (!answer) return null
|
|
298
|
-
const citations: Citation[] = []
|
|
299
|
-
for (const c of citationsRaw ?? []) {
|
|
300
|
-
if (!isRecord(c)) continue
|
|
301
|
-
const url = asString(c.url)
|
|
302
|
-
if (!url) continue
|
|
303
|
-
const title = asString(c.title)
|
|
304
|
-
citations.push(title ? { url, title } : { url })
|
|
305
|
-
}
|
|
306
|
-
return (
|
|
307
|
-
<ToolCard
|
|
308
|
-
icon="🔎"
|
|
309
|
-
title="Tìm kiếm với Perplexity"
|
|
310
|
-
subtitle={query}
|
|
311
|
-
source="Perplexity"
|
|
312
|
-
jsonText={safeStringify(ctx.result)}
|
|
313
|
-
>
|
|
314
|
-
<div
|
|
315
|
-
className="tool-answer tool-answer-md"
|
|
316
|
-
dangerouslySetInnerHTML={{ __html: renderMarkdown(answer) }}
|
|
317
|
-
/>
|
|
318
|
-
{citations.length > 0 ? (
|
|
319
|
-
<div className="tool-citations">
|
|
320
|
-
<div className="tool-section-label">Nguồn</div>
|
|
321
|
-
<ol className="tool-citations-list">
|
|
322
|
-
{citations.map((c, i) => (
|
|
323
|
-
<li key={i}>
|
|
324
|
-
<a href={c.url} target="_blank" rel="noopener noreferrer">
|
|
325
|
-
{c.title ?? hostOf(c.url)}
|
|
326
|
-
</a>{" "}
|
|
327
|
-
<span className="tool-cite-host">{hostOf(c.url)}</span>
|
|
328
|
-
</li>
|
|
329
|
-
))}
|
|
330
|
-
</ol>
|
|
331
|
-
</div>
|
|
332
|
-
) : null}
|
|
333
|
-
</ToolCard>
|
|
334
|
-
)
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
338
|
-
// web_search
|
|
339
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
340
|
-
|
|
341
|
-
type SearchHit = { title: string; url: string; snippet: string }
|
|
342
|
-
|
|
343
|
-
const renderWebSearch = (ctx: ToolRendererContext): ReactNode | null => {
|
|
344
|
-
if (!isRecord(ctx.result)) return null
|
|
345
|
-
const resultsRaw = asArray(ctx.result.results)
|
|
346
|
-
if (!resultsRaw) return null
|
|
347
|
-
const hits: SearchHit[] = []
|
|
348
|
-
for (const r of resultsRaw) {
|
|
349
|
-
if (!isRecord(r)) continue
|
|
350
|
-
const title = asString(r.title)
|
|
351
|
-
const url = asString(r.url)
|
|
352
|
-
const snippet = asString(r.snippet) ?? ""
|
|
353
|
-
if (!title || !url) continue
|
|
354
|
-
hits.push({ title, url, snippet })
|
|
355
|
-
}
|
|
356
|
-
if (hits.length === 0) return null
|
|
357
|
-
return (
|
|
358
|
-
<ToolCard
|
|
359
|
-
icon="🌐"
|
|
360
|
-
title="Tìm kiếm web"
|
|
361
|
-
subtitle={`${hits.length} kết quả`}
|
|
362
|
-
jsonText={safeStringify(ctx.result)}
|
|
363
|
-
>
|
|
364
|
-
<ol className="tool-search-list">
|
|
365
|
-
{hits.map((h, i) => (
|
|
366
|
-
<li key={i} className="tool-search-item">
|
|
367
|
-
<a href={h.url} target="_blank" rel="noopener noreferrer" className="tool-search-title">
|
|
368
|
-
{h.title}
|
|
369
|
-
</a>
|
|
370
|
-
<div className="tool-search-host">{hostOf(h.url)}</div>
|
|
371
|
-
{h.snippet ? <div className="tool-search-snippet">{h.snippet}</div> : null}
|
|
372
|
-
</li>
|
|
373
|
-
))}
|
|
374
|
-
</ol>
|
|
375
|
-
</ToolCard>
|
|
376
|
-
)
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
380
|
-
// web_fetch
|
|
381
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
382
|
-
|
|
383
|
-
const renderWebFetch = (ctx: ToolRendererContext): ReactNode | null => {
|
|
384
|
-
if (!isRecord(ctx.result)) return null
|
|
385
|
-
const url = asString(ctx.result.url)
|
|
386
|
-
const title = asString(ctx.result.title)
|
|
387
|
-
const content = asString(ctx.result.content) ?? asString(ctx.result.text)
|
|
388
|
-
if (!url) return null
|
|
389
|
-
const preview = content ? (content.length > 800 ? content.slice(0, 800) + "…" : content) : undefined
|
|
390
|
-
return (
|
|
391
|
-
<ToolCard
|
|
392
|
-
icon="📄"
|
|
393
|
-
title={title ?? "Đã tải trang"}
|
|
394
|
-
subtitle={hostOf(url)}
|
|
395
|
-
jsonText={safeStringify(ctx.result)}
|
|
396
|
-
>
|
|
397
|
-
<div className="tool-meta-line">
|
|
398
|
-
<a href={url} target="_blank" rel="noopener noreferrer">{url}</a>
|
|
399
|
-
</div>
|
|
400
|
-
{preview ? <div className="tool-fetch-preview">{preview}</div> : null}
|
|
401
|
-
</ToolCard>
|
|
402
|
-
)
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
406
|
-
// display_response — render the model's message as an assistant-style
|
|
407
|
-
// markdown bubble (NOT a tool card). The tool body is the final
|
|
408
|
-
// user-facing reply.
|
|
409
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
410
|
-
|
|
411
|
-
const renderDisplayResponse = (ctx: ToolRendererContext): ReactNode | null => {
|
|
412
|
-
// The tool echoes `{ response }`. We accept the response from either the
|
|
413
|
-
// result payload or the original tool-call input (so streaming UI that
|
|
414
|
-
// sees the call before the result still renders a bubble).
|
|
415
|
-
const message =
|
|
416
|
-
(isRecord(ctx.result) && asString(ctx.result.response)) ||
|
|
417
|
-
(isRecord(ctx.raw) && isRecord((ctx.raw as { input?: unknown }).input) &&
|
|
418
|
-
asString(((ctx.raw as { input: Record<string, unknown> }).input).response))
|
|
419
|
-
if (!message) return null
|
|
420
|
-
return (
|
|
421
|
-
<div className="msg assistant">
|
|
422
|
-
<div className="role">assistant</div>
|
|
423
|
-
<div className="body" dangerouslySetInnerHTML={{ __html: renderMarkdown(message) }} />
|
|
424
|
-
</div>
|
|
425
|
-
)
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
429
|
-
// Registry
|
|
430
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
431
|
-
|
|
432
|
-
const byName = (
|
|
433
|
-
names: string[],
|
|
434
|
-
render: (ctx: ToolRendererContext) => ReactNode | null,
|
|
435
|
-
): ToolRenderer => ({
|
|
436
|
-
match: (toolName) => names.includes(toolName),
|
|
437
|
-
render,
|
|
438
|
-
})
|
|
439
|
-
|
|
440
|
-
/**
|
|
441
|
-
* Default friendly renderers for the built-in tool catalog. Imported and
|
|
442
|
-
* applied automatically by [`TinywebChat`](../TinywebChat.tsx) — user-supplied
|
|
443
|
-
* `toolRenderers` are tried *first* so apps can override any of these by
|
|
444
|
-
* matching on the same tool name.
|
|
445
|
-
*/
|
|
446
|
-
export const defaultToolRenderers: ToolRenderer[] = [
|
|
447
|
-
byName(["time_now"], renderTimeNow),
|
|
448
|
-
byName(["weather"], renderWeather),
|
|
449
|
-
byName(["crypto_price"], renderCryptoPrice),
|
|
450
|
-
byName(["vn_gold_price"], renderVnGold),
|
|
451
|
-
byName(["vn_lottery"], renderVnLottery),
|
|
452
|
-
byName(["perplexity_search"], renderPerplexity),
|
|
453
|
-
byName(["web_search"], renderWebSearch),
|
|
454
|
-
byName(["web_fetch"], renderWebFetch),
|
|
455
|
-
byName(["display_response"], renderDisplayResponse),
|
|
456
|
-
]
|