@swisper.ai/web-chat-ui 0.3.1 → 0.3.3

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 (29) hide show
  1. package/README.md +2 -1
  2. package/dist/components/chat-reasoning/chat-reasoning.d.ts +194 -0
  3. package/dist/components/chat-reasoning/chat-reasoning.d.ts.map +1 -0
  4. package/dist/components/chat-reasoning/chat-reasoning.stories.d.ts +38 -0
  5. package/dist/components/chat-reasoning/chat-reasoning.stories.d.ts.map +1 -0
  6. package/dist/components/chat-reasoning/index.d.ts +2 -0
  7. package/dist/components/chat-reasoning/index.d.ts.map +1 -0
  8. package/dist/components/chat-thinking-process/chat-thinking-process.d.ts +34 -6
  9. package/dist/components/chat-thinking-process/chat-thinking-process.d.ts.map +1 -1
  10. package/dist/components/index.d.ts +1 -0
  11. package/dist/components/index.d.ts.map +1 -1
  12. package/dist/docs/agent.stories.d.ts.map +1 -1
  13. package/dist/docs/chatbot-panel.d.ts.map +1 -1
  14. package/dist/index.css +1 -1
  15. package/dist/index.es.js +1816 -1359
  16. package/dist/index.es.js.map +1 -1
  17. package/package.json +4 -4
  18. package/src/components/chat-message/__stories__/tool-steps.tsx +44 -23
  19. package/src/components/chat-reasoning/__stories__/fixtures.ts +144 -0
  20. package/src/components/chat-reasoning/chat-reasoning.json +241 -0
  21. package/src/components/chat-reasoning/chat-reasoning.stories.tsx +462 -0
  22. package/src/components/chat-reasoning/chat-reasoning.tsx +1042 -0
  23. package/src/components/chat-reasoning/index.ts +51 -0
  24. package/src/components/chat-thinking-process/chat-thinking-process.json +2 -0
  25. package/src/components/chat-thinking-process/chat-thinking-process.tsx +34 -6
  26. package/src/components/index.ts +1 -0
  27. package/src/docs/agent.stories.tsx +29 -29
  28. package/src/docs/chatbot-panel.tsx +48 -25
  29. package/src/docs/chatbot.mdx +5 -5
