langwatch 0.0.1

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.
Files changed (132) hide show
  1. package/.eslintrc.cjs +37 -0
  2. package/README.md +3 -0
  3. package/dist/chunk-GOA2HL4A.mjs +269 -0
  4. package/dist/chunk-GOA2HL4A.mjs.map +1 -0
  5. package/dist/index.d.mts +82 -0
  6. package/dist/index.d.ts +82 -0
  7. package/dist/index.js +940 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/index.mjs +666 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/utils-s3gGR6vj.d.mts +209 -0
  12. package/dist/utils-s3gGR6vj.d.ts +209 -0
  13. package/dist/utils.d.mts +3 -0
  14. package/dist/utils.d.ts +3 -0
  15. package/dist/utils.js +263 -0
  16. package/dist/utils.js.map +1 -0
  17. package/dist/utils.mjs +7 -0
  18. package/dist/utils.mjs.map +1 -0
  19. package/example/.env.example +12 -0
  20. package/example/.eslintrc.json +26 -0
  21. package/example/LICENSE +13 -0
  22. package/example/README.md +10 -0
  23. package/example/app/(chat)/chat/[id]/page.tsx +60 -0
  24. package/example/app/(chat)/layout.tsx +14 -0
  25. package/example/app/(chat)/page.tsx +22 -0
  26. package/example/app/actions.ts +156 -0
  27. package/example/app/globals.css +76 -0
  28. package/example/app/layout.tsx +64 -0
  29. package/example/app/login/actions.ts +71 -0
  30. package/example/app/login/page.tsx +18 -0
  31. package/example/app/new/page.tsx +5 -0
  32. package/example/app/opengraph-image.png +0 -0
  33. package/example/app/share/[id]/page.tsx +58 -0
  34. package/example/app/signup/actions.ts +111 -0
  35. package/example/app/signup/page.tsx +18 -0
  36. package/example/app/twitter-image.png +0 -0
  37. package/example/auth.config.ts +42 -0
  38. package/example/auth.ts +45 -0
  39. package/example/components/button-scroll-to-bottom.tsx +36 -0
  40. package/example/components/chat-history.tsx +49 -0
  41. package/example/components/chat-list.tsx +52 -0
  42. package/example/components/chat-message-actions.tsx +40 -0
  43. package/example/components/chat-message.tsx +80 -0
  44. package/example/components/chat-panel.tsx +139 -0
  45. package/example/components/chat-share-dialog.tsx +95 -0
  46. package/example/components/chat.tsx +84 -0
  47. package/example/components/clear-history.tsx +75 -0
  48. package/example/components/empty-screen.tsx +38 -0
  49. package/example/components/external-link.tsx +29 -0
  50. package/example/components/footer.tsx +19 -0
  51. package/example/components/header.tsx +80 -0
  52. package/example/components/login-button.tsx +42 -0
  53. package/example/components/login-form.tsx +97 -0
  54. package/example/components/markdown.tsx +9 -0
  55. package/example/components/prompt-form.tsx +115 -0
  56. package/example/components/providers.tsx +17 -0
  57. package/example/components/sidebar-actions.tsx +125 -0
  58. package/example/components/sidebar-desktop.tsx +19 -0
  59. package/example/components/sidebar-footer.tsx +16 -0
  60. package/example/components/sidebar-item.tsx +124 -0
  61. package/example/components/sidebar-items.tsx +42 -0
  62. package/example/components/sidebar-list.tsx +38 -0
  63. package/example/components/sidebar-mobile.tsx +31 -0
  64. package/example/components/sidebar-toggle.tsx +24 -0
  65. package/example/components/sidebar.tsx +21 -0
  66. package/example/components/signup-form.tsx +95 -0
  67. package/example/components/stocks/events-skeleton.tsx +31 -0
  68. package/example/components/stocks/events.tsx +30 -0
  69. package/example/components/stocks/index.tsx +36 -0
  70. package/example/components/stocks/message.tsx +134 -0
  71. package/example/components/stocks/spinner.tsx +16 -0
  72. package/example/components/stocks/stock-purchase.tsx +146 -0
  73. package/example/components/stocks/stock-skeleton.tsx +22 -0
  74. package/example/components/stocks/stock.tsx +210 -0
  75. package/example/components/stocks/stocks-skeleton.tsx +9 -0
  76. package/example/components/stocks/stocks.tsx +67 -0
  77. package/example/components/tailwind-indicator.tsx +14 -0
  78. package/example/components/theme-toggle.tsx +31 -0
  79. package/example/components/ui/alert-dialog.tsx +141 -0
  80. package/example/components/ui/badge.tsx +36 -0
  81. package/example/components/ui/button.tsx +57 -0
  82. package/example/components/ui/codeblock.tsx +148 -0
  83. package/example/components/ui/dialog.tsx +122 -0
  84. package/example/components/ui/dropdown-menu.tsx +205 -0
  85. package/example/components/ui/icons.tsx +507 -0
  86. package/example/components/ui/input.tsx +25 -0
  87. package/example/components/ui/label.tsx +26 -0
  88. package/example/components/ui/select.tsx +164 -0
  89. package/example/components/ui/separator.tsx +31 -0
  90. package/example/components/ui/sheet.tsx +140 -0
  91. package/example/components/ui/sonner.tsx +31 -0
  92. package/example/components/ui/switch.tsx +29 -0
  93. package/example/components/ui/textarea.tsx +24 -0
  94. package/example/components/ui/tooltip.tsx +30 -0
  95. package/example/components/user-menu.tsx +53 -0
  96. package/example/components.json +17 -0
  97. package/example/lib/chat/actions.tsx +606 -0
  98. package/example/lib/hooks/use-copy-to-clipboard.tsx +33 -0
  99. package/example/lib/hooks/use-enter-submit.tsx +23 -0
  100. package/example/lib/hooks/use-local-storage.ts +24 -0
  101. package/example/lib/hooks/use-scroll-anchor.tsx +86 -0
  102. package/example/lib/hooks/use-sidebar.tsx +60 -0
  103. package/example/lib/hooks/use-streamable-text.ts +25 -0
  104. package/example/lib/types.ts +41 -0
  105. package/example/lib/utils.ts +89 -0
  106. package/example/middleware.ts +8 -0
  107. package/example/next-env.d.ts +5 -0
  108. package/example/next.config.js +13 -0
  109. package/example/package-lock.json +9249 -0
  110. package/example/package.json +77 -0
  111. package/example/pnpm-lock.yaml +5712 -0
  112. package/example/postcss.config.js +6 -0
  113. package/example/prettier.config.cjs +34 -0
  114. package/example/public/apple-touch-icon.png +0 -0
  115. package/example/public/favicon-16x16.png +0 -0
  116. package/example/public/favicon.ico +0 -0
  117. package/example/public/next.svg +1 -0
  118. package/example/public/thirteen.svg +1 -0
  119. package/example/public/vercel.svg +1 -0
  120. package/example/tailwind.config.ts +81 -0
  121. package/example/tsconfig.json +35 -0
  122. package/package.json +45 -0
  123. package/src/helpers.ts +64 -0
  124. package/src/index.test.ts +255 -0
  125. package/src/index.ts +397 -0
  126. package/src/server/types/.gitkeep +0 -0
  127. package/src/types.ts +69 -0
  128. package/src/utils.ts +134 -0
  129. package/ts-to-zod.config.js +18 -0
  130. package/tsconfig.json +32 -0
  131. package/tsup.config.ts +10 -0
  132. package/vitest.config.ts +8 -0
