@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
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The code the assistant writes, as a card of the chat: the look of the design system's cards (white, a border, round
|
|
4
|
+
* corners) with the code in a grey panel, in the font code editors use, its words coloured by highlight.js.
|
|
5
|
+
*
|
|
6
|
+
* While the code arrives, Prettier lays out what has arrived so far (see `codeStreamFormat`): the code is tidy as it is
|
|
7
|
+
* written, and the line being typed shows as it comes. Prettier is loaded the first time a card is open on a script, and
|
|
8
|
+
* not before. Code that is not a script (a page, a Svelte component) is shown as it was written, coloured.
|
|
9
|
+
*
|
|
10
|
+
* It is open while it is the part being written and closed once the next part (or the conclusion) has taken over; the
|
|
11
|
+
* person's own choice wins from the moment they press it. Under a conclusion it comes back closed, for the person to open.
|
|
12
|
+
*/
|
|
13
|
+
import { onDestroy, tick } from 'svelte';
|
|
14
|
+
import IconCheck from '@tabler/icons-svelte/icons/check';
|
|
15
|
+
import IconChevronRight from '@tabler/icons-svelte/icons/chevron-right';
|
|
16
|
+
import IconCode from '@tabler/icons-svelte/icons/code';
|
|
17
|
+
import IconCopy from '@tabler/icons-svelte/icons/copy';
|
|
18
|
+
import { useChatLanguage } from '../../i18n/language.js';
|
|
19
|
+
import { highlightCodeToHtml } from '../../utils/codeHighlight.js';
|
|
20
|
+
import { formatScriptWithCursor } from '../../utils/codePrettier.js';
|
|
21
|
+
import { countCodeLines, createStreamingFormatter, detectCodeKind } from '../../utils/codeStreamFormat.js';
|
|
22
|
+
import { chatLog, chatWarn } from '../../utils/logger.js';
|
|
23
|
+
|
|
24
|
+
// The words of the card, in the language of the chat above
|
|
25
|
+
const { t } = useChatLanguage();
|
|
26
|
+
|
|
27
|
+
interface Props {
|
|
28
|
+
/** Everything written so far. */
|
|
29
|
+
code: string;
|
|
30
|
+
/** The code is still arriving: the footer says so, the newest line stays in view and Prettier follows it. */
|
|
31
|
+
streaming?: boolean;
|
|
32
|
+
/** Open until the person chooses (the live part), or closed until they do (the part under a conclusion). */
|
|
33
|
+
openByDefault?: boolean;
|
|
34
|
+
class?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let { code, streaming = false, openByDefault = false, class: className = '' }: Props = $props();
|
|
38
|
+
|
|
39
|
+
/** How often Prettier follows the code while it arrives (a run takes some tens of milliseconds on a long file). */
|
|
40
|
+
const FORMAT_INTERVAL_MS = 250;
|
|
41
|
+
/** Longer code is shown as it was written: laying it out again and again would cost more than it is worth. */
|
|
42
|
+
const MAX_FORMATTED_CHARACTERS = 60_000;
|
|
43
|
+
/** How close to the end of the panel the person has to be for the newest line to stay in view as the code grows. */
|
|
44
|
+
const FOLLOW_THRESHOLD_PIXELS = 24;
|
|
45
|
+
|
|
46
|
+
// What the person chose, once they have chosen: from then on it wins over the default
|
|
47
|
+
let chosenOpen = $state<boolean | null>(null);
|
|
48
|
+
const isOpen = $derived(chosenOpen ?? openByDefault);
|
|
49
|
+
|
|
50
|
+
const bodyId = $props.id();
|
|
51
|
+
let panelElement = $state<HTMLElement | null>(null);
|
|
52
|
+
let isFollowingNewestLine = true;
|
|
53
|
+
|
|
54
|
+
// What the code is decides its colours and whether Prettier can lay it out
|
|
55
|
+
const codeKind = $derived(detectCodeKind(code));
|
|
56
|
+
|
|
57
|
+
// --- Prettier, following the code ---------------------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
/** The start of the code as Prettier laid it out, and the source it stands for. */
|
|
60
|
+
let formattedStart = $state<{ sourcePrefix: string; text: string } | null>(null);
|
|
61
|
+
|
|
62
|
+
const streamingFormatter = createStreamingFormatter(formatScriptWithCursor);
|
|
63
|
+
let formatTimer: ReturnType<typeof setTimeout> | null = null;
|
|
64
|
+
let isFormatting = false;
|
|
65
|
+
let isFormattingAgainNeeded = false;
|
|
66
|
+
let isDestroyed = false;
|
|
67
|
+
let hasReportedFormatFailure = false;
|
|
68
|
+
// What the next run reads: the newest code, not the code of the moment the run was asked for
|
|
69
|
+
let newestCode = '';
|
|
70
|
+
let newestCodeIsComplete = false;
|
|
71
|
+
|
|
72
|
+
/** Runs Prettier on the newest code. Runs never overlap: one that is asked for during another follows it. */
|
|
73
|
+
async function runFormatting(): Promise<void> {
|
|
74
|
+
if (isFormatting) {
|
|
75
|
+
isFormattingAgainNeeded = true;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
isFormatting = true;
|
|
79
|
+
try {
|
|
80
|
+
do {
|
|
81
|
+
isFormattingAgainNeeded = false;
|
|
82
|
+
const sourceToFormat = newestCode;
|
|
83
|
+
const sourceIsComplete = newestCodeIsComplete;
|
|
84
|
+
const startedAt = performance.now();
|
|
85
|
+
const result = await streamingFormatter.format(sourceToFormat, sourceIsComplete);
|
|
86
|
+
if (isDestroyed) return;
|
|
87
|
+
formattedStart = result.isFormatted
|
|
88
|
+
? { sourcePrefix: sourceToFormat.slice(0, result.formattedSourceLength), text: result.formattedStart }
|
|
89
|
+
: null;
|
|
90
|
+
if (sourceIsComplete) {
|
|
91
|
+
chatLog(
|
|
92
|
+
'[Chat] code card: laid out',
|
|
93
|
+
sourceToFormat.length,
|
|
94
|
+
'characters in',
|
|
95
|
+
Math.round(performance.now() - startedAt),
|
|
96
|
+
'ms:',
|
|
97
|
+
result.isFormatted ? 'formatted' : 'shown as written'
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
} while (isFormattingAgainNeeded);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
// The formatter does not reject; this is a guard for the load of Prettier
|
|
103
|
+
if (!hasReportedFormatFailure) {
|
|
104
|
+
hasReportedFormatFailure = true;
|
|
105
|
+
chatWarn('[Chat] code card: the code could not be laid out, it is shown as it was written:', error);
|
|
106
|
+
}
|
|
107
|
+
formattedStart = null;
|
|
108
|
+
} finally {
|
|
109
|
+
isFormatting = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Follow the code: while it arrives, a run every FORMAT_INTERVAL_MS; once it is complete, one at once
|
|
114
|
+
$effect(() => {
|
|
115
|
+
const codeToFormat = code;
|
|
116
|
+
const codeIsComplete = !streaming;
|
|
117
|
+
const canFormat = isOpen && codeKind === 'script' && codeToFormat.length <= MAX_FORMATTED_CHARACTERS;
|
|
118
|
+
if (!canFormat) return;
|
|
119
|
+
newestCode = codeToFormat;
|
|
120
|
+
newestCodeIsComplete = codeIsComplete;
|
|
121
|
+
if (codeIsComplete) {
|
|
122
|
+
if (formatTimer !== null) clearTimeout(formatTimer);
|
|
123
|
+
formatTimer = null;
|
|
124
|
+
void runFormatting();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (formatTimer === null) {
|
|
128
|
+
formatTimer = setTimeout(() => {
|
|
129
|
+
formatTimer = null;
|
|
130
|
+
void runFormatting();
|
|
131
|
+
}, FORMAT_INTERVAL_MS);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// --- What the card shows ------------------------------------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
// The formatted start stands for the start of the code only while the code still starts with the source it was made from
|
|
138
|
+
const isFormattedStartValid = $derived(formattedStart !== null && code.startsWith(formattedStart.sourcePrefix));
|
|
139
|
+
|
|
140
|
+
/** The formatted start, then what has been written since as it was written. */
|
|
141
|
+
const visibleText = $derived.by(() => {
|
|
142
|
+
if (!isFormattedStartValid || formattedStart === null) return code;
|
|
143
|
+
const writtenAfter = code.slice(formattedStart.sourcePrefix.length);
|
|
144
|
+
return writtenAfter === '' ? formattedStart.text : `${formattedStart.text}\n${writtenAfter}`;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// The lines that are done are coloured once, when the line after them begins; only the line being typed is coloured as it comes
|
|
148
|
+
const lastLineBreakIndex = $derived(visibleText.lastIndexOf('\n'));
|
|
149
|
+
const settledText = $derived(lastLineBreakIndex === -1 ? '' : visibleText.slice(0, lastLineBreakIndex));
|
|
150
|
+
const typingText = $derived(visibleText.slice(lastLineBreakIndex + 1));
|
|
151
|
+
const settledHtml = $derived(highlightCodeToHtml(settledText, codeKind));
|
|
152
|
+
const typingHtml = $derived(highlightCodeToHtml(typingText, codeKind));
|
|
153
|
+
const lineCount = $derived(countCodeLines(visibleText));
|
|
154
|
+
|
|
155
|
+
// What the buttons say, for a screen reader (the title says what the card is)
|
|
156
|
+
const toggleLabel = $derived(isOpen ? $t('chat.generation.collapseCode') : $t('chat.generation.expandCode'));
|
|
157
|
+
|
|
158
|
+
function toggleOpen(): void {
|
|
159
|
+
chosenOpen = !isOpen;
|
|
160
|
+
chatLog('[Chat] code card', chosenOpen ? 'opened' : 'closed', 'by the person');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// --- The newest line stays in view ------------------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
// The person who scrolls up to read is not pulled back down; reaching the end again makes the card follow the code once more
|
|
166
|
+
function updateFollowing(): void {
|
|
167
|
+
if (!panelElement) return;
|
|
168
|
+
const distanceFromEnd = panelElement.scrollHeight - panelElement.scrollTop - panelElement.clientHeight;
|
|
169
|
+
isFollowingNewestLine = distanceFromEnd < FOLLOW_THRESHOLD_PIXELS;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
$effect(() => {
|
|
173
|
+
if (!streaming || !isOpen || !panelElement) return;
|
|
174
|
+
if (visibleText.length === 0 || !isFollowingNewestLine) return;
|
|
175
|
+
tick()
|
|
176
|
+
.then(() => {
|
|
177
|
+
if (panelElement) panelElement.scrollTop = panelElement.scrollHeight;
|
|
178
|
+
})
|
|
179
|
+
.catch((error) => console.warn('[Chat] Could not keep the newest line of the code in view:', error));
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// --- Copy -----------------------------------------------------------------------------------------------------------------
|
|
183
|
+
|
|
184
|
+
let hasCopied = $state(false);
|
|
185
|
+
let copiedTimer: ReturnType<typeof setTimeout> | null = null;
|
|
186
|
+
|
|
187
|
+
async function copyCode(): Promise<void> {
|
|
188
|
+
try {
|
|
189
|
+
await navigator.clipboard.writeText(visibleText);
|
|
190
|
+
hasCopied = true;
|
|
191
|
+
chatLog('[Chat] code card: the code was copied');
|
|
192
|
+
if (copiedTimer !== null) clearTimeout(copiedTimer);
|
|
193
|
+
copiedTimer = setTimeout(() => {
|
|
194
|
+
hasCopied = false;
|
|
195
|
+
copiedTimer = null;
|
|
196
|
+
}, 2000);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
chatWarn('[Chat] code card: the code could not be copied:', error);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
onDestroy(() => {
|
|
203
|
+
isDestroyed = true;
|
|
204
|
+
if (formatTimer !== null) clearTimeout(formatTimer);
|
|
205
|
+
if (copiedTimer !== null) clearTimeout(copiedTimer);
|
|
206
|
+
});
|
|
207
|
+
</script>
|
|
208
|
+
|
|
209
|
+
<div
|
|
210
|
+
class="rounded-card border-border bg-background flex w-full max-w-full min-w-0 flex-col border {isOpen
|
|
211
|
+
? 'gap-3 p-4'
|
|
212
|
+
: 'px-4 py-2'} {className}"
|
|
213
|
+
data-testid="code-card"
|
|
214
|
+
data-open={isOpen}
|
|
215
|
+
data-streaming={streaming}
|
|
216
|
+
data-formatted={isFormattedStartValid}
|
|
217
|
+
>
|
|
218
|
+
<header class="flex min-w-0 items-center gap-2">
|
|
219
|
+
<button
|
|
220
|
+
type="button"
|
|
221
|
+
class="text-foreground focus-visible:ring-ring flex min-h-7 min-w-0 flex-1 items-center gap-2 rounded-sm text-start focus-visible:ring-2 focus-visible:outline-none"
|
|
222
|
+
aria-expanded={isOpen}
|
|
223
|
+
aria-controls={bodyId}
|
|
224
|
+
onclick={toggleOpen}
|
|
225
|
+
>
|
|
226
|
+
<IconCode class="size-4 shrink-0 stroke-2" aria-hidden="true" />
|
|
227
|
+
<span class="text-body-medium min-w-0 flex-1 truncate">{$t('chat.generation.codeTitle')}</span>
|
|
228
|
+
<IconChevronRight
|
|
229
|
+
class="text-muted-foreground size-4 shrink-0 stroke-2 transition-transform motion-reduce:transition-none rtl:-scale-x-100 {isOpen
|
|
230
|
+
? 'rotate-90 rtl:-rotate-90'
|
|
231
|
+
: ''}"
|
|
232
|
+
aria-hidden="true"
|
|
233
|
+
/>
|
|
234
|
+
<span class="sr-only">{toggleLabel}</span>
|
|
235
|
+
</button>
|
|
236
|
+
{#if isOpen && visibleText !== ''}
|
|
237
|
+
<button
|
|
238
|
+
type="button"
|
|
239
|
+
class="text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:bg-accent focus-visible:text-foreground flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-md transition-colors focus-visible:outline-none"
|
|
240
|
+
title={hasCopied ? $t('chat.code.copied') : $t('chat.code.copyTitle')}
|
|
241
|
+
aria-label={hasCopied ? $t('chat.code.copied') : $t('chat.code.copyTitle')}
|
|
242
|
+
onclick={copyCode}
|
|
243
|
+
data-testid="code-card-copy"
|
|
244
|
+
>
|
|
245
|
+
{#if hasCopied}
|
|
246
|
+
<IconCheck class="size-4 stroke-2" aria-hidden="true" />
|
|
247
|
+
{:else}
|
|
248
|
+
<IconCopy class="size-4 stroke-2" aria-hidden="true" />
|
|
249
|
+
{/if}
|
|
250
|
+
</button>
|
|
251
|
+
{/if}
|
|
252
|
+
</header>
|
|
253
|
+
|
|
254
|
+
{#if isOpen}
|
|
255
|
+
<div id={bodyId} class="flex min-w-0 flex-col gap-3">
|
|
256
|
+
<!-- The colours of the words: keywords blue, tags and strings maroon, the attributes of a tag red-orange (highlight.js also calls
|
|
257
|
+
`name:` in a parameter list an attribute, so only the ones inside a tag are painted), comments muted. Their classes are
|
|
258
|
+
the card's own `wc-code-*`, not the ones of highlight.js: see CODE_TOKEN_CLASS_PREFIX. The two pieces of html in the panel are
|
|
259
|
+
the only ones in the card, and safe: `highlightCodeToHtml` escapes every character of the code, and the only tags it writes
|
|
260
|
+
are the `span`s of the words. -->
|
|
261
|
+
<!-- The panel scrolls, so the keyboard has to be able to reach it -->
|
|
262
|
+
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
|
|
263
|
+
<pre
|
|
264
|
+
bind:this={panelElement}
|
|
265
|
+
onscroll={updateFollowing}
|
|
266
|
+
tabindex="0"
|
|
267
|
+
aria-label={$t('chat.generation.codeTitle')}
|
|
268
|
+
class="bg-muted text-foreground m-0 max-h-72 w-full overflow-auto rounded-lg p-3 font-[ui-monospace,SFMono-Regular,'SF_Mono',Menlo,Monaco,Consolas,'Liberation_Mono','Courier_New',monospace] text-[12px] leading-[18px] break-words whitespace-pre-wrap [tab-size:2] [scrollbar-color:var(--border)_transparent] [scrollbar-width:thin] [&_.wc-code-tag_.wc-code-attr]:text-[#e5462b] [&_.wc-code-comment]:text-muted-foreground [&_.wc-code-comment]:italic [&_.wc-code-keyword]:text-[#2b5fc2] [&_.wc-code-literal]:text-[#2b5fc2] [&_.wc-code-meta]:text-muted-foreground [&_.wc-code-name]:text-[#8d2a1b] [&_.wc-code-number]:text-[#2b5fc2] [&_.wc-code-regexp]:text-[#8d2a1b] [&_.wc-code-string]:text-[#8d2a1b] [&_.wc-code-tag]:text-[#8d2a1b] [&_.wc-code-title]:text-[#2b5fc2] [.dark_&_.wc-code-tag_.wc-code-attr]:text-[#ff8a73] [.dark_&_.wc-code-keyword]:text-[#8ab4ff] [.dark_&_.wc-code-literal]:text-[#8ab4ff] [.dark_&_.wc-code-name]:text-[#f0a89a] [.dark_&_.wc-code-number]:text-[#8ab4ff] [.dark_&_.wc-code-regexp]:text-[#f0a89a] [.dark_&_.wc-code-string]:text-[#f0a89a] [.dark_&_.wc-code-tag]:text-[#f0a89a] [.dark_&_.wc-code-title]:text-[#8ab4ff]"
|
|
269
|
+
dir="ltr"
|
|
270
|
+
data-testid="code-card-text"><code
|
|
271
|
+
>{@html settledHtml}{lastLineBreakIndex === -1 ? '' : '\n'}{@html typingHtml}</code
|
|
272
|
+
></pre>
|
|
273
|
+
<footer class="text-caption text-muted-foreground flex flex-col gap-0.5" data-testid="code-card-status">
|
|
274
|
+
<span class="flex items-center gap-2">
|
|
275
|
+
<span
|
|
276
|
+
class="bg-brand size-2 shrink-0 rounded-full {streaming ? 'animate-pulse motion-reduce:animate-none' : ''}"
|
|
277
|
+
aria-hidden="true"
|
|
278
|
+
></span>
|
|
279
|
+
<span>{streaming ? $t('chat.generation.writingCode') : $t('chat.generation.codeWritten')}</span>
|
|
280
|
+
</span>
|
|
281
|
+
<span class="flex items-center gap-2 ps-0.5">
|
|
282
|
+
<span class="border-border h-2.5 w-2 shrink-0 border-s border-b" aria-hidden="true"></span>
|
|
283
|
+
<span>{$t('chat.generation.codeLineCount', { count: lineCount })}</span>
|
|
284
|
+
</span>
|
|
285
|
+
</footer>
|
|
286
|
+
</div>
|
|
287
|
+
{/if}
|
|
288
|
+
</div>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
/** Everything written so far. */
|
|
3
|
+
code: string;
|
|
4
|
+
/** The code is still arriving: the footer says so, the newest line stays in view and Prettier follows it. */
|
|
5
|
+
streaming?: boolean;
|
|
6
|
+
/** Open until the person chooses (the live part), or closed until they do (the part under a conclusion). */
|
|
7
|
+
openByDefault?: boolean;
|
|
8
|
+
class?: string;
|
|
9
|
+
}
|
|
10
|
+
declare const CodeCard: import("svelte").Component<Props, {}, "">;
|
|
11
|
+
type CodeCard = ReturnType<typeof CodeCard>;
|
|
12
|
+
export default CodeCard;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* One part of the assistant's work, shown in the chat and folded away with one press: its thinking, as plain text like a
|
|
4
|
+
* message, or the code it wrote, as a code card (see `CodeCard`, which draws the code kind: Prettier lays the code out as it
|
|
5
|
+
* is written and highlight.js colours it).
|
|
6
|
+
*
|
|
7
|
+
* It is open while it is the part being written and closed once the next part (or the conclusion) has taken over; the
|
|
8
|
+
* person's own choice wins from the moment they press it. Under a conclusion it comes back closed, for the person to open.
|
|
9
|
+
*/
|
|
10
|
+
import { tick } from 'svelte';
|
|
11
|
+
import IconChevronRight from '@tabler/icons-svelte/icons/chevron-right';
|
|
12
|
+
import { useChatLanguage } from '../../i18n/language.js';
|
|
13
|
+
import { chatLog } from '../../utils/logger.js';
|
|
14
|
+
import CodeCard from './CodeCard.svelte';
|
|
15
|
+
|
|
16
|
+
// The words of the module, in the language of the chat above
|
|
17
|
+
const { t } = useChatLanguage();
|
|
18
|
+
|
|
19
|
+
interface Props {
|
|
20
|
+
/** What this part of the work is. */
|
|
21
|
+
kind: 'thinking' | 'code';
|
|
22
|
+
/** Everything written so far. */
|
|
23
|
+
text: string;
|
|
24
|
+
/** The text is still arriving: the title says so, and the newest line stays in view. */
|
|
25
|
+
streaming?: boolean;
|
|
26
|
+
/** Whole seconds the thinking took, once it is over. */
|
|
27
|
+
seconds?: number | null;
|
|
28
|
+
/** Open until the person chooses (the live part), or closed until they do (the part under a conclusion). */
|
|
29
|
+
openByDefault?: boolean;
|
|
30
|
+
class?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let { kind, text, streaming = false, seconds = null, openByDefault = false, class: className = '' }: Props = $props();
|
|
34
|
+
|
|
35
|
+
// What the person chose, once they have chosen: from then on it wins over the default
|
|
36
|
+
let chosenOpen = $state<boolean | null>(null);
|
|
37
|
+
const isOpen = $derived(chosenOpen ?? openByDefault);
|
|
38
|
+
|
|
39
|
+
const bodyId = $props.id();
|
|
40
|
+
let bodyElement = $state<HTMLElement | null>(null);
|
|
41
|
+
|
|
42
|
+
// (The code kind has its own card, which says what it is itself.)
|
|
43
|
+
const title = $derived.by(() => {
|
|
44
|
+
if (!streaming && seconds != null) return $t('chat.generation.thoughtFor', { seconds: String(seconds) });
|
|
45
|
+
return $t('chat.generation.thinking');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// What the button does, for a screen reader (the title says what it is)
|
|
49
|
+
const toggleLabel = $derived(isOpen ? $t('chat.generation.collapseThought') : $t('chat.generation.expandThought'));
|
|
50
|
+
|
|
51
|
+
function toggleOpen(): void {
|
|
52
|
+
chosenOpen = !isOpen;
|
|
53
|
+
chatLog('[Chat] generation module', kind, chosenOpen ? 'opened' : 'closed', 'by the person');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// While the text arrives, keep its newest line in view
|
|
57
|
+
$effect(() => {
|
|
58
|
+
if (!streaming || !isOpen || !bodyElement) return;
|
|
59
|
+
if (text.length === 0) return;
|
|
60
|
+
tick()
|
|
61
|
+
.then(() => {
|
|
62
|
+
if (bodyElement) bodyElement.scrollTop = bodyElement.scrollHeight;
|
|
63
|
+
})
|
|
64
|
+
.catch((error) => console.warn('[Chat] Could not keep the newest line in view:', error));
|
|
65
|
+
});
|
|
66
|
+
</script>
|
|
67
|
+
|
|
68
|
+
{#if kind === 'code'}
|
|
69
|
+
<!-- The code is a card of its own (open, closed, copy, the status line): see CodeCard -->
|
|
70
|
+
<CodeCard code={text} {streaming} {openByDefault} class={className} />
|
|
71
|
+
{:else}
|
|
72
|
+
<div class="flex min-w-0 flex-col {className}" data-testid="generation-module" data-generation-module={kind} data-open={isOpen}>
|
|
73
|
+
<button
|
|
74
|
+
type="button"
|
|
75
|
+
class="text-caption-medium text-muted-foreground hover:text-foreground focus-visible:ring-ring flex min-h-7 w-fit max-w-full items-center gap-1 rounded-sm text-start transition-colors focus-visible:ring-2 focus-visible:outline-none"
|
|
76
|
+
aria-expanded={isOpen}
|
|
77
|
+
aria-controls={bodyId}
|
|
78
|
+
onclick={toggleOpen}
|
|
79
|
+
>
|
|
80
|
+
<IconChevronRight
|
|
81
|
+
class="size-4 shrink-0 stroke-2 transition-transform motion-reduce:transition-none rtl:-scale-x-100 {isOpen
|
|
82
|
+
? 'rotate-90 rtl:-rotate-90'
|
|
83
|
+
: ''}"
|
|
84
|
+
aria-hidden="true"
|
|
85
|
+
/>
|
|
86
|
+
<span class="min-w-0 truncate">{title}</span>
|
|
87
|
+
<span class="sr-only">{toggleLabel}</span>
|
|
88
|
+
</button>
|
|
89
|
+
<div id={bodyId} class="min-w-0">
|
|
90
|
+
{#if isOpen}
|
|
91
|
+
<div
|
|
92
|
+
bind:this={bodyElement}
|
|
93
|
+
class="text-caption text-muted-foreground max-h-40 overflow-auto ps-5 break-words whitespace-pre-wrap"
|
|
94
|
+
dir="auto"
|
|
95
|
+
data-testid="generation-thinking-text">{text}</div>
|
|
96
|
+
{/if}
|
|
97
|
+
</div>
|
|
98
|
+
</div>
|
|
99
|
+
{/if}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
/** What this part of the work is. */
|
|
3
|
+
kind: 'thinking' | 'code';
|
|
4
|
+
/** Everything written so far. */
|
|
5
|
+
text: string;
|
|
6
|
+
/** The text is still arriving: the title says so, and the newest line stays in view. */
|
|
7
|
+
streaming?: boolean;
|
|
8
|
+
/** Whole seconds the thinking took, once it is over. */
|
|
9
|
+
seconds?: number | null;
|
|
10
|
+
/** Open until the person chooses (the live part), or closed until they do (the part under a conclusion). */
|
|
11
|
+
openByDefault?: boolean;
|
|
12
|
+
class?: string;
|
|
13
|
+
}
|
|
14
|
+
declare const GenerationModule: import("svelte").Component<Props, {}, "">;
|
|
15
|
+
type GenerationModule = ReturnType<typeof GenerationModule>;
|
|
16
|
+
export default GenerationModule;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The face next to a message a person of the team wrote: their picture, or their initials, and their e-mail when the pointer
|
|
4
|
+
* rests on it (or the keyboard is on it). The same tooltip as the member avatars in the project header of the Widgetic site,
|
|
5
|
+
* and the same click: with an e-mail the face is a link that writes to the person (`mailto:`, with the hand cursor), as
|
|
6
|
+
* in that header. Without one it is only a picture: nothing to click, nothing to reach with the keyboard.
|
|
7
|
+
*
|
|
8
|
+
* `Avatar` and `Tooltip` come from the components barrel of the design system, like the panel header's: its export map has
|
|
9
|
+
* no path to them anywhere else. Neither shows a word of its own (initials from a name, an e-mail address in the tooltip),
|
|
10
|
+
* so there are no words of the design system to hand over.
|
|
11
|
+
*/
|
|
12
|
+
import { Avatar, Tooltip } from '@widgetic/design-system/components';
|
|
13
|
+
import { useChatLanguage } from '../../i18n/language.js';
|
|
14
|
+
import type { ChatMember } from '../../types/agent.js';
|
|
15
|
+
import { memberDisplayName, memberMailtoHref, memberTooltipEmail } from '../../utils/agentTurn.js';
|
|
16
|
+
|
|
17
|
+
// The words of the avatar, in the language of the chat above
|
|
18
|
+
const { t } = useChatLanguage();
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
member: ChatMember;
|
|
22
|
+
class?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let { member, class: className = '' }: Props = $props();
|
|
26
|
+
|
|
27
|
+
const displayName = $derived(memberDisplayName(member));
|
|
28
|
+
// Null when the person has no e-mail (or a placeholder word for one): then there is nothing to show on hover, and nowhere to write to
|
|
29
|
+
const tooltipEmail = $derived(memberTooltipEmail(member.email));
|
|
30
|
+
const mailtoHref = $derived(memberMailtoHref(member.email));
|
|
31
|
+
// What a screen reader says: "Name (email)", just the name, just the e-mail, or that it is a person of the team
|
|
32
|
+
const accessibleLabel = $derived.by(() => {
|
|
33
|
+
if (displayName && tooltipEmail) return $t('chat.member.avatarLabel', { name: displayName, email: tooltipEmail });
|
|
34
|
+
return displayName || tooltipEmail || $t('chat.member.unknown');
|
|
35
|
+
});
|
|
36
|
+
// What it says when it is a link, as the header's does: "Email Name" (the e-mail itself when the person has no name)
|
|
37
|
+
const emailLabel = $derived($t('chat.member.emailLabel', { name: displayName || tooltipEmail || '' }));
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<!-- The picture, the same whether the face is a link or not -->
|
|
41
|
+
{#snippet face()}
|
|
42
|
+
<Avatar name={displayName} src={member.avatarUrl ?? undefined} size="sm" />
|
|
43
|
+
{/snippet}
|
|
44
|
+
|
|
45
|
+
<Tooltip.Provider delayDuration={200}>
|
|
46
|
+
<Tooltip.Root disabled={!tooltipEmail}>
|
|
47
|
+
<Tooltip.Trigger>
|
|
48
|
+
{#snippet child({ props }: { props: Record<string, unknown> })}
|
|
49
|
+
{#if mailtoHref}
|
|
50
|
+
<!-- A mail address, not a route of an app: SvelteKit's `resolve()` (the hint of the Svelte tools for any `href`) is for routes -->
|
|
51
|
+
<a
|
|
52
|
+
{...props}
|
|
53
|
+
href={mailtoHref}
|
|
54
|
+
class="ring-offset-background focus-visible:ring-ring inline-flex shrink-0 cursor-pointer rounded-full focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none {className}"
|
|
55
|
+
aria-label={emailLabel}
|
|
56
|
+
data-testid="chat-member-avatar"
|
|
57
|
+
>
|
|
58
|
+
{@render face()}
|
|
59
|
+
</a>
|
|
60
|
+
{:else}
|
|
61
|
+
<span
|
|
62
|
+
{...props}
|
|
63
|
+
class="inline-flex shrink-0 rounded-full {className}"
|
|
64
|
+
role="img"
|
|
65
|
+
aria-label={accessibleLabel}
|
|
66
|
+
data-testid="chat-member-avatar"
|
|
67
|
+
>
|
|
68
|
+
{@render face()}
|
|
69
|
+
</span>
|
|
70
|
+
{/if}
|
|
71
|
+
{/snippet}
|
|
72
|
+
</Tooltip.Trigger>
|
|
73
|
+
{#if tooltipEmail}
|
|
74
|
+
<Tooltip.Portal>
|
|
75
|
+
<Tooltip.Content
|
|
76
|
+
side="bottom"
|
|
77
|
+
align="center"
|
|
78
|
+
collisionPadding={8}
|
|
79
|
+
class="max-w-[min(20rem,calc(100vw-2rem))] break-all text-center"
|
|
80
|
+
data-testid="chat-member-email-tooltip"
|
|
81
|
+
>
|
|
82
|
+
{tooltipEmail}
|
|
83
|
+
</Tooltip.Content>
|
|
84
|
+
</Tooltip.Portal>
|
|
85
|
+
{/if}
|
|
86
|
+
</Tooltip.Root>
|
|
87
|
+
</Tooltip.Provider>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChatMember } from '../../types/agent.js';
|
|
2
|
+
interface Props {
|
|
3
|
+
member: ChatMember;
|
|
4
|
+
class?: string;
|
|
5
|
+
}
|
|
6
|
+
declare const MemberAvatar: import("svelte").Component<Props, {}, "">;
|
|
7
|
+
type MemberAvatar = ReturnType<typeof MemberAvatar>;
|
|
8
|
+
export default MemberAvatar;
|
|
@@ -9,4 +9,9 @@ export { default as AttachmentDisplay } from './AttachmentDisplay.svelte';
|
|
|
9
9
|
export { default as CodeBlock } from './CodeBlock.svelte';
|
|
10
10
|
export { default as StatusIndicator } from './StatusIndicator.svelte';
|
|
11
11
|
export { default as LoadingIndicator } from './LoadingIndicator.svelte';
|
|
12
|
+
export { default as AgentAvatar } from './agent/AgentAvatar.svelte';
|
|
13
|
+
export { default as AgentTurn } from './agent/AgentTurn.svelte';
|
|
14
|
+
export { default as CodeCard } from './agent/CodeCard.svelte';
|
|
15
|
+
export { default as GenerationModule } from './agent/GenerationModule.svelte';
|
|
16
|
+
export { default as MemberAvatar } from './agent/MemberAvatar.svelte';
|
|
12
17
|
export type { ComponentProps } from 'svelte';
|
package/dist/components/index.js
CHANGED
|
@@ -10,3 +10,12 @@ export { default as AttachmentDisplay } from './AttachmentDisplay.svelte';
|
|
|
10
10
|
export { default as CodeBlock } from './CodeBlock.svelte';
|
|
11
11
|
export { default as StatusIndicator } from './StatusIndicator.svelte';
|
|
12
12
|
export { default as LoadingIndicator } from './LoadingIndicator.svelte';
|
|
13
|
+
// The assistant's turn, for a page that builds its own panel: the moving avatar, the turn around it (the avatar with its
|
|
14
|
+
// content beside it), the collapsible thinking and the code card (the code, laid out by Prettier as it is written), and a
|
|
15
|
+
// team member's avatar with the e-mail on hover.
|
|
16
|
+
// Inside `Chat` they speak the page's language; used on their own they speak English.
|
|
17
|
+
export { default as AgentAvatar } from './agent/AgentAvatar.svelte';
|
|
18
|
+
export { default as AgentTurn } from './agent/AgentTurn.svelte';
|
|
19
|
+
export { default as CodeCard } from './agent/CodeCard.svelte';
|
|
20
|
+
export { default as GenerationModule } from './agent/GenerationModule.svelte';
|
|
21
|
+
export { default as MemberAvatar } from './agent/MemberAvatar.svelte';
|
package/dist/i18n/messages.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ export declare const CHAT_MESSAGES: {
|
|
|
32
32
|
/** The name of a conversation that has none. */
|
|
33
33
|
readonly 'chat.header.untitled': "Untitled";
|
|
34
34
|
readonly 'chat.header.newConversationTitle': "New chat";
|
|
35
|
+
readonly 'chat.header.noPastChats': "No previous chats yet";
|
|
35
36
|
readonly 'chat.header.newConversation': "Start new conversation";
|
|
36
37
|
/** Label for the debug console icon in the panel chat header. */
|
|
37
38
|
readonly 'chat.header.console': "Console";
|
|
@@ -43,20 +44,43 @@ export declare const CHAT_MESSAGES: {
|
|
|
43
44
|
readonly 'chat.messages.emptyTitle': "What do you want to build?";
|
|
44
45
|
readonly 'chat.messages.emptyHint': "Sketch it on the canvas, or describe it here.";
|
|
45
46
|
readonly 'chat.messages.loadingHistory': "Loading chat history...";
|
|
46
|
-
/** Said while the assistant works on an answer. */
|
|
47
|
-
readonly 'chat.messages.thinking': "
|
|
47
|
+
/** Said while the assistant works on an answer (typed out, in a shimmer, by the avatar). */
|
|
48
|
+
readonly 'chat.messages.thinking': "Thinking…";
|
|
48
49
|
/** Header while streamed reasoning is visible during codegen. */
|
|
49
|
-
readonly 'chat.generation.thinking': "Thinking";
|
|
50
|
+
readonly 'chat.generation.thinking': "Thinking…";
|
|
50
51
|
/** Header while streamed code is visible during codegen. */
|
|
51
|
-
readonly 'chat.generation.writingCode': "Writing code";
|
|
52
|
+
readonly 'chat.generation.writingCode': "Writing code…";
|
|
52
53
|
/** Collapsed reasoning summary after code begins streaming. */
|
|
53
54
|
readonly 'chat.generation.thoughtFor': "Thought for {seconds} s";
|
|
54
55
|
readonly 'chat.generation.expandThought': "Show thinking";
|
|
55
56
|
readonly 'chat.generation.collapseThought': "Hide thinking";
|
|
56
57
|
/** Polite screen-reader line when codegen enters the thinking phase. */
|
|
57
|
-
readonly 'chat.generation.statusThinking': "
|
|
58
|
+
readonly 'chat.generation.statusThinking': "Thinking";
|
|
58
59
|
/** Polite screen-reader line when codegen enters the writing-code phase. */
|
|
59
|
-
readonly 'chat.generation.statusWritingCode': "
|
|
60
|
+
readonly 'chat.generation.statusWritingCode': "Writing code";
|
|
61
|
+
/** Title of the code the assistant wrote, once it has finished writing it (while it writes, the title is `chat.generation.writingCode`). */
|
|
62
|
+
readonly 'chat.generation.codeTitle': "Code";
|
|
63
|
+
/** The status line of the code card once the assistant has finished writing the code (while it writes, the line is `chat.generation.writingCode`). */
|
|
64
|
+
readonly 'chat.generation.codeWritten': "Code written";
|
|
65
|
+
/** The size of the code in the code card: its lines as they are laid out. */
|
|
66
|
+
readonly 'chat.generation.codeLineCount': "{count, plural, one {# line} other {# lines}}";
|
|
67
|
+
readonly 'chat.generation.expandCode': "Show code";
|
|
68
|
+
readonly 'chat.generation.collapseCode': "Hide code";
|
|
69
|
+
/**
|
|
70
|
+
* The conclusion line under the success avatar, when the assistant finished building and said nothing else the chat shows
|
|
71
|
+
* (the "Code generation completed" lines the host saves are not shown: they become the version label of the turn).
|
|
72
|
+
*/
|
|
73
|
+
readonly 'chat.generation.doneMessage': "Done. Your changes are ready.";
|
|
74
|
+
/** What a screen reader says about the avatar once what the assistant was asked to do is done. */
|
|
75
|
+
readonly 'chat.agent.avatar.success': "Done";
|
|
76
|
+
/** What a screen reader says about the avatar when the assistant's work failed. */
|
|
77
|
+
readonly 'chat.agent.avatar.error': "Something went wrong";
|
|
78
|
+
/** What a screen reader says about the avatar of a person of the team (`{name}` and `{email}`). */
|
|
79
|
+
readonly 'chat.member.avatarLabel': "{name} ({email})";
|
|
80
|
+
/** What a screen reader says about the avatar of a person of the team when it is a link that writes to them (`{name}`). */
|
|
81
|
+
readonly 'chat.member.emailLabel': "Email {name}";
|
|
82
|
+
/** The name of a person of the team the page did not tell the chat about. */
|
|
83
|
+
readonly 'chat.member.unknown': "Team member";
|
|
60
84
|
/** Heading when the gateway plan has no title. */
|
|
61
85
|
readonly 'chat.plan.title': "Build plan";
|
|
62
86
|
/** Progress badge on the plan card (`{current}` / `{total}`). */
|
|
@@ -68,9 +92,9 @@ export declare const CHAT_MESSAGES: {
|
|
|
68
92
|
readonly 'chat.messages.attachmentMissingTitle': "Attachment not available";
|
|
69
93
|
readonly 'chat.messages.attachmentMissingLabel': "Missing file attachment";
|
|
70
94
|
readonly 'chat.messages.attachmentMissingCaption': "Attachment not saved. Re-attach with Upload, then send it again.";
|
|
71
|
-
/**
|
|
95
|
+
/** A failed reply in the docked panel is one short line (this, or what the server said) with a refresh icon that runs the prompt again. */
|
|
72
96
|
readonly 'chat.messages.failedTitle': "Could not build the widget";
|
|
73
|
-
|
|
97
|
+
/** The name of the refresh icon next to a failure: its tooltip and what a screen reader says. */
|
|
74
98
|
readonly 'chat.messages.failedRetry': "Try again";
|
|
75
99
|
/** The line a build the person stopped leaves in the conversation (docked panel): it is their own command, not a failure. */
|
|
76
100
|
readonly 'chat.messages.stoppedByYou': "You stopped the build.";
|
|
@@ -144,7 +168,6 @@ export declare const CHAT_MESSAGES: {
|
|
|
144
168
|
readonly 'chat.input.send': "Send message";
|
|
145
169
|
readonly 'chat.input.tooLong': "Message is too long";
|
|
146
170
|
readonly 'chat.input.publishNudge': "First generate your widget, then you can publish it.";
|
|
147
|
-
readonly 'chat.composer.assistant': "Assistant";
|
|
148
171
|
readonly 'chat.messages.scrollToBottom': "Scroll to latest messages";
|
|
149
172
|
readonly 'chat.messages.clarificationPrompt': "Which option fits best?";
|
|
150
173
|
readonly 'chat.checkpoint.version': "Version {n}";
|
|
@@ -211,7 +234,6 @@ export declare const CHAT_MESSAGES: {
|
|
|
211
234
|
readonly 'chat.error.loadConversations': "Failed to load conversations.";
|
|
212
235
|
readonly 'chat.error.loadConversation': "Failed to load conversation.";
|
|
213
236
|
readonly 'chat.error.sendMessage': "Failed to send message. Please try again.";
|
|
214
|
-
readonly 'chat.error.sendSuggestion': "Failed to send the selected suggestion. Please try again.";
|
|
215
237
|
readonly 'chat.error.screenshotUnavailable': "Screenshot capture failed — no preview available.";
|
|
216
238
|
readonly 'chat.error.screenshotFailed': "Failed to capture screenshot";
|
|
217
239
|
readonly 'chat.error.screenshotActionFailed': "Screenshot action failed. Please try again.";
|