@widgetic/chat 0.1.38 → 0.1.40
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/README.md +71 -0
- package/dist/components/AttachmentDisplay.svelte +14 -4
- package/dist/components/Chat.svelte +131 -106
- package/dist/components/Chat.svelte.d.ts +29 -0
- package/dist/components/ChatComposer.svelte +5 -1
- package/dist/components/ChatMessages.svelte +595 -175
- package/dist/components/ChatMessages.svelte.d.ts +20 -0
- package/dist/components/CodeBlock.svelte +9 -1
- package/dist/components/ComposerAttachmentsTray.svelte +64 -0
- package/dist/components/ComposerAttachmentsTray.svelte.d.ts +11 -0
- package/dist/components/PanelChatHeader.svelte +26 -6
- package/dist/components/PanelChatHeader.svelte.d.ts +1 -0
- package/dist/components/PanelFollowUpSuggestions.svelte +2 -2
- package/dist/components/PanelSuggestionButtons.svelte +2 -1
- package/dist/components/agent/AgentAvatar.svelte +153 -0
- package/dist/components/agent/AgentAvatar.svelte.d.ts +27 -0
- package/dist/components/agent/AgentTurn.svelte +64 -0
- package/dist/components/agent/AgentTurn.svelte.d.ts +32 -0
- package/dist/components/agent/AvatarBoundary.svelte +29 -0
- package/dist/components/agent/AvatarBoundary.svelte.d.ts +20 -0
- package/dist/components/agent/CodeCard.svelte +288 -0
- package/dist/components/agent/CodeCard.svelte.d.ts +12 -0
- package/dist/components/agent/GenerationModule.svelte +99 -0
- package/dist/components/agent/GenerationModule.svelte.d.ts +16 -0
- package/dist/components/agent/MemberAvatar.svelte +87 -0
- package/dist/components/agent/MemberAvatar.svelte.d.ts +8 -0
- package/dist/components/index.d.ts +5 -0
- package/dist/components/index.js +9 -0
- package/dist/i18n/messages.d.ts +32 -10
- package/dist/i18n/messages.js +32 -10
- package/dist/types/agent.d.ts +74 -0
- package/dist/types/agent.js +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/dist/utils/agentTurn.d.ts +219 -0
- package/dist/utils/agentTurn.js +301 -0
- package/dist/utils/attachmentPreviewUrl.d.ts +21 -0
- package/dist/utils/attachmentPreviewUrl.js +39 -0
- package/dist/utils/bubbleFit.d.ts +75 -0
- package/dist/utils/bubbleFit.js +116 -0
- package/dist/utils/bubbleShrinkWrap.d.ts +32 -0
- package/dist/utils/bubbleShrinkWrap.js +360 -0
- package/dist/utils/chatImageUploadPrep.d.ts +22 -0
- package/dist/utils/chatImageUploadPrep.js +90 -0
- package/dist/utils/codeHighlight.d.ts +13 -0
- package/dist/utils/codeHighlight.js +61 -0
- package/dist/utils/codePrettier.d.ts +6 -0
- package/dist/utils/codePrettier.js +34 -0
- package/dist/utils/codeStreamFormat.d.ts +90 -0
- package/dist/utils/codeStreamFormat.js +429 -0
- package/dist/utils/generationArchiveStore.d.ts +15 -0
- package/dist/utils/generationArchiveStore.js +89 -0
- package/dist/utils/imageTransform.d.ts +12 -0
- package/dist/utils/imageTransform.js +45 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/suggestionDraft.d.ts +5 -0
- package/dist/utils/suggestionDraft.js +15 -0
- package/package.json +5 -4
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ChatMessage, ChatContext } from '../types/index.js';
|
|
2
2
|
import type { ClarificationSuggestion, DesignControlRequest, VersionRating } from '../types/message.js';
|
|
3
|
+
import type { AgentAvatarRenderer, AgentSoundPlayer, ChatMember } from '../types/agent.js';
|
|
3
4
|
import { type VersionThumb } from '../utils/versionRatings.js';
|
|
4
5
|
import type { Snippet } from 'svelte';
|
|
5
6
|
import { type GenerationPlanWire } from '../utils/generationPlan.js';
|
|
@@ -95,6 +96,25 @@ interface Props {
|
|
|
95
96
|
onRequestOlderMessages?: () => Promise<boolean | void>;
|
|
96
97
|
/** Widget creator side panel: Figma empty / follow-up suggestion cards. */
|
|
97
98
|
panelLayout?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* The people of the project's team. A message a person wrote gets that person's picture (or initials) at its right, and
|
|
101
|
+
* their e-mail while the pointer is on it.
|
|
102
|
+
*/
|
|
103
|
+
members?: ChatMember[];
|
|
104
|
+
/** The signed-in person: who wrote the messages they just sent, and anything no member matches. */
|
|
105
|
+
currentUser?: ChatMember | null;
|
|
106
|
+
/** How the assistant's avatar is drawn (the Scribble Mascot in the Widgetic apps). A plain round avatar without it. */
|
|
107
|
+
renderAgentAvatar?: AgentAvatarRenderer;
|
|
108
|
+
/** Lets the avatar be heard: told its state once for each change, for runs the person watched. Silent without it. */
|
|
109
|
+
agentSound?: AgentSoundPlayer;
|
|
110
|
+
/**
|
|
111
|
+
* Sizes the bubbles of what people wrote to their text with Pretext (https://chenglou.me/pretext/bubbles/): a bubble whose text
|
|
112
|
+
* wraps is as narrow as its widest line allows, not as wide as the chat, so no dead space is left beside short lines. A text that
|
|
113
|
+
* does not wrap, and a message with code blocks, keep the width CSS gives them. On by default; pass `false` for CSS's widths.
|
|
114
|
+
*/
|
|
115
|
+
tightBubbles?: boolean;
|
|
116
|
+
/** Active conversation id — persists generation archive in IndexedDB across reload. */
|
|
117
|
+
conversationId?: string | null;
|
|
98
118
|
}
|
|
99
119
|
declare const ChatMessages: import("svelte").Component<Props, {}, "">;
|
|
100
120
|
type ChatMessages = ReturnType<typeof ChatMessages>;
|
|
@@ -259,7 +259,6 @@
|
|
|
259
259
|
|
|
260
260
|
/* Line numbers styling */
|
|
261
261
|
.with-line-numbers :global(.hljs) {
|
|
262
|
-
padding-left: 3em; /* rtl-ok: the numbers are at the left of the code, which is read left to right on every screen */
|
|
263
262
|
position: relative;
|
|
264
263
|
}
|
|
265
264
|
|
|
@@ -286,6 +285,15 @@
|
|
|
286
285
|
padding: 0;
|
|
287
286
|
}
|
|
288
287
|
|
|
288
|
+
/* The numbers are drawn over the left edge of the code, so the code keeps the room for them. This rule has to come after
|
|
289
|
+
the reset above: both have the same weight, and the reset would otherwise take the room back and hide the first
|
|
290
|
+
characters of every line under the numbers. The code is a block: the padding of an inline element only reaches the
|
|
291
|
+
first line, so the other lines would still start under the numbers. */
|
|
292
|
+
.code-pre.with-line-numbers :global(.hljs) {
|
|
293
|
+
display: block;
|
|
294
|
+
padding-left: 3em; /* rtl-ok: the numbers are at the left of the code, which is read left to right on every screen */
|
|
295
|
+
}
|
|
296
|
+
|
|
289
297
|
/* Embedded highlight.js GitHub Light theme tokens */
|
|
290
298
|
:global(.hljs) {
|
|
291
299
|
color: #24292e;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { AttachmentInput } from '../types/attachment.js';
|
|
3
|
+
import { Chip } from '@widgetic/design-system/components/chat/index';
|
|
4
|
+
import IconX from '@tabler/icons-svelte/icons/x';
|
|
5
|
+
import { useChatLanguage } from '../i18n/language.js';
|
|
6
|
+
import { buildImageThumbnailUrl } from '../utils/imageTransform.js';
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
attachments: AttachmentInput[];
|
|
10
|
+
previewUrl: (attachment: AttachmentInput) => string;
|
|
11
|
+
isImage: (attachment: AttachmentInput) => boolean;
|
|
12
|
+
onRemove: (index: number) => void;
|
|
13
|
+
onOpenPreview?: (url: string) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let { attachments, previewUrl, isImage, onRemove, onOpenPreview }: Props = $props();
|
|
17
|
+
const { t } = useChatLanguage();
|
|
18
|
+
|
|
19
|
+
const IMAGE_PREVIEW_CSS_PX = 56;
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<div class="composer-attachments-tray flex flex-wrap gap-2 pb-2" data-testid="composer-attachments-tray">
|
|
23
|
+
{#each attachments as attachment, index (attachment.preview || attachment.uploadedUrl || attachment.file?.name || index)}
|
|
24
|
+
{@const rawUrl = previewUrl(attachment)}
|
|
25
|
+
{#if isImage(attachment)}
|
|
26
|
+
<div
|
|
27
|
+
class="relative size-14 shrink-0 overflow-hidden rounded-lg border border-border bg-muted"
|
|
28
|
+
data-testid="composer-image-preview"
|
|
29
|
+
>
|
|
30
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
31
|
+
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
|
|
32
|
+
<img
|
|
33
|
+
src={buildImageThumbnailUrl(rawUrl, IMAGE_PREVIEW_CSS_PX) || rawUrl}
|
|
34
|
+
alt={attachment.file?.name || $t('chat.attachment.namelessAlt')}
|
|
35
|
+
class="size-full object-cover"
|
|
36
|
+
onclick={() => rawUrl && onOpenPreview?.(rawUrl)}
|
|
37
|
+
/>
|
|
38
|
+
<button
|
|
39
|
+
type="button"
|
|
40
|
+
class="absolute end-0.5 top-0.5 flex size-5 items-center justify-center rounded-full bg-background/90 text-muted-foreground shadow-sm hover:text-foreground"
|
|
41
|
+
aria-label={$t('chat.attachment.remove')}
|
|
42
|
+
onclick={() => onRemove(index)}
|
|
43
|
+
>
|
|
44
|
+
<IconX class="size-3.5 stroke-2" aria-hidden="true" />
|
|
45
|
+
</button>
|
|
46
|
+
{#if attachment.uploadProgress && attachment.uploadProgress > 0 && attachment.uploadProgress < 100}
|
|
47
|
+
<div class="absolute inset-x-0 bottom-0 h-0.5 bg-border">
|
|
48
|
+
<div
|
|
49
|
+
class="bg-primary h-full transition-all duration-300"
|
|
50
|
+
style="width: {attachment.uploadProgress}%"
|
|
51
|
+
></div>
|
|
52
|
+
</div>
|
|
53
|
+
{/if}
|
|
54
|
+
</div>
|
|
55
|
+
{:else}
|
|
56
|
+
<Chip
|
|
57
|
+
label={attachment.file?.name || $t('chat.attachment.namelessAlt')}
|
|
58
|
+
showRemove={true}
|
|
59
|
+
removeLabel={$t('chat.attachment.remove')}
|
|
60
|
+
onRemove={() => onRemove(index)}
|
|
61
|
+
/>
|
|
62
|
+
{/if}
|
|
63
|
+
{/each}
|
|
64
|
+
</div>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AttachmentInput } from '../types/attachment.js';
|
|
2
|
+
interface Props {
|
|
3
|
+
attachments: AttachmentInput[];
|
|
4
|
+
previewUrl: (attachment: AttachmentInput) => string;
|
|
5
|
+
isImage: (attachment: AttachmentInput) => boolean;
|
|
6
|
+
onRemove: (index: number) => void;
|
|
7
|
+
onOpenPreview?: (url: string) => void;
|
|
8
|
+
}
|
|
9
|
+
declare const ComposerAttachmentsTray: import("svelte").Component<Props, {}, "">;
|
|
10
|
+
type ComposerAttachmentsTray = ReturnType<typeof ComposerAttachmentsTray>;
|
|
11
|
+
export default ComposerAttachmentsTray;
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
conversations?: ConversationRow[];
|
|
19
19
|
currentConversationId?: string | null;
|
|
20
20
|
onSelectConversation?: (conversationId: string) => void;
|
|
21
|
+
isGenerating?: boolean;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
let {
|
|
@@ -29,6 +30,7 @@
|
|
|
29
30
|
conversations = [],
|
|
30
31
|
currentConversationId = null,
|
|
31
32
|
onSelectConversation,
|
|
33
|
+
isGenerating = false,
|
|
32
34
|
}: Props = $props();
|
|
33
35
|
|
|
34
36
|
const { t } = useChatLanguage();
|
|
@@ -38,8 +40,8 @@
|
|
|
38
40
|
return String(row.id || row.conversation_id || row.conversationId || '');
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
+
const historyEnabled = $derived(
|
|
44
|
+
typeof onSelectConversation === 'function' && typeof onNewConversation === 'function'
|
|
43
45
|
);
|
|
44
46
|
</script>
|
|
45
47
|
|
|
@@ -51,8 +53,8 @@
|
|
|
51
53
|
{showConsole}
|
|
52
54
|
{consoleOpen}
|
|
53
55
|
onToggleConsole={onToggleConsole}
|
|
54
|
-
onNewChat={onNewConversation}
|
|
55
|
-
onTitleClick={
|
|
56
|
+
onNewChat={historyEnabled && !isGenerating ? onNewConversation : undefined}
|
|
57
|
+
onTitleClick={historyEnabled ? () => { historyOpen = !historyOpen; } : undefined}
|
|
56
58
|
messages={{
|
|
57
59
|
newChat: $t('chat.header.newConversationTitle'),
|
|
58
60
|
console: $t('chat.header.console'),
|
|
@@ -60,20 +62,38 @@
|
|
|
60
62
|
}}
|
|
61
63
|
/>
|
|
62
64
|
|
|
63
|
-
{#if
|
|
65
|
+
{#if historyEnabled}
|
|
64
66
|
<DropdownMenu.Root bind:open={historyOpen}>
|
|
65
|
-
<DropdownMenu.Content align="start" class="max-h-64 w-
|
|
67
|
+
<DropdownMenu.Content align="start" class="max-h-64 w-72 overflow-y-auto">
|
|
68
|
+
<DropdownMenu.Item
|
|
69
|
+
class="gap-2 font-medium"
|
|
70
|
+
disabled={isGenerating}
|
|
71
|
+
onSelect={() => {
|
|
72
|
+
historyOpen = false;
|
|
73
|
+
onNewConversation?.();
|
|
74
|
+
}}
|
|
75
|
+
>
|
|
76
|
+
{$t('chat.header.newConversationTitle')}
|
|
77
|
+
</DropdownMenu.Item>
|
|
78
|
+
<DropdownMenu.Separator />
|
|
66
79
|
{#each conversations as row (conversationId(row))}
|
|
67
80
|
{@const id = conversationId(row)}
|
|
68
81
|
<DropdownMenu.Item
|
|
69
82
|
class="justify-between gap-2"
|
|
83
|
+
data-active={id === currentConversationId ? true : undefined}
|
|
70
84
|
onSelect={() => {
|
|
85
|
+
historyOpen = false;
|
|
71
86
|
if (id && id !== currentConversationId) onSelectConversation?.(id);
|
|
72
87
|
}}
|
|
73
88
|
>
|
|
74
89
|
<span class="truncate">{row.title?.trim() || $t('chat.header.untitled')}</span>
|
|
75
90
|
</DropdownMenu.Item>
|
|
76
91
|
{/each}
|
|
92
|
+
{#if conversations.length === 0}
|
|
93
|
+
<DropdownMenu.Item disabled class="text-muted-foreground text-caption">
|
|
94
|
+
{$t('chat.header.noPastChats')}
|
|
95
|
+
</DropdownMenu.Item>
|
|
96
|
+
{/if}
|
|
77
97
|
</DropdownMenu.Content>
|
|
78
98
|
</DropdownMenu.Root>
|
|
79
99
|
{/if}
|
|
@@ -13,6 +13,7 @@ interface Props {
|
|
|
13
13
|
conversations?: ConversationRow[];
|
|
14
14
|
currentConversationId?: string | null;
|
|
15
15
|
onSelectConversation?: (conversationId: string) => void;
|
|
16
|
+
isGenerating?: boolean;
|
|
16
17
|
}
|
|
17
18
|
declare const PanelChatHeader: import("svelte").Component<Props, {}, "">;
|
|
18
19
|
type PanelChatHeader = ReturnType<typeof PanelChatHeader>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import IconClock from '@tabler/icons-svelte/icons/clock';
|
|
3
|
-
import
|
|
3
|
+
import IconWand from '@tabler/icons-svelte/icons/wand';
|
|
4
4
|
import IconMessage from '@tabler/icons-svelte/icons/message';
|
|
5
5
|
import { SuggestionCard } from '@widgetic/design-system/components/chat/index';
|
|
6
6
|
import { useChatLanguage } from '../i18n/language.js';
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
let { labels, onPick, disabled = false }: Props = $props();
|
|
18
18
|
|
|
19
|
-
const ICONS = [IconClock,
|
|
19
|
+
const ICONS = [IconClock, IconWand, IconMessage];
|
|
20
20
|
</script>
|
|
21
21
|
|
|
22
22
|
<div class="panel-followup-suggestion-ct flex w-full max-w-full flex-col gap-1">
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
}
|
|
41
41
|
</script>
|
|
42
42
|
|
|
43
|
-
<div class="panel-suggestion-ct flex w-full max-w-full flex-col gap-1">
|
|
43
|
+
<div class="panel-suggestion-ct flex w-full max-w-full flex-col gap-1 pb-2">
|
|
44
44
|
<h4 class="text-caption text-muted-foreground">{$t(titleKey)}</h4>
|
|
45
45
|
<div class="flex w-full flex-col gap-1">
|
|
46
46
|
{#each suggestionKeys as suggestionKey (suggestionKey)}
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
<SuggestionCard
|
|
50
50
|
title={label}
|
|
51
51
|
disabled={disabled}
|
|
52
|
+
class="min-h-11"
|
|
52
53
|
onclick={() => onPick(label)}
|
|
53
54
|
>
|
|
54
55
|
{#snippet icon()}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
/** True once the page's drawing has been reported: the warning is for the page, not for each avatar (a history holds dozens). */
|
|
3
|
+
let hasReportedPageAvatarFailure = false;
|
|
4
|
+
</script>
|
|
5
|
+
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
/**
|
|
8
|
+
* The assistant's avatar.
|
|
9
|
+
*
|
|
10
|
+
* It draws what the page gave (`renderAgentAvatar`, the Scribble Mascot in the Widgetic apps) or, when the page gave
|
|
11
|
+
* nothing, a plain round avatar. Its box glides between the large size (waiting, a card to answer, the conclusion) and
|
|
12
|
+
* the small one (writing, history) instead of jumping, and the size it is drawn at is handed to the page's drawing at
|
|
13
|
+
* every step, so the mascot grows and shrinks with its box.
|
|
14
|
+
*/
|
|
15
|
+
import { untrack } from 'svelte';
|
|
16
|
+
import { Tween, prefersReducedMotion } from 'svelte/motion';
|
|
17
|
+
import { cubicOut } from 'svelte/easing';
|
|
18
|
+
import IconSparkles from '@tabler/icons-svelte/icons/sparkles';
|
|
19
|
+
import { useChatLanguage } from '../../i18n/language.js';
|
|
20
|
+
import type { AgentAvatarRenderer, AgentAvatarState } from '../../types/agent.js';
|
|
21
|
+
import { agentAvatarLabelKey, agentAvatarPixels, type AgentAvatarSize } from '../../utils/agentTurn.js';
|
|
22
|
+
import { chatLog, chatWarn } from '../../utils/logger.js';
|
|
23
|
+
import AvatarBoundary from './AvatarBoundary.svelte';
|
|
24
|
+
|
|
25
|
+
// The words of the avatar, in the language of the chat above
|
|
26
|
+
const { t } = useChatLanguage();
|
|
27
|
+
|
|
28
|
+
/** How long the avatar takes to grow or shrink: long enough to follow the move, short enough not to hold anything up. */
|
|
29
|
+
const GLIDE_MILLISECONDS = 420;
|
|
30
|
+
|
|
31
|
+
interface Props {
|
|
32
|
+
/**
|
|
33
|
+
* What the assistant is doing: the avatar's motion and, while it works or after it ended, what a screen reader says. (Not called `state`: a local
|
|
34
|
+
* with that name would turn the `$state` rune below into a store subscription.)
|
|
35
|
+
*/
|
|
36
|
+
avatarState: AgentAvatarState;
|
|
37
|
+
/** The size it rests at. */
|
|
38
|
+
size: AgentAvatarSize;
|
|
39
|
+
/** False for a still picture (older turns). */
|
|
40
|
+
live?: boolean;
|
|
41
|
+
/** The size to start from, when it should glide to `size` as it appears (the conclusion grows out of the small avatar of the work). */
|
|
42
|
+
startSize?: AgentAvatarSize;
|
|
43
|
+
/**
|
|
44
|
+
* The state to be drawn in as it appears, when it takes over from another avatar (the live one of the run). It moves on to
|
|
45
|
+
* `avatarState` right after, so a drawing that blends between states (the mascot) calms or celebrates from what the person
|
|
46
|
+
* was watching instead of appearing in the new state.
|
|
47
|
+
*/
|
|
48
|
+
startState?: AgentAvatarState;
|
|
49
|
+
/** The page's way of drawing the avatar. Without it a plain round avatar is drawn. */
|
|
50
|
+
renderAgentAvatar?: AgentAvatarRenderer;
|
|
51
|
+
class?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let {
|
|
55
|
+
avatarState,
|
|
56
|
+
size,
|
|
57
|
+
live = true,
|
|
58
|
+
startSize = undefined,
|
|
59
|
+
startState = undefined,
|
|
60
|
+
renderAgentAvatar = undefined,
|
|
61
|
+
class: className = ''
|
|
62
|
+
}: Props = $props();
|
|
63
|
+
|
|
64
|
+
// The size it rests at, in pixels, and the size it is at right now on its way there
|
|
65
|
+
const restingPixels = $derived(agentAvatarPixels(size, Boolean(renderAgentAvatar)));
|
|
66
|
+
const sizeGlide = new Tween(
|
|
67
|
+
untrack(() => agentAvatarPixels(startSize ?? size, Boolean(renderAgentAvatar))),
|
|
68
|
+
{ duration: GLIDE_MILLISECONDS, easing: cubicOut }
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
$effect(() => {
|
|
72
|
+
// A person who asked for less motion gets the new size at once
|
|
73
|
+
const duration = prefersReducedMotion.current ? 0 : GLIDE_MILLISECONDS;
|
|
74
|
+
const target = restingPixels;
|
|
75
|
+
untrack(() => {
|
|
76
|
+
void sizeGlide.set(target, { duration });
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Whole pixels only: a drawing that is told a new size at every fraction of a pixel redraws for nothing
|
|
81
|
+
const drawnPixels = $derived(Math.round(sizeGlide.current));
|
|
82
|
+
|
|
83
|
+
// The state the drawing is told. An avatar that takes over from another one is drawn in that one's state for its first pass and
|
|
84
|
+
// then moves on to its own, so a drawing that blends between states does so (the one of the moment, not a swap). With no state
|
|
85
|
+
// to start from it is its own from the start, and nothing extra happens.
|
|
86
|
+
let hasMovedOnFromStart = $state(untrack(() => startState === undefined || startState === avatarState));
|
|
87
|
+
$effect(() => {
|
|
88
|
+
hasMovedOnFromStart = true;
|
|
89
|
+
});
|
|
90
|
+
const drawnState = $derived<AgentAvatarState>(hasMovedOnFromStart ? avatarState : (startState ?? avatarState));
|
|
91
|
+
// What a screen reader says: nothing for an avatar at rest (the words beside it say it all)
|
|
92
|
+
const avatarLabelKey = $derived(agentAvatarLabelKey(avatarState));
|
|
93
|
+
const avatarLabel = $derived(avatarLabelKey ? $t(avatarLabelKey) : '');
|
|
94
|
+
const isWorking = $derived(avatarState === 'thinking' || avatarState === 'writing');
|
|
95
|
+
|
|
96
|
+
// Said once per kind of change (never on every frame of the glide): what the avatar was asked to do
|
|
97
|
+
let lastReportedLook = '';
|
|
98
|
+
$effect(() => {
|
|
99
|
+
const look = `${avatarState}:${size}:${live ? 'live' : 'still'}:${renderAgentAvatar ? 'page' : 'plain'}`;
|
|
100
|
+
if (look === lastReportedLook) return;
|
|
101
|
+
lastReportedLook = look;
|
|
102
|
+
chatLog('[Chat] assistant avatar:', look);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// A page's drawing that breaks (no WebGL, a bug) must never take the chat with it: the plain avatar takes its place
|
|
106
|
+
let pageAvatarFailed = $state(false);
|
|
107
|
+
function handlePageAvatarError(error: unknown): void {
|
|
108
|
+
// Said once for the page (each avatar of a history would otherwise say it again), with the error that came first
|
|
109
|
+
if (!hasReportedPageAvatarFailure) {
|
|
110
|
+
hasReportedPageAvatarFailure = true;
|
|
111
|
+
chatWarn('[Chat] The page’s avatar drawing failed, the plain avatar is shown instead:', error);
|
|
112
|
+
}
|
|
113
|
+
pageAvatarFailed = true;
|
|
114
|
+
}
|
|
115
|
+
</script>
|
|
116
|
+
|
|
117
|
+
{#snippet plainAvatar()}
|
|
118
|
+
<span
|
|
119
|
+
class="bg-muted text-foreground flex size-full items-center justify-center rounded-full {isWorking
|
|
120
|
+
? 'motion-safe:animate-pulse'
|
|
121
|
+
: ''}"
|
|
122
|
+
>
|
|
123
|
+
<IconSparkles
|
|
124
|
+
class="stroke-2"
|
|
125
|
+
style="width: {Math.round(drawnPixels * 0.5)}px; height: {Math.round(drawnPixels * 0.5)}px;"
|
|
126
|
+
aria-hidden="true"
|
|
127
|
+
/>
|
|
128
|
+
</span>
|
|
129
|
+
{/snippet}
|
|
130
|
+
|
|
131
|
+
<span
|
|
132
|
+
class="agent-avatar-box relative inline-flex shrink-0 items-center justify-center {className}"
|
|
133
|
+
style="width: {drawnPixels}px; height: {drawnPixels}px;"
|
|
134
|
+
role={avatarLabel ? 'img' : undefined}
|
|
135
|
+
aria-label={avatarLabel || undefined}
|
|
136
|
+
aria-hidden={avatarLabel ? undefined : 'true'}
|
|
137
|
+
data-testid="agent-avatar"
|
|
138
|
+
data-agent-avatar-state={avatarState}
|
|
139
|
+
data-agent-avatar-size={size}
|
|
140
|
+
data-agent-avatar-live={live}
|
|
141
|
+
>
|
|
142
|
+
{#if renderAgentAvatar && !pageAvatarFailed}
|
|
143
|
+
{@const drawPageAvatar = renderAgentAvatar}
|
|
144
|
+
<AvatarBoundary onerror={handlePageAvatarError}>
|
|
145
|
+
{@render drawPageAvatar({ state: drawnState, size: drawnPixels, live, label: avatarLabel })}
|
|
146
|
+
{#snippet fallback()}
|
|
147
|
+
{@render plainAvatar()}
|
|
148
|
+
{/snippet}
|
|
149
|
+
</AvatarBoundary>
|
|
150
|
+
{:else}
|
|
151
|
+
{@render plainAvatar()}
|
|
152
|
+
{/if}
|
|
153
|
+
</span>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AgentAvatarRenderer, AgentAvatarState } from '../../types/agent.js';
|
|
2
|
+
import { type AgentAvatarSize } from '../../utils/agentTurn.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
/**
|
|
5
|
+
* What the assistant is doing: the avatar's motion and, while it works or after it ended, what a screen reader says. (Not called `state`: a local
|
|
6
|
+
* with that name would turn the `$state` rune below into a store subscription.)
|
|
7
|
+
*/
|
|
8
|
+
avatarState: AgentAvatarState;
|
|
9
|
+
/** The size it rests at. */
|
|
10
|
+
size: AgentAvatarSize;
|
|
11
|
+
/** False for a still picture (older turns). */
|
|
12
|
+
live?: boolean;
|
|
13
|
+
/** The size to start from, when it should glide to `size` as it appears (the conclusion grows out of the small avatar of the work). */
|
|
14
|
+
startSize?: AgentAvatarSize;
|
|
15
|
+
/**
|
|
16
|
+
* The state to be drawn in as it appears, when it takes over from another avatar (the live one of the run). It moves on to
|
|
17
|
+
* `avatarState` right after, so a drawing that blends between states (the mascot) calms or celebrates from what the person
|
|
18
|
+
* was watching instead of appearing in the new state.
|
|
19
|
+
*/
|
|
20
|
+
startState?: AgentAvatarState;
|
|
21
|
+
/** The page's way of drawing the avatar. Without it a plain round avatar is drawn. */
|
|
22
|
+
renderAgentAvatar?: AgentAvatarRenderer;
|
|
23
|
+
class?: string;
|
|
24
|
+
}
|
|
25
|
+
declare const AgentAvatar: import("svelte").Component<Props, {}, "">;
|
|
26
|
+
type AgentAvatar = ReturnType<typeof AgentAvatar>;
|
|
27
|
+
export default AgentAvatar;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* What the assistant says in one turn, with its face: the avatar at the left and everything it said or showed to its right.
|
|
4
|
+
* There is no name over it (it is clear who is speaking): the words are the content's own, "Thinking…", "Writing code…".
|
|
5
|
+
*
|
|
6
|
+
* The same row holds the large avatar of the moments that matter (the card to answer or the plan sits to its right) and the
|
|
7
|
+
* small one of the work (the text sits beside it), so going from one to the other only changes the size of the avatar: the
|
|
8
|
+
* content slides along with it, nothing is rebuilt.
|
|
9
|
+
*
|
|
10
|
+
* The content starts at the avatar's top, whatever its height: the first thing in it (a line of text, "Thinking…", a one-line
|
|
11
|
+
* failure, a card) is level with the top of the avatar, beside the large one as well as beside the small one. A first line is
|
|
12
|
+
* as tall as the small avatar (`min-h-7`, the 28 pixels of `AGENT_AVATAR_PIXELS.small`) with its text in the middle of it, so
|
|
13
|
+
* beside the small avatar a single line is also centred on it, and beside the large one it does not sink to the middle of it.
|
|
14
|
+
*/
|
|
15
|
+
import type { Snippet } from 'svelte';
|
|
16
|
+
import type { AgentAvatarRenderer, AgentAvatarState } from '../../types/agent.js';
|
|
17
|
+
import type { AgentAvatarSize, AgentLook } from '../../utils/agentTurn.js';
|
|
18
|
+
import AgentAvatar from './AgentAvatar.svelte';
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
/** What the avatar looks like and does. */
|
|
22
|
+
look: AgentLook;
|
|
23
|
+
/** The page's way of drawing the avatar (see `AgentAvatarRenderer`). */
|
|
24
|
+
renderAgentAvatar?: AgentAvatarRenderer;
|
|
25
|
+
/** The size the avatar glides from as it appears (the conclusion grows out of the small avatar of the work). */
|
|
26
|
+
startSize?: AgentAvatarSize;
|
|
27
|
+
/** The state the avatar is drawn in as it appears, before it moves on to its own (see `AgentAvatar`). */
|
|
28
|
+
startState?: AgentAvatarState;
|
|
29
|
+
class?: string;
|
|
30
|
+
/** Everything the assistant said or showed in this turn. */
|
|
31
|
+
children: Snippet;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let {
|
|
35
|
+
look,
|
|
36
|
+
renderAgentAvatar = undefined,
|
|
37
|
+
startSize = undefined,
|
|
38
|
+
startState = undefined,
|
|
39
|
+
class: className = '',
|
|
40
|
+
children
|
|
41
|
+
}: Props = $props();
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<!--
|
|
45
|
+
Two boxes side by side: the avatar (its box sets the width of the first, so the content follows it while it glides), and the
|
|
46
|
+
content. The row is as tall as the taller of the two; both start at the top of it, so the first line of the content is level
|
|
47
|
+
with the top of the avatar.
|
|
48
|
+
-->
|
|
49
|
+
<div
|
|
50
|
+
class="agent-turn flex w-full items-start gap-3 {className}"
|
|
51
|
+
data-testid="agent-turn"
|
|
52
|
+
data-agent-turn-state={look.state}
|
|
53
|
+
data-agent-turn-size={look.size}
|
|
54
|
+
>
|
|
55
|
+
<!-- A flex box, not a block: the avatar is an inline box, and in a block the line it sits on is taller than it (the strut of the
|
|
56
|
+
line height hangs under it), which made this column a few pixels taller than the avatar and its middle fall below its own. -->
|
|
57
|
+
<div class="flex shrink-0 self-start">
|
|
58
|
+
<AgentAvatar avatarState={look.state} size={look.size} live={look.live} {startSize} {startState} {renderAgentAvatar} />
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
<div class="flex min-w-0 flex-1 flex-col gap-2 self-start" data-testid="agent-turn-content">
|
|
62
|
+
{@render children()}
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the assistant says in one turn, with its face: the avatar at the left and everything it said or showed to its right.
|
|
3
|
+
* There is no name over it (it is clear who is speaking): the words are the content's own, "Thinking…", "Writing code…".
|
|
4
|
+
*
|
|
5
|
+
* The same row holds the large avatar of the moments that matter (the card to answer or the plan sits to its right) and the
|
|
6
|
+
* small one of the work (the text sits beside it), so going from one to the other only changes the size of the avatar: the
|
|
7
|
+
* content slides along with it, nothing is rebuilt.
|
|
8
|
+
*
|
|
9
|
+
* The content starts at the avatar's top, whatever its height: the first thing in it (a line of text, "Thinking…", a one-line
|
|
10
|
+
* failure, a card) is level with the top of the avatar, beside the large one as well as beside the small one. A first line is
|
|
11
|
+
* as tall as the small avatar (`min-h-7`, the 28 pixels of `AGENT_AVATAR_PIXELS.small`) with its text in the middle of it, so
|
|
12
|
+
* beside the small avatar a single line is also centred on it, and beside the large one it does not sink to the middle of it.
|
|
13
|
+
*/
|
|
14
|
+
import type { Snippet } from 'svelte';
|
|
15
|
+
import type { AgentAvatarRenderer, AgentAvatarState } from '../../types/agent.js';
|
|
16
|
+
import type { AgentAvatarSize, AgentLook } from '../../utils/agentTurn.js';
|
|
17
|
+
interface Props {
|
|
18
|
+
/** What the avatar looks like and does. */
|
|
19
|
+
look: AgentLook;
|
|
20
|
+
/** The page's way of drawing the avatar (see `AgentAvatarRenderer`). */
|
|
21
|
+
renderAgentAvatar?: AgentAvatarRenderer;
|
|
22
|
+
/** The size the avatar glides from as it appears (the conclusion grows out of the small avatar of the work). */
|
|
23
|
+
startSize?: AgentAvatarSize;
|
|
24
|
+
/** The state the avatar is drawn in as it appears, before it moves on to its own (see `AgentAvatar`). */
|
|
25
|
+
startState?: AgentAvatarState;
|
|
26
|
+
class?: string;
|
|
27
|
+
/** Everything the assistant said or showed in this turn. */
|
|
28
|
+
children: Snippet;
|
|
29
|
+
}
|
|
30
|
+
declare const AgentTurn: import("svelte").Component<Props, {}, "">;
|
|
31
|
+
type AgentTurn = ReturnType<typeof AgentTurn>;
|
|
32
|
+
export default AgentTurn;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Keeps a drawing that breaks from taking the chat down with it: what is inside is drawn as long as nothing throws, and
|
|
4
|
+
* `fallback` is drawn in its place (and `onerror` is told) the moment something does.
|
|
5
|
+
*
|
|
6
|
+
* It is a file of its own on purpose. `<svelte:boundary>` (Svelte 5.3) is not known to the `svelte-eslint-parser` the chat's
|
|
7
|
+
* linter uses (0.43): it stops at the tag with a parse error. Only this file is left out of the linter (`eslint.config.js`),
|
|
8
|
+
* until the parser is updated, instead of the whole avatar.
|
|
9
|
+
*/
|
|
10
|
+
import type { Snippet } from 'svelte';
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
/** What to draw. */
|
|
14
|
+
children: Snippet;
|
|
15
|
+
/** What to draw instead when `children` throws. */
|
|
16
|
+
fallback: Snippet;
|
|
17
|
+
/** Told with the error when something inside throws. */
|
|
18
|
+
onerror?: (error: unknown) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let { children, fallback, onerror }: Props = $props();
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<svelte:boundary {onerror}>
|
|
25
|
+
{@render children()}
|
|
26
|
+
{#snippet failed()}
|
|
27
|
+
{@render fallback()}
|
|
28
|
+
{/snippet}
|
|
29
|
+
</svelte:boundary>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps a drawing that breaks from taking the chat down with it: what is inside is drawn as long as nothing throws, and
|
|
3
|
+
* `fallback` is drawn in its place (and `onerror` is told) the moment something does.
|
|
4
|
+
*
|
|
5
|
+
* It is a file of its own on purpose. `<svelte:boundary>` (Svelte 5.3) is not known to the `svelte-eslint-parser` the chat's
|
|
6
|
+
* linter uses (0.43): it stops at the tag with a parse error. Only this file is left out of the linter (`eslint.config.js`),
|
|
7
|
+
* until the parser is updated, instead of the whole avatar.
|
|
8
|
+
*/
|
|
9
|
+
import type { Snippet } from 'svelte';
|
|
10
|
+
interface Props {
|
|
11
|
+
/** What to draw. */
|
|
12
|
+
children: Snippet;
|
|
13
|
+
/** What to draw instead when `children` throws. */
|
|
14
|
+
fallback: Snippet;
|
|
15
|
+
/** Told with the error when something inside throws. */
|
|
16
|
+
onerror?: (error: unknown) => void;
|
|
17
|
+
}
|
|
18
|
+
declare const AvatarBoundary: import("svelte").Component<Props, {}, "">;
|
|
19
|
+
type AvatarBoundary = ReturnType<typeof AvatarBoundary>;
|
|
20
|
+
export default AvatarBoundary;
|