@@ -0,0 +1,606 @@
1
+ import 'server-only'
2
+
3
+ import {
4
+ createAI,
5
+ createStreamableUI,
6
+ getMutableAIState,
7
+ getAIState,
8
+ streamUI,
9
+ createStreamableValue
10
+ } from 'ai/rsc'
11
+ import { openai } from '@ai-sdk/openai'
12
+
13
+ import {
14
+ spinner,
15
+ BotCard,
16
+ BotMessage,
17
+ SystemMessage,
18
+ Stock,
19
+ Purchase
20
+ } from '@/components/stocks'
21
+
22
+ import { z } from 'zod'
23
+ import { EventsSkeleton } from '@/components/stocks/events-skeleton'
24
+ import { Events } from '@/components/stocks/events'
25
+ import { StocksSkeleton } from '@/components/stocks/stocks-skeleton'
26
+ import { Stocks } from '@/components/stocks/stocks'
27
+ import { StockSkeleton } from '@/components/stocks/stock-skeleton'
28
+ import {
29
+ formatNumber,
30
+ runAsyncFnWithoutBlocking,
31
+ sleep,
32
+ nanoid
33
+ } from '@/lib/utils'
34
+ import { saveChat } from '@/app/actions'
35
+ import { SpinnerMessage, UserMessage } from '@/components/stocks/message'
36
+ import { Chat, Message } from '@/lib/types'
37
+ import { auth } from '@/auth'
38
+ import { LangWatch, convertFromVercelAIMessages } from 'langwatch'
39
+
40
+ async function confirmPurchase(symbol: string, price: number, amount: number) {
41
+ 'use server'
42
+
43
+ const aiState = getMutableAIState<typeof AI>()
44
+
45
+ const purchasing = createStreamableUI(
46
+ <div className="inline-flex items-start gap-1 md:items-center">
47
+ {spinner}
48
+ <p className="mb-2">
49
+ Purchasing {amount} ${symbol}...
50
+ </p>
51
+ </div>
52
+ )
53
+
54
+ const systemMessage = createStreamableUI(null)
55
+
56
+ runAsyncFnWithoutBlocking(async () => {
57
+ await sleep(1000)
58
+
59
+ purchasing.update(
60
+ <div className="inline-flex items-start gap-1 md:items-center">
61
+ {spinner}
62
+ <p className="mb-2">
63
+ Purchasing {amount} ${symbol}... working on it...
64
+ </p>
65
+ </div>
66
+ )
67
+
68
+ await sleep(1000)
69
+
70
+ purchasing.done(
71
+ <div>
72
+ <p className="mb-2">
73
+ You have successfully purchased {amount} ${symbol}. Total cost:{' '}
74
+ {formatNumber(amount * price)}
75
+ </p>
76
+ </div>
77
+ )
78
+
79
+ systemMessage.done(
80
+ <SystemMessage>
81
+ You have purchased {amount} shares of {symbol} at ${price}. Total cost ={' '}
82
+ {formatNumber(amount * price)}.
83
+ </SystemMessage>
84
+ )
85
+
86
+ aiState.done({
87
+ ...aiState.get(),
88
+ messages: [
89
+ ...aiState.get().messages,
90
+ {
91
+ id: nanoid(),
92
+ role: 'system',
93
+ content: `[User has purchased ${amount} shares of ${symbol} at ${price}. Total cost = ${
94
+ amount * price
95
+ }]`
96
+ }
97
+ ]
98
+ })
99
+ })
100
+
101
+ return {
102
+ purchasingUI: purchasing.value,
103
+ newMessage: {
104
+ id: nanoid(),
105
+ display: systemMessage.value
106
+ }
107
+ }
108
+ }
109
+
110
+ async function submitUserMessage(content: string) {
111
+ 'use server'
112
+
113
+ const langwatch = new LangWatch()
114
+ langwatch.on('error', e => {
115
+ console.log('Error from LangWatch:', e)
116
+ })
117
+
118
+ const trace = langwatch.getTrace()
119
+
120
+ const aiState = getMutableAIState<typeof AI>()
121
+
122
+ aiState.update({
123
+ ...aiState.get(),
124
+ messages: [
125
+ ...aiState.get().messages,
126
+ {
127
+ id: nanoid(),
128
+ role: 'user',
129
+ content
130
+ }
131
+ ]
132
+ })
133
+
134
+ const system = `\
135
+ You are a stock trading conversation bot and you can help users buy stocks, step by step.
136
+ You and the user can discuss stock prices and the user can adjust the amount of stocks they want to buy, or place an order, in the UI.
137
+
138
+ Messages inside [] means that it's a UI element or a user event. For example:
139
+ - "[Price of AAPL = 100]" means that an interface of the stock price of AAPL is shown to the user.
140
+ - "[User has changed the amount of AAPL to 10]" means that the user has changed the amount of AAPL to 10 in the UI.
141
+
142
+ If the user requests purchasing a stock, call \`show_stock_purchase_ui\` to show the purchase UI.
143
+ If the user just wants the price, call \`show_stock_price\` to show the price.
144
+ If you want to show trending stocks, call \`list_stocks\`.
145
+ If you want to show events, call \`get_events\`.
146
+ If the user wants to sell stock, or complete another impossible task, respond that you are a demo and cannot do that.
147
+
148
+ Besides that, you can also chat with users and do some calculations if needed.`
149
+
150
+ const span = trace.startLLMSpan({
151
+ model: 'gpt-3.5-turbo',
152
+ input: {
153
+ type: 'chat_messages',
154
+ value: [
155
+ {
156
+ role: 'system',
157
+ content: system
158
+ },
159
+ ...convertFromVercelAIMessages(aiState.get().messages)
160
+ ]
161
+ }
162
+ })
163
+
164
+ const onFinish = (output: Message[]) => {
165
+ aiState.done({
166
+ ...aiState.get(),
167
+ messages: [...aiState.get().messages, ...output]
168
+ })
169
+
170
+ span.end({
171
+ outputs: [
172
+ {
173
+ type: 'chat_messages',
174
+ value: convertFromVercelAIMessages(output)
175
+ }
176
+ ]
177
+ })
178
+ }
179
+
180
+ let textStream: undefined | ReturnType<typeof createStreamableValue<string>>
181
+ let textNode: undefined | React.ReactNode
182
+
183
+ const result = await streamUI({
184
+ model: openai('gpt-3.5-turbo'),
185
+ initial: <SpinnerMessage />,
186
+ system,
187
+ messages: [
188
+ ...aiState.get().messages.map((message: any) => ({
189
+ role: message.role,
190
+ content: message.content,
191
+ name: message.name
192
+ }))
193
+ ],
194
+ text: ({ content, done, delta }) => {
195
+ if (!textStream) {
196
+ textStream = createStreamableValue('')
197
+ textNode = <BotMessage content={textStream.value} />
198
+ }
199
+
200
+ if (done) {
201
+ textStream.done()
202
+
203
+ onFinish([
204
+ {
205
+ id: nanoid(),
206
+ role: 'assistant',
207
+ content
208
+ }
209
+ ])
210
+ } else {
211
+ textStream.update(delta)
212
+ }
213
+
214
+ return textNode
215
+ },
216
+ tools: {
217
+ listStocks: {
218
+ description: 'List three imaginary stocks that are trending.',
219
+ parameters: z.object({
220
+ stocks: z.array(
221
+ z.object({
222
+ symbol: z.string().describe('The symbol of the stock'),
223
+ price: z.number().describe('The price of the stock'),
224
+ delta: z.number().describe('The change in price of the stock')
225
+ })
226
+ )
227
+ }),
228
+ generate: async function* ({ stocks }) {
229
+ yield (
230
+ <BotCard>
231
+ <StocksSkeleton />
232
+ </BotCard>
233
+ )
234
+
235
+ await sleep(1000)
236
+
237
+ const toolCallId = nanoid()
238
+
239
+ onFinish([
240
+ {
241
+ id: nanoid(),
242
+ role: 'assistant',
243
+ content: [
244
+ {
245
+ type: 'tool-call',
246
+ toolName: 'listStocks',
247
+ toolCallId,
248
+ args: { stocks }
249
+ }
250
+ ]
251
+ },
252
+ {
253
+ id: nanoid(),
254
+ role: 'tool',
255
+ content: [
256
+ {
257
+ type: 'tool-result',
258
+ toolName: 'listStocks',
259
+ toolCallId,
260
+ result: stocks
261
+ }
262
+ ]
263
+ }
264
+ ])
265
+
266
+ return (
267
+ <BotCard>
268
+ <Stocks props={stocks} />
269
+ </BotCard>
270
+ )
271
+ }
272
+ },
273
+ showStockPrice: {
274
+ description:
275
+ 'Get the current stock price of a given stock or currency. Use this to show the price to the user.',
276
+ parameters: z.object({
277
+ symbol: z
278
+ .string()
279
+ .describe(
280
+ 'The name or symbol of the stock or currency. e.g. DOGE/AAPL/USD.'
281
+ ),
282
+ price: z.number().describe('The price of the stock.'),
283
+ delta: z.number().describe('The change in price of the stock')
284
+ }),
285
+ generate: async function* ({ symbol, price, delta }) {
286
+ yield (
287
+ <BotCard>
288
+ <StockSkeleton />
289
+ </BotCard>
290
+ )
291
+
292
+ await sleep(1000)
293
+
294
+ const toolCallId = nanoid()
295
+
296
+ onFinish([
297
+ {
298
+ id: nanoid(),
299
+ role: 'assistant',
300
+ content: [
301
+ {
302
+ type: 'tool-call',
303
+ toolName: 'showStockPrice',
304
+ toolCallId,
305
+ args: { symbol, price, delta }
306
+ }
307
+ ]
308
+ },
309
+ {
310
+ id: nanoid(),
311
+ role: 'tool',
312
+ content: [
313
+ {
314
+ type: 'tool-result',
315
+ toolName: 'showStockPrice',
316
+ toolCallId,
317
+ result: { symbol, price, delta }
318
+ }
319
+ ]
320
+ }
321
+ ])
322
+
323
+ return (
324
+ <BotCard>
325
+ <Stock props={{ symbol, price, delta }} />
326
+ </BotCard>
327
+ )
328
+ }
329
+ },
330
+ showStockPurchase: {
331
+ description:
332
+ 'Show price and the UI to purchase a stock or currency. Use this if the user wants to purchase a stock or currency.',
333
+ parameters: z.object({
334
+ symbol: z
335
+ .string()
336
+ .describe(
337
+ 'The name or symbol of the stock or currency. e.g. DOGE/AAPL/USD.'
338
+ ),
339
+ price: z.number().describe('The price of the stock.'),
340
+ numberOfShares: z
341
+ .number()
342
+ .describe(
343
+ 'The **number of shares** for a stock or currency to purchase. Can be optional if the user did not specify it.'
344
+ )
345
+ }),
346
+ generate: async function* ({ symbol, price, numberOfShares = 100 }) {
347
+ const toolCallId = nanoid()
348
+
349
+ if (numberOfShares <= 0 || numberOfShares > 1000) {
350
+ onFinish([
351
+ {
352
+ id: nanoid(),
353
+ role: 'assistant',
354
+ content: [
355
+ {
356
+ type: 'tool-call',
357
+ toolName: 'showStockPurchase',
358
+ toolCallId,
359
+ args: { symbol, price, numberOfShares }
360
+ }
361
+ ]
362
+ },
363
+ {
364
+ id: nanoid(),
365
+ role: 'tool',
366
+ content: [
367
+ {
368
+ type: 'tool-result',
369
+ toolName: 'showStockPurchase',
370
+ toolCallId,
371
+ result: {
372
+ symbol,
373
+ price,
374
+ numberOfShares,
375
+ status: 'expired'
376
+ }
377
+ }
378
+ ]
379
+ },
380
+ {
381
+ id: nanoid(),
382
+ role: 'system',
383
+ content: `[User has selected an invalid amount]`
384
+ }
385
+ ])
386
+
387
+ return <BotMessage content={'Invalid amount'} />
388
+ } else {
389
+ onFinish([
390
+ {
391
+ id: nanoid(),
392
+ role: 'assistant',
393
+ content: [
394
+ {
395
+ type: 'tool-call',
396
+ toolName: 'showStockPurchase',
397
+ toolCallId,
398
+ args: { symbol, price, numberOfShares }
399
+ }
400
+ ]
401
+ },
402
+ {
403
+ id: nanoid(),
404
+ role: 'tool',
405
+ content: [
406
+ {
407
+ type: 'tool-result',
408
+ toolName: 'showStockPurchase',
409
+ toolCallId,
410
+ result: {
411
+ symbol,
412
+ price,
413
+ numberOfShares
414
+ }
415
+ }
416
+ ]
417
+ }
418
+ ])
419
+
420
+ return (
421
+ <BotCard>
422
+ <Purchase
423
+ props={{
424
+ numberOfShares,
425
+ symbol,
426
+ price: +price,
427
+ status: 'requires_action'
428
+ }}
429
+ />
430
+ </BotCard>
431
+ )
432
+ }
433
+ }
434
+ },
435
+ getEvents: {
436
+ description:
437
+ 'List funny imaginary events between user highlighted dates that describe stock activity.',
438
+ parameters: z.object({
439
+ events: z.array(
440
+ z.object({
441
+ date: z
442
+ .string()
443
+ .describe('The date of the event, in ISO-8601 format'),
444
+ headline: z.string().describe('The headline of the event'),
445
+ description: z.string().describe('The description of the event')
446
+ })
447
+ )
448
+ }),
449
+ generate: async function* ({ events }) {
450
+ yield (
451
+ <BotCard>
452
+ <EventsSkeleton />
453
+ </BotCard>
454
+ )
455
+
456
+ await sleep(1000)
457
+
458
+ const toolCallId = nanoid()
459
+
460
+ onFinish([
461
+ {
462
+ id: nanoid(),
463
+ role: 'assistant',
464
+ content: [
465
+ {
466
+ type: 'tool-call',
467
+ toolName: 'getEvents',
468
+ toolCallId,
469
+ args: { events }
470
+ }
471
+ ]
472
+ },
473
+ {
474
+ id: nanoid(),
475
+ role: 'tool',
476
+ content: [
477
+ {
478
+ type: 'tool-result',
479
+ toolName: 'getEvents',
480
+ toolCallId,
481
+ result: events
482
+ }
483
+ ]
484
+ }
485
+ ])
486
+
487
+ return (
488
+ <BotCard>
489
+ <Events props={events} />
490
+ </BotCard>
491
+ )
492
+ }
493
+ }
494
+ }
495
+ })
496
+
497
+ return {
498
+ id: nanoid(),
499
+ display: result.value
500
+ }
501
+ }
502
+
503
+ export type AIState = {
504
+ chatId: string
505
+ messages: Message[]
506
+ }
507
+
508
+ export type UIState = {
509
+ id: string
510
+ display: React.ReactNode
511
+ }[]
512
+
513
+ export const AI = createAI<AIState, UIState>({
514
+ actions: {
515
+ submitUserMessage,
516
+ confirmPurchase
517
+ },
518
+ initialUIState: [],
519
+ initialAIState: { chatId: nanoid(), messages: [] },
520
+ onGetUIState: async () => {
521
+ 'use server'
522
+
523
+ const session = await auth()
524
+
525
+ if (session && session.user) {
526
+ const aiState = getAIState()
527
+
528
+ if (aiState) {
529
+ // @ts-ignore
530
+ const uiState = getUIStateFromAIState(aiState)
531
+ return uiState
532
+ }
533
+ } else {
534
+ return
535
+ }
536
+ },
537
+ onSetAIState: async ({ state }) => {
538
+ 'use server'
539
+
540
+ const session = await auth()
541
+
542
+ if (session && session.user) {
543
+ const { chatId, messages } = state
544
+
545
+ const createdAt = new Date()
546
+ const userId = session.user.id as string
547
+ const path = `/chat/${chatId}`
548
+
549
+ const firstMessageContent = messages[0].content as string
550
+ const title = firstMessageContent.substring(0, 100)
551
+
552
+ const chat: Chat = {
553
+ id: chatId,
554
+ title,
555
+ userId,
556
+ createdAt,
557
+ messages,
558
+ path
559
+ }
560
+
561
+ await saveChat(chat)
562
+ } else {
563
+ return
564
+ }
565
+ }
566
+ })
567
+
568
+ export const getUIStateFromAIState = (aiState: Chat) => {
569
+ return aiState.messages
570
+ .filter(message => message.role !== 'system')
571
+ .map((message, index) => ({
572
+ id: `${aiState.chatId}-${index}`,
573
+ display:
574
+ message.role === 'tool' ? (
575
+ message.content.map(tool => {
576
+ return tool.toolName === 'listStocks' ? (
577
+ <BotCard>
578
+ {/* TODO: Infer types based on the tool result*/}
579
+ {/* @ts-expect-error */}
580
+ <Stocks props={tool.result} />
581
+ </BotCard>
582
+ ) : tool.toolName === 'showStockPrice' ? (
583
+ <BotCard>
584
+ {/* @ts-expect-error */}
585
+ <Stock props={tool.result} />
586
+ </BotCard>
587
+ ) : tool.toolName === 'showStockPurchase' ? (
588
+ <BotCard>
589
+ {/* @ts-expect-error */}
590
+ <Purchase props={tool.result} />
591
+ </BotCard>
592
+ ) : tool.toolName === 'getEvents' ? (
593
+ <BotCard>
594
+ {/* @ts-expect-error */}
595
+ <Events props={tool.result} />
596
+ </BotCard>
597
+ ) : null
598
+ })
599
+ ) : message.role === 'user' ? (
600
+ <UserMessage>{message.content as string}</UserMessage>
601
+ ) : message.role === 'assistant' &&
602
+ typeof message.content === 'string' ? (
603
+ <BotMessage content={message.content} />
604
+ ) : null
605
+ }))
606
+ }
@@ -0,0 +1,33 @@
1
+ 'use client'
2
+
3
+ import * as React from 'react'
4
+
5
+ export interface useCopyToClipboardProps {
6
+ timeout?: number
7
+ }
8
+
9
+ export function useCopyToClipboard({
10
+ timeout = 2000
11
+ }: useCopyToClipboardProps) {
12
+ const [isCopied, setIsCopied] = React.useState<Boolean>(false)
13
+
14
+ const copyToClipboard = (value: string) => {
15
+ if (typeof window === 'undefined' || !navigator.clipboard?.writeText) {
16
+ return
17
+ }
18
+
19
+ if (!value) {
20
+ return
21
+ }
22
+
23
+ navigator.clipboard.writeText(value).then(() => {
24
+ setIsCopied(true)
25
+
26
+ setTimeout(() => {
27
+ setIsCopied(false)
28
+ }, timeout)
29
+ })
30
+ }
31
+
32
+ return { isCopied, copyToClipboard }
33
+ }
@@ -0,0 +1,23 @@
1
+ import { useRef, type RefObject } from 'react'
2
+
3
+ export function useEnterSubmit(): {
4
+ formRef: RefObject<HTMLFormElement>
5
+ onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
6
+ } {
7
+ const formRef = useRef<HTMLFormElement>(null)
8
+
9
+ const handleKeyDown = (
10
+ event: React.KeyboardEvent<HTMLTextAreaElement>
11
+ ): void => {
12
+ if (
13
+ event.key === 'Enter' &&
14
+ !event.shiftKey &&
15
+ !event.nativeEvent.isComposing
16
+ ) {
17
+ formRef.current?.requestSubmit()
18
+ event.preventDefault()
19
+ }
20
+ }
21
+
22
+ return { formRef, onKeyDown: handleKeyDown }
23
+ }
@@ -0,0 +1,24 @@
1
+ import { useEffect, useState } from 'react'
2
+
3
+ export const useLocalStorage = <T>(
4
+ key: string,
5
+ initialValue: T
6
+ ): [T, (value: T) => void] => {
7
+ const [storedValue, setStoredValue] = useState(initialValue)
8
+
9
+ useEffect(() => {
10
+ // Retrieve from localStorage
11
+ const item = window.localStorage.getItem(key)
12
+ if (item) {
13
+ setStoredValue(JSON.parse(item))
14
+ }
15
+ }, [key])
16
+
17
+ const setValue = (value: T) => {
18
+ // Save state
19
+ setStoredValue(value)
20
+ // Save to localStorage
21
+ window.localStorage.setItem(key, JSON.stringify(value))
22
+ }
23
+ return [storedValue, setValue]
24
+ }