@widgetic/chat 0.1.42 → 0.1.43
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 +461 -6
- package/dist/components/Chat.svelte +1078 -87
- package/dist/components/Chat.svelte.d.ts +102 -2
- package/dist/components/ChatComposer.svelte +382 -9
- package/dist/components/ChatComposer.svelte.d.ts +66 -3
- package/dist/components/ChatMessageQueue.svelte +56 -0
- package/dist/components/ChatMessageQueue.svelte.d.ts +20 -0
- package/dist/components/ChatMessages.svelte +477 -46
- package/dist/components/ChatMessages.svelte.d.ts +45 -2
- package/dist/components/ComposerAttachmentsTray.svelte +42 -10
- package/dist/components/ComposerAttachmentsTray.svelte.d.ts +5 -0
- package/dist/components/ComposerMentionChips.svelte +53 -0
- package/dist/components/ComposerMentionChips.svelte.d.ts +15 -0
- package/dist/components/FollowUpCard.svelte +134 -0
- package/dist/components/FollowUpCard.svelte.d.ts +17 -0
- package/dist/components/MentionChip.svelte +41 -0
- package/dist/components/MentionChip.svelte.d.ts +12 -0
- package/dist/components/MentionMenu.svelte +114 -0
- package/dist/components/MentionMenu.svelte.d.ts +19 -0
- package/dist/components/agent/AgentAvatar.svelte +6 -4
- package/dist/components/agent/AgentAvatar.svelte.d.ts +1 -1
- package/dist/components/agent/CodeCard.svelte +122 -27
- package/dist/components/agent/CodeCard.svelte.d.ts +3 -0
- package/dist/components/agent/CodeDiff.svelte +180 -0
- package/dist/components/agent/CodeDiff.svelte.d.ts +10 -0
- package/dist/components/agent/GenerationModule.svelte +13 -2
- package/dist/components/agent/GenerationModule.svelte.d.ts +3 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +9 -0
- package/dist/errors/logTechnicalUserError.d.ts +1 -0
- package/dist/errors/logTechnicalUserError.js +37 -0
- package/dist/i18n/messages.d.ts +106 -1
- package/dist/i18n/messages.js +112 -1
- package/dist/i18n/sketchLocales.js +8 -8
- package/dist/messageQueue.d.ts +83 -0
- package/dist/messageQueue.js +84 -0
- package/dist/services/sketchAssistantClient.js +6 -2
- package/dist/services/sketchAssistantErrors.d.ts +4 -0
- package/dist/services/sketchAssistantErrors.js +22 -0
- package/dist/services/sketchAssistantOrchestrator.d.ts +1 -6
- package/dist/services/sketchAssistantOrchestrator.js +10 -24
- package/dist/services/sketchAssistantReply.js +3 -1
- package/dist/stores/chatStore.d.ts +5 -0
- package/dist/stores/chatStore.js +21 -0
- package/dist/types/agent.d.ts +4 -3
- package/dist/types/attachment.d.ts +16 -0
- package/dist/types/config.d.ts +6 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/mention.d.ts +40 -0
- package/dist/types/mention.js +8 -0
- package/dist/types/message.d.ts +60 -1
- package/dist/utils/agentTurn.d.ts +40 -9
- package/dist/utils/agentTurn.js +43 -11
- package/dist/utils/attachmentProgress.d.ts +12 -0
- package/dist/utils/attachmentProgress.js +18 -0
- package/dist/utils/bubbleRichFit.d.ts +68 -0
- package/dist/utils/bubbleRichFit.js +45 -0
- package/dist/utils/bubbleShrinkWrap.d.ts +8 -1
- package/dist/utils/bubbleShrinkWrap.js +113 -17
- package/dist/utils/codeChanges.d.ts +103 -0
- package/dist/utils/codeChanges.js +226 -0
- package/dist/utils/codeHighlight.js +1 -0
- package/dist/utils/codeStreamFormat.d.ts +5 -2
- package/dist/utils/composerMentions.d.ts +96 -0
- package/dist/utils/composerMentions.js +217 -0
- package/dist/utils/followUps.d.ts +74 -0
- package/dist/utils/followUps.js +116 -0
- package/dist/utils/generationArchiveStore.d.ts +6 -0
- package/dist/utils/generationArchiveStore.js +11 -3
- package/dist/utils/index.d.ts +12 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/orderedLine.d.ts +29 -0
- package/dist/utils/orderedLine.js +17 -0
- package/dist/utils/pastedContent.d.ts +99 -0
- package/dist/utils/pastedContent.js +258 -0
- package/dist/utils/questionCards.d.ts +131 -0
- package/dist/utils/questionCards.js +287 -0
- package/dist/utils/referenceItems.d.ts +36 -0
- package/dist/utils/referenceItems.js +56 -0
- package/dist/utils/sentReferenceBadges.d.ts +26 -0
- package/dist/utils/sentReferenceBadges.js +32 -0
- package/package.json +1 -1
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/** The most questions a card draws, whatever the server sent (the gateway sends at most four). */
|
|
2
|
+
export const QUESTION_CARD_MAX_QUESTIONS = 8;
|
|
3
|
+
/** The most options one question draws (the gateway sends at most five). */
|
|
4
|
+
export const QUESTION_CARD_MAX_OPTIONS = 8;
|
|
5
|
+
/** The most characters of a question, of an option, and of the sentence that introduces the card. */
|
|
6
|
+
export const QUESTION_CARD_MAX_QUESTION_CHARS = 300;
|
|
7
|
+
export const QUESTION_CARD_MAX_OPTION_CHARS = 300;
|
|
8
|
+
export const QUESTION_CARD_MAX_MESSAGE_CHARS = 600;
|
|
9
|
+
/** Between the picks of one question, in the one `answer` text that goes to the server. */
|
|
10
|
+
export const QUESTION_ANSWER_LINE_SEPARATOR = '\n';
|
|
11
|
+
/** A value as one line of at most `maxChars` characters, or '' when it is not text. */
|
|
12
|
+
function oneLineOf(value, maxChars) {
|
|
13
|
+
if (typeof value !== 'string')
|
|
14
|
+
return '';
|
|
15
|
+
const line = value.replace(/\s+/g, ' ').trim();
|
|
16
|
+
return line.length > maxChars ? line.slice(0, maxChars).trimEnd() : line;
|
|
17
|
+
}
|
|
18
|
+
/** Regular expression characters written as they are. */
|
|
19
|
+
function escapeForPattern(text) {
|
|
20
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
21
|
+
}
|
|
22
|
+
function isPlainObject(value) {
|
|
23
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
/** The options of one question as the card draws them: each with words and an id of its own, at most `QUESTION_CARD_MAX_OPTIONS`. */
|
|
26
|
+
function cleanOptions(rawOptions, questionId) {
|
|
27
|
+
if (!Array.isArray(rawOptions))
|
|
28
|
+
return [];
|
|
29
|
+
const options = [];
|
|
30
|
+
const seenIds = new Set();
|
|
31
|
+
for (const rawOption of rawOptions) {
|
|
32
|
+
if (options.length >= QUESTION_CARD_MAX_OPTIONS)
|
|
33
|
+
break;
|
|
34
|
+
// A plain string is an option too (a host that keeps its card in a lighter shape)
|
|
35
|
+
const label = oneLineOf(isPlainObject(rawOption) ? rawOption.label : rawOption, QUESTION_CARD_MAX_OPTION_CHARS);
|
|
36
|
+
if (label === '')
|
|
37
|
+
continue;
|
|
38
|
+
const givenId = isPlainObject(rawOption) ? oneLineOf(rawOption.id, 80) : '';
|
|
39
|
+
const id = givenId !== '' && !seenIds.has(givenId) ? givenId : `${questionId}o${options.length + 1}`;
|
|
40
|
+
seenIds.add(id);
|
|
41
|
+
options.push({
|
|
42
|
+
id,
|
|
43
|
+
label,
|
|
44
|
+
recommended: isPlainObject(rawOption) && rawOption.recommended === true
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return options;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The card as the chat draws it, made from what the gateway sent (`metadata.clarification` of an operation that ended in
|
|
51
|
+
* `needs_answers`, or `generation_surface.clarification` of the saved message). Nothing is trusted: a question with no words is
|
|
52
|
+
* left out, so is an option with none, "several picks" means something only when there are at least two options, and a card with
|
|
53
|
+
* no question left is no card (null). The picks of an earlier answer (`result`) are not read from the server's card.
|
|
54
|
+
*/
|
|
55
|
+
export function questionCardFromClarification(raw) {
|
|
56
|
+
if (!isPlainObject(raw) || !Array.isArray(raw.questions))
|
|
57
|
+
return null;
|
|
58
|
+
const questions = [];
|
|
59
|
+
const seenIds = new Set();
|
|
60
|
+
for (const rawQuestion of raw.questions) {
|
|
61
|
+
if (questions.length >= QUESTION_CARD_MAX_QUESTIONS)
|
|
62
|
+
break;
|
|
63
|
+
if (!isPlainObject(rawQuestion))
|
|
64
|
+
continue;
|
|
65
|
+
const question = oneLineOf(rawQuestion.question, QUESTION_CARD_MAX_QUESTION_CHARS);
|
|
66
|
+
if (question === '')
|
|
67
|
+
continue;
|
|
68
|
+
const givenId = oneLineOf(rawQuestion.id, 80);
|
|
69
|
+
const id = givenId !== '' && !seenIds.has(givenId) ? givenId : `q${questions.length + 1}`;
|
|
70
|
+
seenIds.add(id);
|
|
71
|
+
const options = cleanOptions(rawQuestion.options, id);
|
|
72
|
+
const recommendedWord = oneLineOf(rawQuestion.recommendedWord, 40);
|
|
73
|
+
questions.push({
|
|
74
|
+
id,
|
|
75
|
+
question,
|
|
76
|
+
options,
|
|
77
|
+
allowMultiple: rawQuestion.allowMultiple === true && options.length >= 2,
|
|
78
|
+
allowOther: rawQuestion.allowOther === true || options.length === 0,
|
|
79
|
+
...(recommendedWord !== '' ? { recommendedWord } : {})
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (questions.length === 0)
|
|
83
|
+
return null;
|
|
84
|
+
return { message: oneLineOf(raw.message, QUESTION_CARD_MAX_MESSAGE_CHARS), questions };
|
|
85
|
+
}
|
|
86
|
+
/** The card of a message, or null when the message carries none (or one that has no usable question). */
|
|
87
|
+
export function questionCardOf(message) {
|
|
88
|
+
if (!message || message.messageType !== 'assistant' || !message.questionCard)
|
|
89
|
+
return null;
|
|
90
|
+
const card = message.questionCard;
|
|
91
|
+
return Array.isArray(card.questions) && card.questions.length > 0 ? card : null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The words a card is saved with in the conversation: the sentence that introduces it, then each question numbered with its options
|
|
95
|
+
* under it. The same lines the gateway saves, so a card the chat adds itself reads the same after a reload. It is what is drawn
|
|
96
|
+
* when the card is closed and its picks are not known.
|
|
97
|
+
*/
|
|
98
|
+
export function questionCardPlainText(card) {
|
|
99
|
+
const lines = [card.message ?? ''];
|
|
100
|
+
card.questions.forEach((question, questionIndex) => {
|
|
101
|
+
lines.push('', `${questionIndex + 1}. ${question.question}`);
|
|
102
|
+
for (const option of question.options)
|
|
103
|
+
lines.push(` - ${option.label}`);
|
|
104
|
+
});
|
|
105
|
+
return lines.join('\n').trim();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* A recommended option says so in its own label ("Minimal (Recommended)", in the language of the person). That word is for choosing:
|
|
109
|
+
* once it is chosen it is only noise, so the summary shows the label without it.
|
|
110
|
+
*/
|
|
111
|
+
export function withoutRecommendedWord(label, recommendedWord) {
|
|
112
|
+
const word = (recommendedWord ?? '').trim();
|
|
113
|
+
if (word === '')
|
|
114
|
+
return label;
|
|
115
|
+
return label.replace(new RegExp(`\\s*\\(\\s*${escapeForPattern(word)}\\s*\\)\\s*$`, 'i'), '').trim();
|
|
116
|
+
}
|
|
117
|
+
/** The picks of one answer, one for each line of it. An answer with nothing in it (a skipped question) has none. */
|
|
118
|
+
export function picksOfAnswer(answer) {
|
|
119
|
+
if (typeof answer !== 'string')
|
|
120
|
+
return [];
|
|
121
|
+
return answer
|
|
122
|
+
.split(/\r?\n/)
|
|
123
|
+
.map((line) => line.replace(/\s+/g, ' ').trim())
|
|
124
|
+
.filter((line) => line !== '');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The answers of the card for the picks made in it: one for each question, in the order of the card. `pickedOptionIds` is what the
|
|
128
|
+
* design system's card hands over, the ids of the options picked for each question (an empty list for a question that was skipped).
|
|
129
|
+
* The picks of a question are written in the order of its options, one for each line; a question that takes one answer keeps only
|
|
130
|
+
* the first of them; an id that is not an option of the question is left out.
|
|
131
|
+
*/
|
|
132
|
+
export function answersFromPicks(card, pickedOptionIds) {
|
|
133
|
+
return card.questions.map((question, questionIndex) => {
|
|
134
|
+
const picked = new Set(pickedOptionIds[questionIndex] ?? []);
|
|
135
|
+
const labels = question.options.filter((option) => picked.has(option.id)).map((option) => option.label);
|
|
136
|
+
const kept = question.allowMultiple ? labels : labels.slice(0, 1);
|
|
137
|
+
return { question: question.question, answer: kept.join(QUESTION_ANSWER_LINE_SEPARATOR) };
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** The answers of a card whose every question was left to the assistant: the same questions, each with an empty answer. */
|
|
141
|
+
export function skippedAnswersOf(card) {
|
|
142
|
+
return card.questions.map((question) => ({ question: question.question, answer: '' }));
|
|
143
|
+
}
|
|
144
|
+
/** Whether an answer is a question that was skipped. */
|
|
145
|
+
export function isSkippedAnswer(answer) {
|
|
146
|
+
return picksOfAnswer(answer?.answer).length === 0;
|
|
147
|
+
}
|
|
148
|
+
/** Whether every answer is a skipped question (a list with no answer is not "skipped": nothing was asked of the person). */
|
|
149
|
+
export function areAllAnswersSkipped(answers) {
|
|
150
|
+
return answers.length > 0 && answers.every(isSkippedAnswer);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The rows of the summary of a card: each question with the words of what was picked. An answer belongs to the question at its
|
|
154
|
+
* place, or, when the lists do not line up, to the question with the same words; a question with no answer is skipped.
|
|
155
|
+
*/
|
|
156
|
+
export function summaryRowsOf(card, answers) {
|
|
157
|
+
return card.questions.map((question, questionIndex) => {
|
|
158
|
+
const atPlace = answers[questionIndex];
|
|
159
|
+
const answer = atPlace && atPlace.question === question.question ? atPlace : answers.find((entry) => entry.question === question.question);
|
|
160
|
+
return {
|
|
161
|
+
question: question.question,
|
|
162
|
+
answers: picksOfAnswer(answer?.answer).map((pick) => withoutRecommendedWord(pick, question.recommendedWord))
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/** The rows of a card whose every question was skipped. */
|
|
167
|
+
export function skippedRowsOf(card) {
|
|
168
|
+
return card.questions.map((question) => ({ question: question.question, answers: [] }));
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* What the card of the message at `index` is. Null when that message carries no card.
|
|
172
|
+
*
|
|
173
|
+
* What the person did in the card wins (`questionCard.result`, kept on the message). Without it, the conversation says: no message
|
|
174
|
+
* after the card, and it is open; a message of the person right after it, and the person wrote in the chat instead (skipped); a
|
|
175
|
+
* message of the assistant after it, and the card was answered at some point whose picks are not known (closed).
|
|
176
|
+
*/
|
|
177
|
+
export function questionCardStateOf(messages, index) {
|
|
178
|
+
const card = questionCardOf(messages[index]);
|
|
179
|
+
if (!card)
|
|
180
|
+
return null;
|
|
181
|
+
const answers = card.result?.answers;
|
|
182
|
+
if (Array.isArray(answers) && answers.length > 0) {
|
|
183
|
+
const rows = summaryRowsOf(card, answers);
|
|
184
|
+
return rows.every((row) => row.answers.length === 0) ? { kind: 'skipped', rows } : { kind: 'answered', rows };
|
|
185
|
+
}
|
|
186
|
+
const nextMessage = messages[index + 1];
|
|
187
|
+
if (!nextMessage)
|
|
188
|
+
return { kind: 'open' };
|
|
189
|
+
if (nextMessage.messageType === 'user')
|
|
190
|
+
return { kind: 'skipped', rows: skippedRowsOf(card) };
|
|
191
|
+
return { kind: 'closed' };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The card that is waiting for an answer, with the id of the message that holds it: the newest message of the conversation, when
|
|
195
|
+
* it carries a card nobody has done anything with. Null otherwise. Ask it BEFORE the message of the person is added to the
|
|
196
|
+
* conversation: once it is added the card is no longer the newest.
|
|
197
|
+
*/
|
|
198
|
+
export function openQuestionCardOf(messages) {
|
|
199
|
+
const lastIndex = messages.length - 1;
|
|
200
|
+
if (lastIndex < 0)
|
|
201
|
+
return null;
|
|
202
|
+
const state = questionCardStateOf(messages, lastIndex);
|
|
203
|
+
const card = questionCardOf(messages[lastIndex]);
|
|
204
|
+
return state?.kind === 'open' && card ? { messageId: messages[lastIndex].id, card } : null;
|
|
205
|
+
}
|
|
206
|
+
/** True when two cards ask the same questions, in the same order (the words of the questions are what a card is known by). */
|
|
207
|
+
function askSameQuestions(first, second) {
|
|
208
|
+
if (first.questions.length !== second.questions.length)
|
|
209
|
+
return false;
|
|
210
|
+
return first.questions.every((question, questionIndex) => question.question.trim() === second.questions[questionIndex].question.trim());
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* A reload replaces the thread with the rows the server saved, and the server saves the card but not what the person picked on it:
|
|
214
|
+
* the picks are only in this session. This carries them over, so a card that was answered does not turn into plain words when the
|
|
215
|
+
* conversation is read back (the creator reads it back a moment after every build). For each loaded reply:
|
|
216
|
+
* - with a card, and a local reply that asks the same questions and has picks (`result`): the loaded card gets those picks;
|
|
217
|
+
* - with no card, and a local reply that has one and the same words: the local card stays (the host put it in the chat and the
|
|
218
|
+
* row was saved without it).
|
|
219
|
+
* A local reply is used for one loaded reply only, and the newest ones are paired first, because both lists end at the newest message.
|
|
220
|
+
* Nothing else is touched; with no card in the local messages, the loaded list itself is returned.
|
|
221
|
+
*/
|
|
222
|
+
export function keepLocalQuestionCards(loaded, local) {
|
|
223
|
+
const localCardIndexes = [];
|
|
224
|
+
local.forEach((message, index) => {
|
|
225
|
+
if (questionCardOf(message))
|
|
226
|
+
localCardIndexes.push(index);
|
|
227
|
+
});
|
|
228
|
+
if (localCardIndexes.length === 0)
|
|
229
|
+
return loaded;
|
|
230
|
+
const kept = loaded.slice();
|
|
231
|
+
const usedLocalIndexes = new Set();
|
|
232
|
+
for (let loadedIndex = loaded.length - 1; loadedIndex >= 0; loadedIndex -= 1) {
|
|
233
|
+
const row = loaded[loadedIndex];
|
|
234
|
+
if (row?.messageType !== 'assistant')
|
|
235
|
+
continue;
|
|
236
|
+
const rowCard = questionCardOf(row);
|
|
237
|
+
const rowWords = (row.messageContent ?? '').trim();
|
|
238
|
+
let twinIndex = -1;
|
|
239
|
+
for (let position = localCardIndexes.length - 1; position >= 0; position -= 1) {
|
|
240
|
+
const candidateIndex = localCardIndexes[position];
|
|
241
|
+
if (usedLocalIndexes.has(candidateIndex))
|
|
242
|
+
continue;
|
|
243
|
+
const candidateCard = questionCardOf(local[candidateIndex]);
|
|
244
|
+
const isTwin = rowCard
|
|
245
|
+
? askSameQuestions(candidateCard, rowCard)
|
|
246
|
+
: rowWords !== '' && (local[candidateIndex].messageContent ?? '').trim() === rowWords;
|
|
247
|
+
if (isTwin) {
|
|
248
|
+
twinIndex = candidateIndex;
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (twinIndex < 0)
|
|
253
|
+
continue;
|
|
254
|
+
usedLocalIndexes.add(twinIndex);
|
|
255
|
+
const twinCard = questionCardOf(local[twinIndex]);
|
|
256
|
+
if (!rowCard) {
|
|
257
|
+
kept[loadedIndex] = { ...row, questionCard: twinCard };
|
|
258
|
+
}
|
|
259
|
+
else if (twinCard.result && !rowCard.result) {
|
|
260
|
+
kept[loadedIndex] = { ...row, questionCard: { ...rowCard, result: twinCard.result } };
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return kept;
|
|
264
|
+
}
|
|
265
|
+
/** A text without its case and spacing, to compare a message with the words of a chip. */
|
|
266
|
+
function comparableText(text) {
|
|
267
|
+
return text.normalize('NFC').replace(/\s+/g, ' ').trim().toLowerCase();
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* What became of an older question card, the one drawn from the chips of a reply (`optionLabels` are their words). While no message
|
|
271
|
+
* of the person comes after the reply it is open. Once one does, the person went on in the chat: if that message holds the words of a
|
|
272
|
+
* chip, that chip is what was answered (picking a chip puts its words in the message box), and if not, the question was skipped.
|
|
273
|
+
*/
|
|
274
|
+
export function chipQuestionOutcomeOf(messages, replyIndex, optionLabels) {
|
|
275
|
+
for (let index = replyIndex + 1; index < messages.length; index += 1) {
|
|
276
|
+
const message = messages[index];
|
|
277
|
+
if (message.messageType !== 'user')
|
|
278
|
+
continue;
|
|
279
|
+
const said = comparableText(message.messageContent ?? '');
|
|
280
|
+
const answered = optionLabels.find((label) => {
|
|
281
|
+
const wanted = comparableText(label);
|
|
282
|
+
return wanted !== '' && said.includes(wanted);
|
|
283
|
+
});
|
|
284
|
+
return answered !== undefined ? { kind: 'answered', answer: answered } : { kind: 'skipped' };
|
|
285
|
+
}
|
|
286
|
+
return { kind: 'open' };
|
|
287
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ReferenceItem } from "@widgetic/design-system/components/chat/index";
|
|
2
|
+
import type { AttachmentInput } from "../types/attachment.js";
|
|
3
|
+
/**
|
|
4
|
+
* The files of the composer tray as the message box offers them to a `#` (the design system's `references` prop). Kept apart from
|
|
5
|
+
* the components so Node can test it. The label a file is called by is given in `Chat.svelte` when the file joins the tray; the rules
|
|
6
|
+
* for it (`assignReferenceLabel`) live in the design system.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Which attached files a message may refer to when the page does not say: the pictures. A model is told the name of a picture and what
|
|
10
|
+
* is on it; a video or a PDF is not read that way, so it is not offered.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isReferenceablePicture(attachment: Pick<AttachmentInput, "type" | "file">): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* What the host knows a file by in the `references` of a send: the id of its upload when the upload has ended, and the label until
|
|
15
|
+
* then (a file that is still on its way is still a file the person called by name). The host ties a `#word` to a file by the
|
|
16
|
+
* `referenceLabel` that goes out with the file, whichever of the two this is.
|
|
17
|
+
*/
|
|
18
|
+
export declare function referenceIdOf(attachment: Pick<AttachmentInput, "uploadId" | "referenceLabel">): string;
|
|
19
|
+
/** What the items need from the page that draws the tray: the picture of a file and its size, already written for people. */
|
|
20
|
+
export interface ReferenceItemSources {
|
|
21
|
+
/** Whether the message may refer to this file. */
|
|
22
|
+
isReferenceable: (attachment: AttachmentInput) => boolean;
|
|
23
|
+
/** An address a picture of the file can be drawn from, or '' when it has none (yet). */
|
|
24
|
+
previewUrlOf: (attachment: AttachmentInput) => string;
|
|
25
|
+
/** The size of the file as people read it ("1.2 MB"), or '' when it is not known. */
|
|
26
|
+
sizeLabelOf: (attachment: AttachmentInput) => string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The list the `#` menu offers, in tray order. A file that cannot be referenced, or has no label yet, is not in it; the number of a
|
|
30
|
+
* file is its place among the files that are, counted from 1, so the menu counts 1, 2, 3 whatever else the tray holds.
|
|
31
|
+
*/
|
|
32
|
+
export declare function buildReferenceItems(attachments: readonly AttachmentInput[], sources: ReferenceItemSources): ReferenceItem[];
|
|
33
|
+
/** Whether a file of the tray is the one a badge or a menu row is on (labels are compared without regard to case). */
|
|
34
|
+
export declare function isHighlightedAttachment(attachment: Pick<AttachmentInput, "referenceLabel">, highlightedLabel: string | null | undefined): boolean;
|
|
35
|
+
/** The labels the files of the tray carry: the ones a new file must not be given again. */
|
|
36
|
+
export declare function labelsOfAttachments(attachments: readonly Pick<AttachmentInput, "referenceLabel">[]): string[];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The files of the composer tray as the message box offers them to a `#` (the design system's `references` prop). Kept apart from
|
|
3
|
+
* the components so Node can test it. The label a file is called by is given in `Chat.svelte` when the file joins the tray; the rules
|
|
4
|
+
* for it (`assignReferenceLabel`) live in the design system.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Which attached files a message may refer to when the page does not say: the pictures. A model is told the name of a picture and what
|
|
8
|
+
* is on it; a video or a PDF is not read that way, so it is not offered.
|
|
9
|
+
*/
|
|
10
|
+
export function isReferenceablePicture(attachment) {
|
|
11
|
+
return (attachment.type === "image" ||
|
|
12
|
+
attachment.file?.type?.startsWith("image/") === true);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* What the host knows a file by in the `references` of a send: the id of its upload when the upload has ended, and the label until
|
|
16
|
+
* then (a file that is still on its way is still a file the person called by name). The host ties a `#word` to a file by the
|
|
17
|
+
* `referenceLabel` that goes out with the file, whichever of the two this is.
|
|
18
|
+
*/
|
|
19
|
+
export function referenceIdOf(attachment) {
|
|
20
|
+
return attachment.uploadId || attachment.referenceLabel || "";
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The list the `#` menu offers, in tray order. A file that cannot be referenced, or has no label yet, is not in it; the number of a
|
|
24
|
+
* file is its place among the files that are, counted from 1, so the menu counts 1, 2, 3 whatever else the tray holds.
|
|
25
|
+
*/
|
|
26
|
+
export function buildReferenceItems(attachments, sources) {
|
|
27
|
+
const items = [];
|
|
28
|
+
for (const attachment of attachments) {
|
|
29
|
+
const label = attachment.referenceLabel;
|
|
30
|
+
if (!label || !sources.isReferenceable(attachment))
|
|
31
|
+
continue;
|
|
32
|
+
const previewUrl = sources.previewUrlOf(attachment);
|
|
33
|
+
const sizeLabel = sources.sizeLabelOf(attachment);
|
|
34
|
+
items.push({
|
|
35
|
+
id: referenceIdOf(attachment),
|
|
36
|
+
label,
|
|
37
|
+
number: items.length + 1,
|
|
38
|
+
fileName: attachment.file?.name ?? "",
|
|
39
|
+
previewUrl: previewUrl || null,
|
|
40
|
+
sizeLabel: sizeLabel || undefined,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return items;
|
|
44
|
+
}
|
|
45
|
+
/** Whether a file of the tray is the one a badge or a menu row is on (labels are compared without regard to case). */
|
|
46
|
+
export function isHighlightedAttachment(attachment, highlightedLabel) {
|
|
47
|
+
if (!highlightedLabel || !attachment.referenceLabel)
|
|
48
|
+
return false;
|
|
49
|
+
return (attachment.referenceLabel.toLowerCase() === highlightedLabel.toLowerCase());
|
|
50
|
+
}
|
|
51
|
+
/** The labels the files of the tray carry: the ones a new file must not be given again. */
|
|
52
|
+
export function labelsOfAttachments(attachments) {
|
|
53
|
+
return attachments
|
|
54
|
+
.map((attachment) => attachment.referenceLabel)
|
|
55
|
+
.filter((label) => typeof label === "string" && label !== "");
|
|
56
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The files of a sent message as the badges in its text need them. Kept apart from the components so Node can test it.
|
|
3
|
+
*
|
|
4
|
+
* A person who wrote `#hero_banner` in the message box sees a badge there (a thumbnail, the name, an X). Once the message is sent,
|
|
5
|
+
* the text is only text, and the message holds its files: so the badge is drawn again from the two, without the X. Which word names
|
|
6
|
+
* which file is the design system's rule (`segmentSentText`, which works the label out from the file's name and stays plain where it
|
|
7
|
+
* cannot be sure); what the chat adds is which of the message's files may have a badge (the pictures) and the picture a badge shows.
|
|
8
|
+
*/
|
|
9
|
+
import type { SentAttachment } from '@widgetic/design-system/prompt-references';
|
|
10
|
+
import type { ContentItemOutput } from '../types/message.js';
|
|
11
|
+
/** The side of the picture inside a badge, in CSS px (the design system's `BADGE_THUMBNAIL_SIZE_PX`). */
|
|
12
|
+
export declare const SENT_BADGE_PICTURE_PX = 18;
|
|
13
|
+
/** What decides whether a file is a picture: its type, or, when the type does not say, an address that is one. */
|
|
14
|
+
type PictureFacts = Pick<ContentItemOutput, 'file_type' | 'url'>;
|
|
15
|
+
/** Whether a file of a message is a picture (the only files a badge shows, since a badge shows what is in the file). */
|
|
16
|
+
export declare function isPictureOfMessage(attachment: PictureFacts): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The files of a message as `segmentSentText` takes them, in the order the message holds them. Every file counts for the rule (a
|
|
19
|
+
* name two files share is not trusted, whatever the files are); only a picture that has an address to draw it from may have a badge.
|
|
20
|
+
*/
|
|
21
|
+
export declare function sentAttachmentsOf(attachments: readonly ContentItemOutput[]): SentAttachment[];
|
|
22
|
+
/** The address of the picture a badge shows, at the size a badge draws it, or '' when the file has none. */
|
|
23
|
+
export declare function sentBadgePictureOf(attachment: Pick<ContentItemOutput, 'url'> | undefined): string;
|
|
24
|
+
/** What a badge says to a screen reader beyond its words: the name of the file, or nothing when the file has none. */
|
|
25
|
+
export declare function sentBadgeTitleOf(attachment: Pick<ContentItemOutput, 'file_name'> | undefined): string | undefined;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { buildImageThumbnailUrl } from './imageTransform.js';
|
|
2
|
+
/** The side of the picture inside a badge, in CSS px (the design system's `BADGE_THUMBNAIL_SIZE_PX`). */
|
|
3
|
+
export const SENT_BADGE_PICTURE_PX = 18;
|
|
4
|
+
/** Whether a file of a message is a picture (the only files a badge shows, since a badge shows what is in the file). */
|
|
5
|
+
export function isPictureOfMessage(attachment) {
|
|
6
|
+
if (typeof attachment.file_type === 'string' && attachment.file_type.startsWith('image/'))
|
|
7
|
+
return true;
|
|
8
|
+
const url = attachment.url;
|
|
9
|
+
return typeof url === 'string' && (/^data:image\//i.test(url) || /\.(png|jpe?g|gif|webp|svg)(\?|$)/i.test(url));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The files of a message as `segmentSentText` takes them, in the order the message holds them. Every file counts for the rule (a
|
|
13
|
+
* name two files share is not trusted, whatever the files are); only a picture that has an address to draw it from may have a badge.
|
|
14
|
+
*/
|
|
15
|
+
export function sentAttachmentsOf(attachments) {
|
|
16
|
+
return attachments.map((attachment) => ({
|
|
17
|
+
fileName: attachment.file_name ?? null,
|
|
18
|
+
badge: isPictureOfMessage(attachment) && typeof attachment.url === 'string' && attachment.url !== ''
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
/** The address of the picture a badge shows, at the size a badge draws it, or '' when the file has none. */
|
|
22
|
+
export function sentBadgePictureOf(attachment) {
|
|
23
|
+
const url = attachment?.url;
|
|
24
|
+
if (typeof url !== 'string' || url === '')
|
|
25
|
+
return '';
|
|
26
|
+
return buildImageThumbnailUrl(url, SENT_BADGE_PICTURE_PX);
|
|
27
|
+
}
|
|
28
|
+
/** What a badge says to a screen reader beyond its words: the name of the file, or nothing when the file has none. */
|
|
29
|
+
export function sentBadgeTitleOf(attachment) {
|
|
30
|
+
const name = attachment?.file_name?.trim();
|
|
31
|
+
return name ? name : undefined;
|
|
32
|
+
}
|