@@ -0,0 +1,462 @@
1
+ import type { Meta, StoryObj } from '@storybook/react-vite';
2
+ import { useEffect, useState } from 'react';
3
+ import { fn } from 'storybook/test';
4
+
5
+ import { CheckIcon, SbbIcon, StopIcon, WebsiteIcon } from '@swisper.ai/icons';
6
+ import { Button, VisuallyHidden } from '@swisper.ai/ui';
7
+ import { ImagePlaceholder } from '@swisper.ai/ui/storybook';
8
+
9
+ import { ParentBlock } from '../../__storybook__/parent-block';
10
+ import { ChatMessage, ChatMessageContent } from '../chat-message';
11
+
12
+ import {
13
+ LUCERNE_ANSWER,
14
+ LUCERNE_PAGES,
15
+ LUCERNE_REQUEST,
16
+ LUCERNE_STEPS,
17
+ LUCERNE_SUMMARY,
18
+ PARALLEL_STEPS,
19
+ type ReasoningPage,
20
+ type ReasoningStepFixture,
21
+ } from './__stories__/fixtures';
22
+ import {
23
+ ChatReasoning,
24
+ ChatReasoningEnd,
25
+ ChatReasoningEndIcon,
26
+ ChatReasoningEndText,
27
+ ChatReasoningStep,
28
+ ChatReasoningStepHead,
29
+ ChatReasoningStepIcon,
30
+ ChatReasoningStepIconDot,
31
+ ChatReasoningStepIconLogo,
32
+ ChatReasoningStepIconWeb,
33
+ ChatReasoningStepNote,
34
+ ChatReasoningStepNotePageDomain,
35
+ ChatReasoningStepNotePageIcon,
36
+ ChatReasoningStepNotePageRow,
37
+ ChatReasoningStepNotePages,
38
+ ChatReasoningStepNotePageTitle,
39
+ ChatReasoningStepNotes,
40
+ ChatReasoningSteps,
41
+ ChatReasoningStepText,
42
+ ChatReasoningTrigger,
43
+ ChatReasoningTriggerArrow,
44
+ ChatReasoningTriggerLabel,
45
+ ChatReasoningTriggerSymbol,
46
+ ChatReasoningTriggerSymbolSwisper,
47
+ type ChatReasoningProps,
48
+ type ChatReasoningStatus,
49
+ } from './chat-reasoning';
50
+
51
+ const meta: Meta<typeof ChatReasoning> = {
52
+ title: 'Chat/ChatReasoning',
53
+ component: ChatReasoning,
54
+ subcomponents: {
55
+ ChatReasoningTrigger,
56
+ ChatReasoningTriggerSymbol,
57
+ ChatReasoningTriggerSymbolSwisper,
58
+ ChatReasoningTriggerLabel,
59
+ ChatReasoningTriggerArrow,
60
+ ChatReasoningSteps,
61
+ ChatReasoningStep,
62
+ ChatReasoningStepHead,
63
+ ChatReasoningStepIcon,
64
+ ChatReasoningStepIconDot,
65
+ ChatReasoningStepIconLogo,
66
+ ChatReasoningStepIconWeb,
67
+ ChatReasoningStepText,
68
+ ChatReasoningStepNotes,
69
+ ChatReasoningStepNote,
70
+ ChatReasoningStepNotePages,
71
+ ChatReasoningStepNotePageRow,
72
+ ChatReasoningStepNotePageIcon,
73
+ ChatReasoningStepNotePageTitle,
74
+ ChatReasoningStepNotePageDomain,
75
+ ChatReasoningEnd,
76
+ ChatReasoningEndIcon,
77
+ ChatReasoningEndText,
78
+ },
79
+ parameters: { layout: 'padded' },
80
+ tags: ['autodocs'],
81
+ decorators: [
82
+ (Story) => (
83
+ <div className="w-full max-w-182">
84
+ <Story />
85
+ </div>
86
+ ),
87
+ ],
88
+ argTypes: {
89
+ status: {
90
+ control: 'inline-radio',
91
+ options: ['running', 'done', 'stopped'],
92
+ description:
93
+ 'Where the work is. `running` turns the Swisper symbol and sweeps a light across the collapsed line.',
94
+ table: { defaultValue: { summary: 'done' } },
95
+ },
96
+ defaultOpen: {
97
+ control: 'boolean',
98
+ description: 'Whether the window starts open when uncontrolled.',
99
+ table: { defaultValue: { summary: 'false' } },
100
+ },
101
+ open: {
102
+ control: 'boolean',
103
+ description:
104
+ 'Controlled open state. Pair it with `onOpenChange`; open it with the second step and close it when the answer starts.',
105
+ },
106
+ onOpenChange: {
107
+ description: 'Called with the next open state when the row is clicked.',
108
+ },
109
+ children: {
110
+ control: false,
111
+ description:
112
+ 'A `ChatReasoningTrigger` (symbol, label, arrow), then `ChatReasoningSteps` with the steps and the end row.',
113
+ },
114
+ className: {
115
+ control: 'text',
116
+ description: 'Extra classes merged onto the root.',
117
+ },
118
+ },
119
+ args: {
120
+ status: 'done',
121
+ defaultOpen: false,
122
+ onOpenChange: fn(),
123
+ },
124
+ };
125
+
126
+ export default meta;
127
+ type Story = StoryObj<typeof ChatReasoning>;
128
+
129
+ const SOURCE_NAMES = { sbb: 'SBB', postfinance: 'PostFinance', web: 'Web search' } as const;
130
+
131
+ function StepMark({ step }: { step: ReasoningStepFixture }) {
132
+ if (step.source === 'swisper') {
133
+ return (
134
+ <ChatReasoningStepIcon>
135
+ <ChatReasoningStepIconDot />
136
+ </ChatReasoningStepIcon>
137
+ );
138
+ }
139
+
140
+ const label = <VisuallyHidden>{`${SOURCE_NAMES[step.source]}: `}</VisuallyHidden>;
141
+
142
+ if (step.source === 'web') {
143
+ return (
144
+ <ChatReasoningStepIcon>
145
+ <ChatReasoningStepIconWeb>
146
+ <WebsiteIcon aria-hidden />
147
+ </ChatReasoningStepIconWeb>
148
+ {label}
149
+ </ChatReasoningStepIcon>
150
+ );
151
+ }
152
+
153
+ return (
154
+ <ChatReasoningStepIcon>
155
+ <ChatReasoningStepIconLogo>
156
+ {step.source === 'sbb' ? (
157
+ <SbbIcon aria-hidden />
158
+ ) : (
159
+ <ImagePlaceholder className="size-16 rounded-xs [&_svg]:size-12" />
160
+ )}
161
+ </ChatReasoningStepIconLogo>
162
+ {label}
163
+ </ChatReasoningStepIcon>
164
+ );
165
+ }
166
+
167
+ function PageRow({ page }: { page: ReasoningPage }) {
168
+ return (
169
+ <ChatReasoningStepNotePageRow asChild>
170
+ <a href={page.href} target="_blank" rel="noopener noreferrer">
171
+ <ChatReasoningStepNotePageIcon>{page.letter}</ChatReasoningStepNotePageIcon>
172
+ <ChatReasoningStepNotePageTitle>{page.title}</ChatReasoningStepNotePageTitle>
173
+ <ChatReasoningStepNotePageDomain>{page.domain}</ChatReasoningStepNotePageDomain>
174
+ </a>
175
+ </ChatReasoningStepNotePageRow>
176
+ );
177
+ }
178
+
179
+ interface ReasoningStepViewProps {
180
+ step: ReasoningStepFixture;
181
+ isLive?: boolean;
182
+ }
183
+
184
+ function ReasoningStepView({ step, isLive = false }: ReasoningStepViewProps) {
185
+ const hasNotes = !isLive && (step.notes?.length || step.pages?.length);
186
+
187
+ return (
188
+ <ChatReasoningStep isLive={isLive}>
189
+ <ChatReasoningStepHead>
190
+ <StepMark step={step} />
191
+ <ChatReasoningStepText>{isLive ? step.say : step.did}</ChatReasoningStepText>
192
+ </ChatReasoningStepHead>
193
+ {hasNotes ? (
194
+ <ChatReasoningStepNotes>
195
+ {step.notes?.map((note) => (
196
+ <ChatReasoningStepNote key={note}>{note}</ChatReasoningStepNote>
197
+ ))}
198
+ {step.pages?.length ? (
199
+ <ChatReasoningStepNotePages>
200
+ {step.pages.map((page) => (
201
+ <PageRow key={page.href} page={page} />
202
+ ))}
203
+ </ChatReasoningStepNotePages>
204
+ ) : null}
205
+ </ChatReasoningStepNotes>
206
+ ) : null}
207
+ </ChatReasoningStep>
208
+ );
209
+ }
210
+
211
+ function TriggerRow({ status, label }: { status: ChatReasoningStatus; label: string }) {
212
+ return (
213
+ <ChatReasoningTrigger>
214
+ <ChatReasoningTriggerSymbol>
215
+ {status === 'stopped' ? <StopIcon /> : <ChatReasoningTriggerSymbolSwisper />}
216
+ </ChatReasoningTriggerSymbol>
217
+ <ChatReasoningTriggerLabel>{label}</ChatReasoningTriggerLabel>
218
+ <ChatReasoningTriggerArrow />
219
+ </ChatReasoningTrigger>
220
+ );
221
+ }
222
+
223
+ /** The finished reasoning above an answer: collapsed, saying what was checked and how long it took. */
224
+ export const Default: Story = {
225
+ render: (args) => (
226
+ <ChatReasoning {...args}>
227
+ <TriggerRow status="done" label={LUCERNE_SUMMARY} />
228
+ <ChatReasoningSteps>
229
+ {LUCERNE_STEPS.map((step) => (
230
+ <ReasoningStepView key={step.id} step={step} />
231
+ ))}
232
+ <ChatReasoningEnd>
233
+ <ChatReasoningEndIcon>
234
+ <CheckIcon />
235
+ </ChatReasoningEndIcon>
236
+ <ChatReasoningEndText>Done in 40 s</ChatReasoningEndText>
237
+ </ChatReasoningEnd>
238
+ </ChatReasoningSteps>
239
+ </ChatReasoning>
240
+ ),
241
+ };
242
+
243
+ /**
244
+ * Opened after the answer: every step with its mark, notes hanging under it as a tree, the pages
245
+ * Swisper read under the web step, and the end row.
246
+ */
247
+ export const DoneOpen: Story = {
248
+ ...Default,
249
+ args: { defaultOpen: true },
250
+ };
251
+
252
+ /** Still working, collapsed: the symbol turns and a light sweeps across the current step's line. */
253
+ export const Running: Story = {
254
+ args: { status: 'running' },
255
+ render: (args) => (
256
+ <ChatReasoning {...args}>
257
+ <TriggerRow status="running" label={LUCERNE_STEPS[5].say} />
258
+ </ChatReasoning>
259
+ ),
260
+ };
261
+
262
+ /**
263
+ * Tools running in parallel, open: the line says "Working on your request" and each live row gets
264
+ * the light sweep; live source marks pulse on their tile.
265
+ */
266
+ export const Parallel: Story = {
267
+ args: { status: 'running', defaultOpen: true },
268
+ render: (args) => (
269
+ <ChatReasoning {...args}>
270
+ <TriggerRow status="running" label="Working on your request" />
271
+ <ChatReasoningSteps>
272
+ {PARALLEL_STEPS.map((step, index) => (
273
+ <ReasoningStepView key={step.id} step={step} isLive={index >= 2} />
274
+ ))}
275
+ </ChatReasoningSteps>
276
+ </ChatReasoning>
277
+ ),
278
+ };
279
+
280
+ /** Stopped by the person: a stop square in place of the symbol, and the end row says so. */
281
+ export const Stopped: Story = {
282
+ args: { status: 'stopped', defaultOpen: true },
283
+ render: (args) => (
284
+ <ChatReasoning {...args}>
285
+ <TriggerRow status="stopped" label="Stopped after 12s" />
286
+ <ChatReasoningSteps>
287
+ {LUCERNE_STEPS.slice(0, 4).map((step) => (
288
+ <ReasoningStepView key={step.id} step={step} />
289
+ ))}
290
+ <ChatReasoningEnd>
291
+ <ChatReasoningEndIcon>
292
+ <StopIcon />
293
+ </ChatReasoningEndIcon>
294
+ <ChatReasoningEndText>Stopped</ChatReasoningEndText>
295
+ </ChatReasoningEnd>
296
+ </ChatReasoningSteps>
297
+ </ChatReasoning>
298
+ ),
299
+ };
300
+
301
+ /** A quick answer: only the row, from its first step to the done line. The window never opens. */
302
+ export const WithoutSteps: Story = {
303
+ render: (args) => (
304
+ <div className="flex flex-col gap-16">
305
+ <ChatReasoning {...args} status="running">
306
+ <TriggerRow status="running" label="Checking what Swisper already knows" />
307
+ </ChatReasoning>
308
+ <ChatReasoning {...args} status="done">
309
+ <TriggerRow status="done" label="Answered from Swisper’s own knowledge · 1s" />
310
+ </ChatReasoning>
311
+ </div>
312
+ ),
313
+ };
314
+
315
+ /**
316
+ * The pages Swisper read under a web step. Each row is the consumer's link through `asChild`: hover
317
+ * fills it, focus outlines it, and the domain turns blue.
318
+ */
319
+ export const PageRows: Story = {
320
+ args: { defaultOpen: true },
321
+ render: (args) => (
322
+ <ChatReasoning {...args}>
323
+ <TriggerRow status="done" label="Checked 4 sites · 6s" />
324
+ <ChatReasoningSteps>
325
+ <ReasoningStepView step={{ ...LUCERNE_STEPS[8], pages: LUCERNE_PAGES }} />
326
+ </ChatReasoningSteps>
327
+ </ChatReasoning>
328
+ ),
329
+ };
330
+
331
+ /** In a column narrower than 448px the row grows to 44px; the line still ends in an ellipsis. */
332
+ export const Narrow: Story = {
333
+ args: { defaultOpen: true },
334
+ decorators: [
335
+ (Story) => (
336
+ <div className="w-95">
337
+ <Story />
338
+ </div>
339
+ ),
340
+ ],
341
+ render: (args) => (
342
+ <ChatReasoning {...args}>
343
+ <TriggerRow status="done" label={LUCERNE_SUMMARY} />
344
+ <ChatReasoningSteps>
345
+ {LUCERNE_STEPS.slice(3, 6).map((step) => (
346
+ <ReasoningStepView key={step.id} step={step} />
347
+ ))}
348
+ </ChatReasoningSteps>
349
+ </ChatReasoning>
350
+ ),
351
+ };
352
+
353
+ const STEP_INTERVAL_MS = 1400;
354
+
355
+ /**
356
+ * The open and close rules, played: the row shows from the first step, the window opens with the
357
+ * second and closes when the answer starts. Rows ease in, and each line stays up at least a second.
358
+ */
359
+ export const Streaming: Story = {
360
+ args: { status: 'running' },
361
+ render: function StreamingRender({ onOpenChange }: Partial<ChatReasoningProps>) {
362
+ const [run, setRun] = useState(0);
363
+ const [count, setCount] = useState(1);
364
+ const [isOpen, setIsOpen] = useState(false);
365
+ const isDone = count > LUCERNE_STEPS.length;
366
+ const status: ChatReasoningStatus = isDone ? 'done' : 'running';
367
+ const visible = LUCERNE_STEPS.slice(0, count);
368
+
369
+ useEffect(() => {
370
+ setCount(1);
371
+ setIsOpen(false);
372
+ const timer = setInterval(() => {
373
+ setCount((current) => {
374
+ const next = current + 1;
375
+ if (next === 2) setIsOpen(true);
376
+ if (next > LUCERNE_STEPS.length) {
377
+ setIsOpen(false);
378
+ clearInterval(timer);
379
+ }
380
+ return next;
381
+ });
382
+ }, STEP_INTERVAL_MS);
383
+
384
+ return () => clearInterval(timer);
385
+ }, [run]);
386
+
387
+ let label = LUCERNE_STEPS[0].say;
388
+ if (isDone) label = LUCERNE_SUMMARY;
389
+ else if (isOpen) label = 'Working on your request';
390
+ else if (count > 1) label = LUCERNE_STEPS[count - 1].say;
391
+
392
+ return (
393
+ <div className="flex flex-col gap-16">
394
+ <ChatMessage>
395
+ <ChatReasoning
396
+ status={status}
397
+ open={isOpen}
398
+ onOpenChange={(open) => {
399
+ setIsOpen(open);
400
+ onOpenChange?.(open);
401
+ }}
402
+ >
403
+ <TriggerRow status={status} label={label} />
404
+ <ChatReasoningSteps>
405
+ {visible.map((step, index) => (
406
+ <ReasoningStepView
407
+ key={step.id}
408
+ step={step}
409
+ isLive={!isDone && index === visible.length - 1}
410
+ />
411
+ ))}
412
+ {isDone && (
413
+ <ChatReasoningEnd>
414
+ <ChatReasoningEndIcon>
415
+ <CheckIcon />
416
+ </ChatReasoningEndIcon>
417
+ <ChatReasoningEndText>Done in 40 s</ChatReasoningEndText>
418
+ </ChatReasoningEnd>
419
+ )}
420
+ </ChatReasoningSteps>
421
+ </ChatReasoning>
422
+ {isDone && <ChatMessageContent>{LUCERNE_ANSWER}</ChatMessageContent>}
423
+ </ChatMessage>
424
+ <div>
425
+ <Button size="sm" onClick={() => setRun((value) => value + 1)}>
426
+ Replay
427
+ </Button>
428
+ </div>
429
+ </div>
430
+ );
431
+ },
432
+ };
433
+
434
+ /** The reasoning where it's allowed: in an assistant message, above the answer. */
435
+ export const Examples: Story = {
436
+ render: (args) => (
437
+ <div className="flex flex-col gap-24">
438
+ <ParentBlock parent="web-chat-ui/ChatMessage">
439
+ <ChatMessage isQuestion>
440
+ <ChatMessageContent>{LUCERNE_REQUEST}</ChatMessageContent>
441
+ </ChatMessage>
442
+ <ChatMessage>
443
+ <ChatReasoning {...args}>
444
+ <TriggerRow status="done" label={LUCERNE_SUMMARY} />
445
+ <ChatReasoningSteps>
446
+ {LUCERNE_STEPS.map((step) => (
447
+ <ReasoningStepView key={step.id} step={step} />
448
+ ))}
449
+ <ChatReasoningEnd>
450
+ <ChatReasoningEndIcon>
451
+ <CheckIcon />
452
+ </ChatReasoningEndIcon>
453
+ <ChatReasoningEndText>Done in 40 s</ChatReasoningEndText>
454
+ </ChatReasoningEnd>
455
+ </ChatReasoningSteps>
456
+ </ChatReasoning>
457
+ <ChatMessageContent>{LUCERNE_ANSWER}</ChatMessageContent>
458
+ </ChatMessage>
459
+ </ParentBlock>
460
+ </div>
461
+ ),
462
+ };