@boldare/askdare 0.1.0 → 0.2.0-rc.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.
- package/CHANGELOG.md +31 -0
- package/README.md +10 -2
- package/dist/cli.js +286 -176
- package/dist/index.js +2 -0
- package/dist/next.d.ts +169 -4
- package/dist/next.js +395 -87
- package/dist/react-marked.js +1797 -0
- package/dist/react.d.ts +470 -0
- package/dist/react.js +2691 -0
- package/dist/store.d.ts +13 -0
- package/dist/ui/askdare.css +1720 -0
- package/dist/ui/avatar.tsx +27 -0
- package/dist/ui/chat.tsx +26 -0
- package/dist/ui/confirm.tsx +71 -0
- package/dist/ui/context.ts +31 -0
- package/dist/ui/feedback.tsx +78 -0
- package/dist/ui/footer.tsx +60 -0
- package/dist/ui/header.tsx +51 -0
- package/dist/ui/icons.tsx +89 -0
- package/dist/ui/index.ts +8 -0
- package/dist/ui/message.tsx +21 -0
- package/dist/ui/panel.tsx +121 -0
- package/dist/ui/welcome.tsx +88 -0
- package/dist/version.js +12 -0
- package/docs/cli.md +30 -0
- package/docs/configuration.md +74 -4
- package/docs/costs.md +7 -1
- package/docs/embedding.md +2 -0
- package/docs/feedback.md +54 -0
- package/docs/getting-started.md +3 -0
- package/docs/going-public.md +1 -0
- package/docs/observability.md +76 -0
- package/docs/privacy.md +27 -7
- package/docs/protocol.md +13 -3
- package/docs/react.md +283 -0
- package/docs/styling.md +14 -2
- package/docs/support.md +4 -2
- package/docs/texts.md +6 -0
- package/docs/troubleshooting.md +12 -0
- package/llms.txt +5 -2
- package/package.json +21 -9
- package/skills/askdare/SKILL.md +6 -2
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import type { ReactElement } from 'react';
|
|
3
|
+
import { useSkin } from './context';
|
|
4
|
+
import { SparkIcon } from './icons';
|
|
5
|
+
|
|
6
|
+
const initials = (title: string): string =>
|
|
7
|
+
title
|
|
8
|
+
.split(/\s+/)
|
|
9
|
+
.filter(Boolean)
|
|
10
|
+
.slice(0, 2)
|
|
11
|
+
.map((word) => word[0]?.toUpperCase() ?? '')
|
|
12
|
+
.join('') || 'AI';
|
|
13
|
+
|
|
14
|
+
/** The assistant's face: the configured picture, else the preset's orb, icon or initials. */
|
|
15
|
+
export function Avatar({ size }: { readonly size: 'small' | 'large' }): ReactElement | null {
|
|
16
|
+
const { bot } = useSkin();
|
|
17
|
+
const variant = bot.avatar ? 'image' : bot.variants.avatar;
|
|
18
|
+
if (variant === 'none') return null;
|
|
19
|
+
return (
|
|
20
|
+
<span data-ask-part="avatar" data-kind={variant} data-size={size} aria-hidden="true">
|
|
21
|
+
{variant === 'image' && <img src={bot.avatar} alt="" />}
|
|
22
|
+
{variant === 'orb' && <span data-ask-part="orb" />}
|
|
23
|
+
{variant === 'icon' && <SparkIcon size={size === 'large' ? 28 : 16} />}
|
|
24
|
+
{variant === 'initials' && initials(bot.title)}
|
|
25
|
+
</span>
|
|
26
|
+
);
|
|
27
|
+
}
|
package/dist/ui/chat.tsx
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
'use client';
|
|
3
|
+
|
|
4
|
+
// The entry of an ejected copy (`npx @boldare/askdare add ui`): the same DefaultChat as in
|
|
5
|
+
// @boldare/askdare/react, built from the files in this folder and their own stylesheet. Inside
|
|
6
|
+
// the package nothing imports it: there, DefaultChat loads the skin from the chat route.
|
|
7
|
+
import { Theme, type ThemeProps } from '@boldare/askdare/react';
|
|
8
|
+
import type { ReactElement } from 'react';
|
|
9
|
+
import './askdare.css';
|
|
10
|
+
import { DefaultPanel, type DefaultPanelProps } from './panel';
|
|
11
|
+
|
|
12
|
+
export interface DefaultChatProps extends DefaultPanelProps {
|
|
13
|
+
/** On the element that holds the chat. Give it a height: the chat fills it. */
|
|
14
|
+
readonly className?: string | undefined;
|
|
15
|
+
readonly colorScheme?: ThemeProps['colorScheme'];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The chat with this folder's look: `<Root config={config}><DefaultChat className="…" /></Root>`. */
|
|
19
|
+
export function DefaultChat({ className, colorScheme, ...panel }: DefaultChatProps): ReactElement {
|
|
20
|
+
return (
|
|
21
|
+
// data-ask-skin: the skin's rules reach only inside this element, never a headless chat.
|
|
22
|
+
<Theme className={className} colorScheme={colorScheme} data-ask-skin="">
|
|
23
|
+
<DefaultPanel {...panel} />
|
|
24
|
+
</Theme>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import { useChat } from '@boldare/askdare/react';
|
|
3
|
+
import { type KeyboardEvent, type ReactElement, useEffect, useId, useRef } from 'react';
|
|
4
|
+
import { RestartIcon } from './icons';
|
|
5
|
+
|
|
6
|
+
/** Keeps Tab inside the question while it is open. */
|
|
7
|
+
function trapTab(event: KeyboardEvent<HTMLDivElement>): void {
|
|
8
|
+
if (event.key !== 'Tab') return;
|
|
9
|
+
const buttons = Array.from(event.currentTarget.querySelectorAll('button'));
|
|
10
|
+
const first = buttons[0];
|
|
11
|
+
const last = buttons[buttons.length - 1];
|
|
12
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
13
|
+
event.preventDefault();
|
|
14
|
+
last?.focus();
|
|
15
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
16
|
+
event.preventDefault();
|
|
17
|
+
first?.focus();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Asks before a new conversation deletes the current one: in the chat, not a browser dialog,
|
|
23
|
+
* over the conversation, which stays in view.
|
|
24
|
+
*/
|
|
25
|
+
export function StartOverQuestion({
|
|
26
|
+
modal,
|
|
27
|
+
onCancel,
|
|
28
|
+
onAccept,
|
|
29
|
+
}: {
|
|
30
|
+
/** Modal only when the chat is the whole document; inside a page, the page stays usable. */
|
|
31
|
+
readonly modal: boolean;
|
|
32
|
+
readonly onCancel: () => void;
|
|
33
|
+
readonly onAccept: () => void;
|
|
34
|
+
}): ReactElement {
|
|
35
|
+
const { t } = useChat();
|
|
36
|
+
const dialog = useRef<HTMLDivElement>(null);
|
|
37
|
+
const textId = useId();
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
// Focus starts on Cancel: the safe answer.
|
|
40
|
+
const frame = requestAnimationFrame(() => dialog.current?.querySelector('button')?.focus());
|
|
41
|
+
return () => cancelAnimationFrame(frame);
|
|
42
|
+
}, []);
|
|
43
|
+
return (
|
|
44
|
+
<div data-ask-part="confirm-backdrop">
|
|
45
|
+
<div
|
|
46
|
+
ref={dialog}
|
|
47
|
+
data-ask-part="confirm"
|
|
48
|
+
role="alertdialog"
|
|
49
|
+
aria-modal={modal || undefined}
|
|
50
|
+
aria-describedby={textId}
|
|
51
|
+
aria-label={t('header.newConversation')}
|
|
52
|
+
onKeyDown={trapTab}
|
|
53
|
+
>
|
|
54
|
+
<span data-ask-part="confirm-icon" aria-hidden="true">
|
|
55
|
+
<RestartIcon />
|
|
56
|
+
</span>
|
|
57
|
+
<p id={textId} data-ask-part="confirm-text">
|
|
58
|
+
{t('confirm.newConversation')}
|
|
59
|
+
</p>
|
|
60
|
+
<div data-ask-part="confirm-actions">
|
|
61
|
+
<button type="button" data-ask-part="confirm-cancel" onClick={onCancel}>
|
|
62
|
+
{t('confirm.cancel')}
|
|
63
|
+
</button>
|
|
64
|
+
<button type="button" data-ask-part="confirm-accept" onClick={onAccept}>
|
|
65
|
+
{t('confirm.accept')}
|
|
66
|
+
</button>
|
|
67
|
+
</div>
|
|
68
|
+
</div>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import type { ChatBot, Translate } from '@boldare/askdare/react';
|
|
3
|
+
import { type Context, createContext, type RefObject, useContext } from 'react';
|
|
4
|
+
|
|
5
|
+
/** What the parts of the default look share; slots read it too. */
|
|
6
|
+
export interface Skin {
|
|
7
|
+
/**
|
|
8
|
+
* The bot and the texts, for parts rendered once per message: unlike `useChat()`, they don't
|
|
9
|
+
* re-render those parts on every change of the chat.
|
|
10
|
+
*/
|
|
11
|
+
readonly bot: ChatBot;
|
|
12
|
+
readonly t: Translate;
|
|
13
|
+
/** Level of the chat's title; the welcome's heading is one below. */
|
|
14
|
+
readonly headingLevel: 1 | 2 | 3 | 4 | 5;
|
|
15
|
+
readonly onClose: (() => void) | undefined;
|
|
16
|
+
/** Opens the question before a new conversation deletes the current one. */
|
|
17
|
+
readonly askStartOver: () => void;
|
|
18
|
+
/** The header's restart button, where focus returns when the question closes. */
|
|
19
|
+
readonly restartRef: RefObject<HTMLButtonElement | null>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const SkinContext: Context<Skin | null> = /* @__PURE__ */ createContext<Skin | null>(null);
|
|
23
|
+
|
|
24
|
+
export function useSkin(): Skin {
|
|
25
|
+
const skin = useContext(SkinContext);
|
|
26
|
+
if (!skin) throw new Error('[askdare] The default look only works inside <DefaultChat>.');
|
|
27
|
+
return skin;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** An h1–h6 tag name for a level, capped at 6. */
|
|
31
|
+
export const heading = (level: number): 'h1' => `h${Math.min(Math.max(level, 1), 6)}` as 'h1';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import { FeedbackButton, useChat, useChatState, useMessage } from '@boldare/askdare/react';
|
|
3
|
+
import { type FormEvent, type ReactElement, useEffect, useId, useRef, useState } from 'react';
|
|
4
|
+
import { useSkin } from './context';
|
|
5
|
+
import { ThumbDownIcon, ThumbUpIcon } from './icons';
|
|
6
|
+
|
|
7
|
+
/** As the server's limit. */
|
|
8
|
+
const MAX_COMMENT = 500;
|
|
9
|
+
|
|
10
|
+
/** Asks what went wrong after a 👎; sending is optional. */
|
|
11
|
+
function Comment(): ReactElement {
|
|
12
|
+
const message = useMessage();
|
|
13
|
+
const { actions, t } = useChat();
|
|
14
|
+
const [text, setText] = useState('');
|
|
15
|
+
const [sending, setSending] = useState(false);
|
|
16
|
+
const field = useRef<HTMLTextAreaElement>(null);
|
|
17
|
+
const fieldId = useId();
|
|
18
|
+
// The visitor just pressed 👎: they can type right away.
|
|
19
|
+
useEffect(() => field.current?.focus(), []);
|
|
20
|
+
const onSubmit = (event: FormEvent): void => {
|
|
21
|
+
event.preventDefault();
|
|
22
|
+
if (!text.trim()) {
|
|
23
|
+
actions.dismissComment();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// The form stays until the comment is sent; if it isn't, the text is still here to retry.
|
|
27
|
+
setSending(true);
|
|
28
|
+
void actions.rate(message.id, 'down', text).finally(() => setSending(false));
|
|
29
|
+
};
|
|
30
|
+
return (
|
|
31
|
+
<form data-ask-part="feedback-comment" onSubmit={onSubmit}>
|
|
32
|
+
<label htmlFor={fieldId}>{t('feedback.comment')}</label>
|
|
33
|
+
<textarea
|
|
34
|
+
ref={field}
|
|
35
|
+
id={fieldId}
|
|
36
|
+
data-ask-part="feedback-comment-input"
|
|
37
|
+
rows={2}
|
|
38
|
+
maxLength={MAX_COMMENT}
|
|
39
|
+
value={text}
|
|
40
|
+
onChange={(event) => setText(event.target.value)}
|
|
41
|
+
/>
|
|
42
|
+
<div data-ask-part="feedback-comment-actions">
|
|
43
|
+
<button type="button" data-ask-part="feedback-skip" onClick={actions.dismissComment}>
|
|
44
|
+
{t('feedback.skip')}
|
|
45
|
+
</button>
|
|
46
|
+
<button
|
|
47
|
+
type="submit"
|
|
48
|
+
data-ask-part="feedback-send"
|
|
49
|
+
disabled={sending || text.trim() === ''}
|
|
50
|
+
>
|
|
51
|
+
{t('feedback.send')}
|
|
52
|
+
</button>
|
|
53
|
+
</div>
|
|
54
|
+
</form>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 👍 and 👎 under an answer that can be rated, and the question after a 👎. */
|
|
59
|
+
export function Feedback(): ReactElement | null {
|
|
60
|
+
const message = useMessage();
|
|
61
|
+
const { t } = useSkin();
|
|
62
|
+
const commenting = useChatState((state) => state.commenting === message.id);
|
|
63
|
+
if (!message.feedback || message.role !== 'assistant' || message.status !== 'done') return null;
|
|
64
|
+
return (
|
|
65
|
+
<>
|
|
66
|
+
{/* biome-ignore lint/a11y/useSemanticElements: a group of two toggle buttons, not a form's fieldset */}
|
|
67
|
+
<div data-ask-part="feedback" role="group" aria-label={t('feedback.label')}>
|
|
68
|
+
<FeedbackButton rating="up">
|
|
69
|
+
<ThumbUpIcon />
|
|
70
|
+
</FeedbackButton>
|
|
71
|
+
<FeedbackButton rating="down">
|
|
72
|
+
<ThumbDownIcon />
|
|
73
|
+
</FeedbackButton>
|
|
74
|
+
</div>
|
|
75
|
+
{commenting && <Comment />}
|
|
76
|
+
</>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import {
|
|
3
|
+
AttachButton,
|
|
4
|
+
Composer,
|
|
5
|
+
DictateButton,
|
|
6
|
+
Files,
|
|
7
|
+
Input,
|
|
8
|
+
Recorder,
|
|
9
|
+
Send,
|
|
10
|
+
useChat,
|
|
11
|
+
} from '@boldare/askdare/react';
|
|
12
|
+
import type { ReactElement } from 'react';
|
|
13
|
+
import { AttachIcon, CloseIcon, MicIcon, SendIcon } from './icons';
|
|
14
|
+
|
|
15
|
+
/** The message field with its buttons, or the dictation in its place, then the AI notice. */
|
|
16
|
+
export function DefaultFooter(): ReactElement {
|
|
17
|
+
const { state, bot, t } = useChat();
|
|
18
|
+
return (
|
|
19
|
+
<>
|
|
20
|
+
<Composer>
|
|
21
|
+
<Files />
|
|
22
|
+
<div data-ask-part="composer-box">
|
|
23
|
+
{state.dictation === 'idle' ? (
|
|
24
|
+
<>
|
|
25
|
+
<AttachButton>
|
|
26
|
+
<AttachIcon />
|
|
27
|
+
</AttachButton>
|
|
28
|
+
<Input />
|
|
29
|
+
<DictateButton>
|
|
30
|
+
<MicIcon />
|
|
31
|
+
</DictateButton>
|
|
32
|
+
<Send>
|
|
33
|
+
<SendIcon />
|
|
34
|
+
<span data-ask-part="send-label" aria-hidden="true">
|
|
35
|
+
{t('composer.send')}
|
|
36
|
+
</span>
|
|
37
|
+
</Send>
|
|
38
|
+
</>
|
|
39
|
+
) : (
|
|
40
|
+
<Recorder cancelIcon={<CloseIcon />} />
|
|
41
|
+
)}
|
|
42
|
+
</div>
|
|
43
|
+
</Composer>
|
|
44
|
+
{bot.disclosure !== false && (
|
|
45
|
+
// The AI notice (EU AI Act, art. 50).
|
|
46
|
+
<p data-ask-part="disclosure">
|
|
47
|
+
{t('disclosure.text')}
|
|
48
|
+
{bot.disclosure.privacyUrl && (
|
|
49
|
+
<>
|
|
50
|
+
{' '}
|
|
51
|
+
<a href={bot.disclosure.privacyUrl} target="_blank" rel="noopener noreferrer">
|
|
52
|
+
{t('disclosure.privacy')}
|
|
53
|
+
</a>
|
|
54
|
+
</>
|
|
55
|
+
)}
|
|
56
|
+
</p>
|
|
57
|
+
)}
|
|
58
|
+
</>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import { useChat } from '@boldare/askdare/react';
|
|
3
|
+
import type { ReactElement } from 'react';
|
|
4
|
+
import { Avatar } from './avatar';
|
|
5
|
+
import { heading, useSkin } from './context';
|
|
6
|
+
import { CloseIcon, RestartIcon } from './icons';
|
|
7
|
+
|
|
8
|
+
/** The bot's name and picture, a new-conversation button and, when it can close, a close button. */
|
|
9
|
+
export function DefaultHeader(): ReactElement {
|
|
10
|
+
const { state, bot, t } = useChat();
|
|
11
|
+
const { headingLevel, onClose, askStartOver, restartRef } = useSkin();
|
|
12
|
+
const Title = heading(headingLevel);
|
|
13
|
+
return (
|
|
14
|
+
<header data-ask-part="header">
|
|
15
|
+
<div data-ask-part="identity">
|
|
16
|
+
<Avatar size="small" />
|
|
17
|
+
<div data-ask-part="identity-text">
|
|
18
|
+
<Title data-ask-part="title">{bot.title}</Title>
|
|
19
|
+
{bot.subtitle && <p data-ask-part="subtitle">{bot.subtitle}</p>}
|
|
20
|
+
</div>
|
|
21
|
+
</div>
|
|
22
|
+
<div data-ask-part="actions">
|
|
23
|
+
{state.messages.length > 0 && (
|
|
24
|
+
<button
|
|
25
|
+
ref={restartRef}
|
|
26
|
+
type="button"
|
|
27
|
+
data-ask-part="new-conversation"
|
|
28
|
+
aria-label={t('header.newConversation')}
|
|
29
|
+
aria-haspopup="dialog"
|
|
30
|
+
title={t('header.newConversation')}
|
|
31
|
+
disabled={state.busy}
|
|
32
|
+
onClick={askStartOver}
|
|
33
|
+
>
|
|
34
|
+
<RestartIcon />
|
|
35
|
+
</button>
|
|
36
|
+
)}
|
|
37
|
+
{onClose && (
|
|
38
|
+
<button
|
|
39
|
+
type="button"
|
|
40
|
+
data-ask-part="close"
|
|
41
|
+
aria-label={t('header.close')}
|
|
42
|
+
title={t('header.close')}
|
|
43
|
+
onClick={onClose}
|
|
44
|
+
>
|
|
45
|
+
<CloseIcon />
|
|
46
|
+
</button>
|
|
47
|
+
)}
|
|
48
|
+
</div>
|
|
49
|
+
</header>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
/** Line icons on a 24px grid, drawn with the current text color. */
|
|
5
|
+
function Icon({
|
|
6
|
+
children,
|
|
7
|
+
size = 20,
|
|
8
|
+
}: {
|
|
9
|
+
readonly children: ReactNode;
|
|
10
|
+
readonly size?: number;
|
|
11
|
+
}): ReactElement {
|
|
12
|
+
return (
|
|
13
|
+
<svg
|
|
14
|
+
viewBox="0 0 24 24"
|
|
15
|
+
width={size}
|
|
16
|
+
height={size}
|
|
17
|
+
aria-hidden="true"
|
|
18
|
+
fill="none"
|
|
19
|
+
stroke="currentColor"
|
|
20
|
+
strokeWidth="1.8"
|
|
21
|
+
strokeLinecap="round"
|
|
22
|
+
strokeLinejoin="round"
|
|
23
|
+
>
|
|
24
|
+
{children}
|
|
25
|
+
</svg>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const SendIcon = (): ReactElement => (
|
|
30
|
+
<Icon>
|
|
31
|
+
<path d="M12 19V5M5.5 11.5 12 5l6.5 6.5" />
|
|
32
|
+
</Icon>
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
export const AttachIcon = (): ReactElement => (
|
|
36
|
+
<Icon>
|
|
37
|
+
<path d="M20 11.5 12.2 19.3a5 5 0 0 1-7.1-7.1l8.1-8.1a3.4 3.4 0 0 1 4.8 4.8l-8 8a1.8 1.8 0 0 1-2.6-2.6l7.3-7.3" />
|
|
38
|
+
</Icon>
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
export const MicIcon = (): ReactElement => (
|
|
42
|
+
<Icon>
|
|
43
|
+
<rect x="9" y="3" width="6" height="11" rx="3" />
|
|
44
|
+
<path d="M5.5 11a6.5 6.5 0 0 0 13 0M12 17.5V21" />
|
|
45
|
+
</Icon>
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
export const CloseIcon = ({ size = 18 }: { readonly size?: number }): ReactElement => (
|
|
49
|
+
<Icon size={size}>
|
|
50
|
+
<path d="M6 6l12 12M18 6 6 18" />
|
|
51
|
+
</Icon>
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
export const RestartIcon = (): ReactElement => (
|
|
55
|
+
<Icon size={18}>
|
|
56
|
+
<path d="M4 12a8 8 0 1 0 2.4-5.7M4 4v4.5h4.5" />
|
|
57
|
+
</Icon>
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export const CheckIcon = (): ReactElement => (
|
|
61
|
+
<Icon size={18}>
|
|
62
|
+
<path d="M5 12.5l4.5 4.5L19 7.5" />
|
|
63
|
+
</Icon>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
export const SparkIcon = ({ size = 16 }: { readonly size?: number }): ReactElement => (
|
|
67
|
+
<Icon size={size}>
|
|
68
|
+
<path d="M12 3.5c.5 4.3 2.2 6 6.5 6.5-4.3.5-6 2.2-6.5 6.5-.5-4.3-2.2-6-6.5-6.5 4.3-.5 6-2.2 6.5-6.5Z" />
|
|
69
|
+
<path d="M18.5 15.5c.2 1.6.9 2.3 2.5 2.5-1.6.2-2.3.9-2.5 2.5-.2-1.6-.9-2.3-2.5-2.5 1.6-.2 2.3-.9 2.5-2.5Z" />
|
|
70
|
+
</Icon>
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
export const ArrowIcon = (): ReactElement => (
|
|
74
|
+
<Icon size={16}>
|
|
75
|
+
<path d="M5 12h14M13 6l6 6-6 6" />
|
|
76
|
+
</Icon>
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
export const ThumbUpIcon = (): ReactElement => (
|
|
80
|
+
<Icon size={16}>
|
|
81
|
+
<path d="M7 10.5v9.5M7 10.5l4.2-7a2.2 2.2 0 0 1 2.6 2.7L13 9.5h5.2a2 2 0 0 1 2 2.4l-1.2 6.5a2 2 0 0 1-2 1.6H4.5V10.5H7Z" />
|
|
82
|
+
</Icon>
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
export const ThumbDownIcon = (): ReactElement => (
|
|
86
|
+
<Icon size={16}>
|
|
87
|
+
<path d="M17 13.5V4M17 13.5l-4.2 7a2.2 2.2 0 0 1-2.6-2.7l.8-3.3H5.8a2 2 0 0 1-2-2.4L5 5.6A2 2 0 0 1 7 4h12.5v9.5H17Z" />
|
|
88
|
+
</Icon>
|
|
89
|
+
);
|
package/dist/ui/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
'use client';
|
|
3
|
+
|
|
4
|
+
// The default look of the chat (ADR-0011): what `npx @boldare/askdare add ui` copies into an app.
|
|
5
|
+
export type { DefaultChatComponents, DefaultPanelProps } from './panel';
|
|
6
|
+
export { DefaultPanel } from './panel';
|
|
7
|
+
export type { DefaultChatProps } from './chat';
|
|
8
|
+
export { DefaultChat } from './chat';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import { Citations, Message, MessageFiles, MessageText, useMessage } from '@boldare/askdare/react';
|
|
3
|
+
import type { ReactElement } from 'react';
|
|
4
|
+
import { Avatar } from './avatar';
|
|
5
|
+
import { Feedback } from './feedback';
|
|
6
|
+
|
|
7
|
+
/** One message: the assistant's face beside its answers, then text, files, sources and rating. */
|
|
8
|
+
export function DefaultMessage(): ReactElement {
|
|
9
|
+
const message = useMessage();
|
|
10
|
+
return (
|
|
11
|
+
<Message>
|
|
12
|
+
{message.role === 'assistant' && <Avatar size="small" />}
|
|
13
|
+
<div data-ask-part="message-body">
|
|
14
|
+
<MessageText />
|
|
15
|
+
<MessageFiles />
|
|
16
|
+
<Citations />
|
|
17
|
+
<Feedback />
|
|
18
|
+
</div>
|
|
19
|
+
</Message>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import {
|
|
3
|
+
ErrorMessage,
|
|
4
|
+
MessageList,
|
|
5
|
+
Messages,
|
|
6
|
+
Panel,
|
|
7
|
+
Status,
|
|
8
|
+
useChat,
|
|
9
|
+
} from '@boldare/askdare/react';
|
|
10
|
+
import {
|
|
11
|
+
type ComponentType,
|
|
12
|
+
type ReactElement,
|
|
13
|
+
useCallback,
|
|
14
|
+
useEffect,
|
|
15
|
+
useMemo,
|
|
16
|
+
useRef,
|
|
17
|
+
useState,
|
|
18
|
+
} from 'react';
|
|
19
|
+
import { StartOverQuestion } from './confirm';
|
|
20
|
+
import { type Skin, SkinContext } from './context';
|
|
21
|
+
import { DefaultFooter } from './footer';
|
|
22
|
+
import { DefaultHeader } from './header';
|
|
23
|
+
import { DefaultMessage } from './message';
|
|
24
|
+
import { DefaultWelcome } from './welcome';
|
|
25
|
+
|
|
26
|
+
/** Parts of the default look you can replace. They take no props: read the chat with the hooks. */
|
|
27
|
+
export interface DefaultChatComponents {
|
|
28
|
+
readonly Header?: ComponentType;
|
|
29
|
+
readonly Welcome?: ComponentType;
|
|
30
|
+
/** Rendered for each message; `useMessage()` gives the message. */
|
|
31
|
+
readonly Message?: ComponentType;
|
|
32
|
+
/** The message field and the AI notice under it. */
|
|
33
|
+
readonly Footer?: ComponentType;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface DefaultPanelProps {
|
|
37
|
+
/** Level of the chat's title (default 2); 1 makes the chat the page's main landmark. */
|
|
38
|
+
readonly headingLevel?: 1 | 2 | 3 | 4 | 5;
|
|
39
|
+
/** Shows a close button, and Escape calls it too. */
|
|
40
|
+
readonly onClose?: (() => void) | undefined;
|
|
41
|
+
readonly components?: DefaultChatComponents | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The default look of the chat inside its frame: header, conversation and message field. */
|
|
45
|
+
export function DefaultPanel({
|
|
46
|
+
headingLevel = 2,
|
|
47
|
+
onClose,
|
|
48
|
+
components = {},
|
|
49
|
+
ownsDocument = false,
|
|
50
|
+
}: DefaultPanelProps & {
|
|
51
|
+
/** The chat is the whole document, as in its iframe: Escape works wherever focus is. */
|
|
52
|
+
readonly ownsDocument?: boolean;
|
|
53
|
+
}): ReactElement {
|
|
54
|
+
const { state, actions, bot, t } = useChat();
|
|
55
|
+
const [confirming, setConfirming] = useState(false);
|
|
56
|
+
const restartRef = useRef<HTMLButtonElement>(null);
|
|
57
|
+
const askStartOver = useCallback(() => setConfirming(true), []);
|
|
58
|
+
const skin = useMemo<Skin>(
|
|
59
|
+
() => ({ bot, t, headingLevel, onClose, askStartOver, restartRef }),
|
|
60
|
+
[bot, t, headingLevel, onClose, askStartOver],
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const cancelStartOver = (): void => {
|
|
64
|
+
setConfirming(false);
|
|
65
|
+
requestAnimationFrame(() => restartRef.current?.focus());
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Escape answers a question of the chat, else cancels a dictation, else closes the chat. Inside a page,
|
|
69
|
+
// only when it is pressed in the chat; as the whole document (the iframe), wherever focus is.
|
|
70
|
+
const main = headingLevel === 1;
|
|
71
|
+
const panelRef = useRef<HTMLElement>(null);
|
|
72
|
+
const onEscape = useRef(() => {});
|
|
73
|
+
onEscape.current = () => {
|
|
74
|
+
if (confirming) cancelStartOver();
|
|
75
|
+
else if (state.commenting !== undefined) actions.dismissComment();
|
|
76
|
+
else if (state.dictation === 'recording') actions.cancelDictation();
|
|
77
|
+
else onClose?.();
|
|
78
|
+
};
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const onKey = (event: KeyboardEvent): void => {
|
|
81
|
+
// Not when a handler already used it, or while an input method composes text.
|
|
82
|
+
if (event.key !== 'Escape' || event.defaultPrevented || event.isComposing) return;
|
|
83
|
+
const inside = event.target instanceof Node && panelRef.current?.contains(event.target);
|
|
84
|
+
if (ownsDocument || inside) onEscape.current();
|
|
85
|
+
};
|
|
86
|
+
window.addEventListener('keydown', onKey);
|
|
87
|
+
return () => window.removeEventListener('keydown', onKey);
|
|
88
|
+
}, [ownsDocument]);
|
|
89
|
+
|
|
90
|
+
const Header = components.Header ?? DefaultHeader;
|
|
91
|
+
const Welcome = components.Welcome ?? DefaultWelcome;
|
|
92
|
+
const Message = components.Message ?? DefaultMessage;
|
|
93
|
+
const Footer = components.Footer ?? DefaultFooter;
|
|
94
|
+
// The same element on every render, so only the message that changed renders again.
|
|
95
|
+
const message = useMemo(() => <Message />, [Message]);
|
|
96
|
+
const showWelcome = state.messages.length === 0 && state.phase !== 'loading';
|
|
97
|
+
return (
|
|
98
|
+
<SkinContext.Provider value={skin}>
|
|
99
|
+
<Panel ref={panelRef} landmark={main ? 'main' : 'region'}>
|
|
100
|
+
<Header />
|
|
101
|
+
{confirming && (
|
|
102
|
+
<StartOverQuestion
|
|
103
|
+
modal={ownsDocument}
|
|
104
|
+
onCancel={cancelStartOver}
|
|
105
|
+
onAccept={() => {
|
|
106
|
+
setConfirming(false);
|
|
107
|
+
void actions.newConversation();
|
|
108
|
+
}}
|
|
109
|
+
/>
|
|
110
|
+
)}
|
|
111
|
+
<Messages>
|
|
112
|
+
{showWelcome && <Welcome />}
|
|
113
|
+
<MessageList>{message}</MessageList>
|
|
114
|
+
<Status />
|
|
115
|
+
</Messages>
|
|
116
|
+
<ErrorMessage />
|
|
117
|
+
<Footer />
|
|
118
|
+
</Panel>
|
|
119
|
+
</SkinContext.Provider>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
|
+
import { useChat } from '@boldare/askdare/react';
|
|
3
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
4
|
+
import { Avatar } from './avatar';
|
|
5
|
+
import { heading, useSkin } from './context';
|
|
6
|
+
import { ArrowIcon, CheckIcon, SparkIcon } from './icons';
|
|
7
|
+
|
|
8
|
+
/** `**words**` in a greeting are shown in bold, as in Aura's two-weight headline; nothing else is markup. */
|
|
9
|
+
function emphasized(text: string): ReactNode[] {
|
|
10
|
+
return text.split('**').map((part, index) =>
|
|
11
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: the parts of a fixed string never move
|
|
12
|
+
index % 2 === 1 ? <strong key={index}>{part}</strong> : part,
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function Starters(): ReactElement | null {
|
|
17
|
+
const { state, actions, bot } = useChat();
|
|
18
|
+
if (bot.starters.length === 0) return null;
|
|
19
|
+
const checklist = bot.variants.welcome === 'intro';
|
|
20
|
+
const disabled = state.busy || state.phase !== 'ready';
|
|
21
|
+
return (
|
|
22
|
+
<ul data-ask-part="starters" aria-label={bot.texts['welcome.suggestions']}>
|
|
23
|
+
{bot.starters.map((starter) => (
|
|
24
|
+
<li key={starter}>
|
|
25
|
+
<button
|
|
26
|
+
type="button"
|
|
27
|
+
data-ask-part="starter"
|
|
28
|
+
disabled={disabled}
|
|
29
|
+
onClick={() => void actions.send(starter)}
|
|
30
|
+
>
|
|
31
|
+
{checklist ? <CheckIcon /> : <SparkIcon />}
|
|
32
|
+
<span>{starter}</span>
|
|
33
|
+
{bot.variants.welcome === 'persona' && <ArrowIcon />}
|
|
34
|
+
</button>
|
|
35
|
+
</li>
|
|
36
|
+
))}
|
|
37
|
+
</ul>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What a visitor sees before the first message. The preset picks the layout: a centered hero,
|
|
43
|
+
* a persona card, or an opening message with a checklist of what the assistant can do.
|
|
44
|
+
*/
|
|
45
|
+
export function DefaultWelcome(): ReactElement {
|
|
46
|
+
const { bot } = useChat();
|
|
47
|
+
const Heading = heading(useSkin().headingLevel + 1);
|
|
48
|
+
const layout = bot.variants.welcome;
|
|
49
|
+
if (layout === 'intro') {
|
|
50
|
+
return (
|
|
51
|
+
<section data-ask-part="welcome" data-layout="intro" aria-label={bot.texts['welcome.label']}>
|
|
52
|
+
<div data-ask-part="intro">
|
|
53
|
+
<Avatar size="small" />
|
|
54
|
+
<div data-ask-part="intro-card">
|
|
55
|
+
<p data-ask-part="greeting">{emphasized(bot.greeting)}</p>
|
|
56
|
+
<Starters />
|
|
57
|
+
</div>
|
|
58
|
+
</div>
|
|
59
|
+
</section>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
if (layout === 'persona') {
|
|
63
|
+
return (
|
|
64
|
+
<section
|
|
65
|
+
data-ask-part="welcome"
|
|
66
|
+
data-layout="persona"
|
|
67
|
+
aria-label={bot.texts['welcome.label']}
|
|
68
|
+
>
|
|
69
|
+
<div data-ask-part="persona">
|
|
70
|
+
<Avatar size="large" />
|
|
71
|
+
<div>
|
|
72
|
+
<Heading data-ask-part="persona-name">{bot.title}</Heading>
|
|
73
|
+
<span data-ask-part="presence">{bot.texts['welcome.online']}</span>
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
<p data-ask-part="greeting">{emphasized(bot.greeting)}</p>
|
|
77
|
+
<Starters />
|
|
78
|
+
</section>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return (
|
|
82
|
+
<section data-ask-part="welcome" data-layout="hero" aria-label={bot.texts['welcome.label']}>
|
|
83
|
+
<Avatar size="large" />
|
|
84
|
+
<Heading data-ask-part="greeting">{emphasized(bot.greeting)}</Heading>
|
|
85
|
+
<Starters />
|
|
86
|
+
</section>
|
|
87
|
+
);
|
|
88
|
+
}
|