@widgetic/chat 0.1.45 → 0.1.48
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 +148 -20
- package/dist/components/AttachmentDisplay.svelte +22 -130
- package/dist/components/AttachmentPreviewDialog.svelte +428 -0
- package/dist/components/AttachmentPreviewDialog.svelte.d.ts +14 -0
- package/dist/components/Chat.svelte +514 -94
- package/dist/components/Chat.svelte.d.ts +19 -2
- package/dist/components/ChatComposer.svelte +126 -72
- package/dist/components/ChatComposer.svelte.d.ts +13 -3
- package/dist/components/ChatDropOverlay.svelte +58 -0
- package/dist/components/ChatDropOverlay.svelte.d.ts +8 -0
- package/dist/components/ChatInput.svelte +42 -1
- package/dist/components/ChatInput.svelte.d.ts +2 -0
- package/dist/components/ChatMessages.svelte +322 -98
- package/dist/components/ChatMessages.svelte.d.ts +11 -2
- package/dist/components/ComposerAttachmentsTray.svelte +144 -56
- package/dist/components/ComposerAttachmentsTray.svelte.d.ts +11 -1
- package/dist/components/FileGlyphIcon.svelte +34 -0
- package/dist/components/FileGlyphIcon.svelte.d.ts +8 -0
- package/dist/components/RestoreCheckpointDialog.svelte +66 -0
- package/dist/components/RestoreCheckpointDialog.svelte.d.ts +11 -0
- package/dist/components/SentAttachments.svelte +118 -0
- package/dist/components/SentAttachments.svelte.d.ts +12 -0
- package/dist/components/agent/AgentTurn.svelte +17 -2
- package/dist/components/agent/CodeCard.svelte +9 -3
- package/dist/i18n/messages.d.ts +45 -3
- package/dist/i18n/messages.js +51 -5
- package/dist/services/index.d.ts +4 -2
- package/dist/services/index.js +2 -1
- package/dist/services/limitRefusal.d.ts +42 -0
- package/dist/services/limitRefusal.js +60 -0
- package/dist/services/sketchAssistantClient.d.ts +11 -3
- package/dist/services/sketchAssistantClient.js +37 -10
- package/dist/services/sketchAssistantErrors.d.ts +16 -1
- package/dist/services/sketchAssistantErrors.js +25 -4
- package/dist/services/sketchAssistantOrchestrator.d.ts +7 -1
- package/dist/services/sketchAssistantOrchestrator.js +2 -2
- package/dist/types/attachment.d.ts +11 -0
- package/dist/utils/agentTurn.d.ts +32 -1
- package/dist/utils/agentTurn.js +46 -3
- package/dist/utils/attachmentDuplicates.d.ts +48 -0
- package/dist/utils/attachmentDuplicates.js +70 -0
- package/dist/utils/attachmentPolicy.d.ts +88 -0
- package/dist/utils/attachmentPolicy.js +135 -0
- package/dist/utils/attachmentPreview.d.ts +146 -0
- package/dist/utils/attachmentPreview.js +224 -0
- package/dist/utils/attachmentTray.d.ts +35 -0
- package/dist/utils/attachmentTray.js +27 -0
- package/dist/utils/chatFileDrag.d.ts +39 -0
- package/dist/utils/chatFileDrag.js +29 -0
- package/dist/utils/composerPreview.d.ts +33 -0
- package/dist/utils/composerPreview.js +31 -0
- package/dist/utils/generationArchiveStore.d.ts +5 -1
- package/dist/utils/generationArchiveStore.js +9 -4
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/limitFailureLine.d.ts +20 -0
- package/dist/utils/limitFailureLine.js +40 -0
- package/dist/utils/panelListPadding.d.ts +20 -0
- package/dist/utils/panelListPadding.js +31 -0
- package/dist/utils/pasteInsertion.d.ts +43 -0
- package/dist/utils/pasteInsertion.js +94 -0
- package/dist/utils/pastedContent.d.ts +13 -0
- package/dist/utils/pastedContent.js +18 -1
- package/dist/utils/referenceItems.js +11 -3
- package/dist/utils/sentAttachments.d.ts +43 -0
- package/dist/utils/sentAttachments.js +38 -0
- package/dist/utils/sentReferenceBadges.d.ts +11 -1
- package/dist/utils/sentReferenceBadges.js +47 -2
- package/package.json +3 -3
|
@@ -59,6 +59,14 @@ export interface TurnReplyFacts {
|
|
|
59
59
|
* the turn (or a generation that ran for it) makes an ordinary last reply a success.
|
|
60
60
|
*/
|
|
61
61
|
export declare function turnOutcome(replies: readonly TurnReplyFacts[], generationRanForTurn: boolean): TurnOutcome | null;
|
|
62
|
+
/**
|
|
63
|
+
* The outcome the avatar is DRAWN for. A success is celebrated (the large avatar plays its one-shot) only by a person who just
|
|
64
|
+
* watched the work succeed. A turn that was judged a success but was not watched happen is drawn as a plain answer, at rest:
|
|
65
|
+
* a conversation that was loaded, work that came back from this browser's archive after a reload (or when the conversation was
|
|
66
|
+
* opened again) is the work of an earlier run, and a refresh must leave the avatar as it was and not make it do the task again.
|
|
67
|
+
* Every other outcome (a failure, a stop, a question) is how the turn really ended and is drawn as it is.
|
|
68
|
+
*/
|
|
69
|
+
export declare function outcomeToShow(outcome: TurnOutcome, successWasWatched: boolean): TurnOutcome;
|
|
62
70
|
/**
|
|
63
71
|
* The look of a turn that is not running. Only the newest turn moves, and only its conclusion is large (the success avatar,
|
|
64
72
|
* or a question to answer): older turns are small still pictures, so a long chat keeps one mascot alive instead of one per
|
|
@@ -169,12 +177,26 @@ export interface FinishedGeneration {
|
|
|
169
177
|
* on the diff. Null when the run has none: the first message of a conversation, a page that does not give changes.
|
|
170
178
|
*/
|
|
171
179
|
changes?: CodeChanges | null;
|
|
180
|
+
/**
|
|
181
|
+
* The agent's own words about what the run changed, when the page gave them (the gateway sends them with the completed
|
|
182
|
+
* operation). The conclusion says them in place of the catalog's "Done" sentence. Null when the run has none: a gateway that
|
|
183
|
+
* sends no summary, a build that was not kept.
|
|
184
|
+
*/
|
|
185
|
+
summary?: string | null;
|
|
172
186
|
}
|
|
173
187
|
/**
|
|
174
188
|
* The most text kept of either part. A generation streams tails; the chat stitches them, and a runaway stream
|
|
175
189
|
* must not grow memory without end.
|
|
176
190
|
*/
|
|
177
191
|
export declare const MAX_KEPT_GENERATION_CHARS = 120000;
|
|
192
|
+
/** The most characters kept of the agent's summary of a run: a sentence or two, never an essay. A longer one is cut and ends in "…". */
|
|
193
|
+
export declare const MAX_KEPT_SUMMARY_CHARS = 600;
|
|
194
|
+
/**
|
|
195
|
+
* The agent's summary of a run, read from whatever the page gave: the words as one paragraph (line breaks and runs of spaces become
|
|
196
|
+
* one space), within `MAX_KEPT_SUMMARY_CHARS`. Anything that is not text, or is only spaces, reads as no summary, and the conclusion
|
|
197
|
+
* keeps the catalog's sentence. It never throws.
|
|
198
|
+
*/
|
|
199
|
+
export declare function readGenerationSummary(raw: unknown): string | null;
|
|
178
200
|
/**
|
|
179
201
|
* A prompt as a way to recognise its turn. A message id is no use: the host swaps a made-up id for the saved one, and
|
|
180
202
|
* may reload the whole conversation after a generation. The words the person wrote survive both.
|
|
@@ -182,7 +204,8 @@ export declare const MAX_KEPT_GENERATION_CHARS = 120000;
|
|
|
182
204
|
export declare function normalizePromptText(content: string | null | undefined): string;
|
|
183
205
|
/**
|
|
184
206
|
* What to keep of a generation that ended, or null when it left nothing to show. `changes` is whatever the page gave as the
|
|
185
|
-
* changes of the run:
|
|
207
|
+
* changes of the run, and `summary` the agent's words about them: both are read here (a part that is not usable is dropped), so
|
|
208
|
+
* what is kept is always plain, bounded data. A run that left only a summary is kept: its conclusion says it.
|
|
186
209
|
*/
|
|
187
210
|
export declare function keepFinishedGeneration(input: {
|
|
188
211
|
promptText: string | null | undefined;
|
|
@@ -190,7 +213,15 @@ export declare function keepFinishedGeneration(input: {
|
|
|
190
213
|
code: string;
|
|
191
214
|
thoughtSeconds: number | null;
|
|
192
215
|
changes?: unknown;
|
|
216
|
+
summary?: unknown;
|
|
193
217
|
}): FinishedGeneration | null;
|
|
218
|
+
/**
|
|
219
|
+
* The kept work of a run with the agent's summary of that run put into it, for a page that gives the summary AFTER the run ended
|
|
220
|
+
* (it comes with the completed operation, like the changes: see `withChangesOfTheRun`). It gives back the very same object when
|
|
221
|
+
* there is nothing to put in (the work has a summary already, or what was given is not usable), so the caller can tell by identity
|
|
222
|
+
* that nothing changed.
|
|
223
|
+
*/
|
|
224
|
+
export declare function withSummaryOfTheRun(finished: FinishedGeneration, summary: unknown): FinishedGeneration;
|
|
194
225
|
/**
|
|
195
226
|
* The kept work of a run with the changes of that run put into it, for a page that gives the changes AFTER the run ended. The
|
|
196
227
|
* gateway sends them with the completed operation, and a page that lets the chat take the next message as soon as the preview is
|
package/dist/utils/agentTurn.js
CHANGED
|
@@ -45,6 +45,16 @@ export function turnOutcome(replies, generationRanForTurn) {
|
|
|
45
45
|
return 'success';
|
|
46
46
|
return 'answer';
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* The outcome the avatar is DRAWN for. A success is celebrated (the large avatar plays its one-shot) only by a person who just
|
|
50
|
+
* watched the work succeed. A turn that was judged a success but was not watched happen is drawn as a plain answer, at rest:
|
|
51
|
+
* a conversation that was loaded, work that came back from this browser's archive after a reload (or when the conversation was
|
|
52
|
+
* opened again) is the work of an earlier run, and a refresh must leave the avatar as it was and not make it do the task again.
|
|
53
|
+
* Every other outcome (a failure, a stop, a question) is how the turn really ended and is drawn as it is.
|
|
54
|
+
*/
|
|
55
|
+
export function outcomeToShow(outcome, successWasWatched) {
|
|
56
|
+
return outcome === 'success' && !successWasWatched ? 'answer' : outcome;
|
|
57
|
+
}
|
|
48
58
|
/**
|
|
49
59
|
* The look of a turn that is not running. Only the newest turn moves, and only its conclusion is large (the success avatar,
|
|
50
60
|
* or a question to answer): older turns are small still pictures, so a long chat keeps one mascot alive instead of one per
|
|
@@ -189,6 +199,23 @@ export function finishedModulesView(finished) {
|
|
|
189
199
|
* must not grow memory without end.
|
|
190
200
|
*/
|
|
191
201
|
export const MAX_KEPT_GENERATION_CHARS = 120_000;
|
|
202
|
+
/** The most characters kept of the agent's summary of a run: a sentence or two, never an essay. A longer one is cut and ends in "…". */
|
|
203
|
+
export const MAX_KEPT_SUMMARY_CHARS = 600;
|
|
204
|
+
/**
|
|
205
|
+
* The agent's summary of a run, read from whatever the page gave: the words as one paragraph (line breaks and runs of spaces become
|
|
206
|
+
* one space), within `MAX_KEPT_SUMMARY_CHARS`. Anything that is not text, or is only spaces, reads as no summary, and the conclusion
|
|
207
|
+
* keeps the catalog's sentence. It never throws.
|
|
208
|
+
*/
|
|
209
|
+
export function readGenerationSummary(raw) {
|
|
210
|
+
if (typeof raw !== 'string')
|
|
211
|
+
return null;
|
|
212
|
+
const asOneParagraph = raw.replace(/\s+/g, ' ').trim();
|
|
213
|
+
if (asOneParagraph === '')
|
|
214
|
+
return null;
|
|
215
|
+
if (asOneParagraph.length <= MAX_KEPT_SUMMARY_CHARS)
|
|
216
|
+
return asOneParagraph;
|
|
217
|
+
return `${asOneParagraph.slice(0, MAX_KEPT_SUMMARY_CHARS - 1).trimEnd()}…`;
|
|
218
|
+
}
|
|
192
219
|
/**
|
|
193
220
|
* A prompt as a way to recognise its turn. A message id is no use: the host swaps a made-up id for the saved one, and
|
|
194
221
|
* may reload the whole conversation after a generation. The words the person wrote survive both.
|
|
@@ -198,22 +225,38 @@ export function normalizePromptText(content) {
|
|
|
198
225
|
}
|
|
199
226
|
/**
|
|
200
227
|
* What to keep of a generation that ended, or null when it left nothing to show. `changes` is whatever the page gave as the
|
|
201
|
-
* changes of the run:
|
|
228
|
+
* changes of the run, and `summary` the agent's words about them: both are read here (a part that is not usable is dropped), so
|
|
229
|
+
* what is kept is always plain, bounded data. A run that left only a summary is kept: its conclusion says it.
|
|
202
230
|
*/
|
|
203
231
|
export function keepFinishedGeneration(input) {
|
|
204
232
|
const thinking = input.thinking.slice(-MAX_KEPT_GENERATION_CHARS);
|
|
205
233
|
const code = input.code.slice(-MAX_KEPT_GENERATION_CHARS);
|
|
206
234
|
const changes = readCodeChanges(input.changes);
|
|
207
|
-
|
|
235
|
+
const summary = readGenerationSummary(input.summary);
|
|
236
|
+
if (thinking.trim().length === 0 && code.trim().length === 0 && changes === null && summary === null)
|
|
208
237
|
return null;
|
|
209
238
|
return {
|
|
210
239
|
prompt: normalizePromptText(input.promptText),
|
|
211
240
|
thinking,
|
|
212
241
|
code,
|
|
213
242
|
thoughtSeconds: input.thoughtSeconds,
|
|
214
|
-
changes
|
|
243
|
+
changes,
|
|
244
|
+
// Left out when there is none: work kept without a summary has the shape it always had
|
|
245
|
+
...(summary !== null ? { summary } : {})
|
|
215
246
|
};
|
|
216
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* The kept work of a run with the agent's summary of that run put into it, for a page that gives the summary AFTER the run ended
|
|
250
|
+
* (it comes with the completed operation, like the changes: see `withChangesOfTheRun`). It gives back the very same object when
|
|
251
|
+
* there is nothing to put in (the work has a summary already, or what was given is not usable), so the caller can tell by identity
|
|
252
|
+
* that nothing changed.
|
|
253
|
+
*/
|
|
254
|
+
export function withSummaryOfTheRun(finished, summary) {
|
|
255
|
+
if (finished.summary)
|
|
256
|
+
return finished;
|
|
257
|
+
const readSummary = readGenerationSummary(summary);
|
|
258
|
+
return readSummary === null ? finished : { ...finished, summary: readSummary };
|
|
259
|
+
}
|
|
217
260
|
/**
|
|
218
261
|
* The kept work of a run with the changes of that run put into it, for a page that gives the changes AFTER the run ended. The
|
|
219
262
|
* gateway sends them with the completed operation, and a page that lets the chat take the next message as soon as the preview is
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telling a file that comes again from a file that is new. A picture dragged twice, a screenshot pasted after it was dropped, a
|
|
3
|
+
* file picked a second time: they are the same bytes under another name or none ("image.png" is every screenshot's name), so the
|
|
4
|
+
* name says nothing and the bytes do. A file's fingerprint is made from them, and a file whose fingerprint is in the tray is not
|
|
5
|
+
* attached again.
|
|
6
|
+
*
|
|
7
|
+
* Plain TypeScript apart from the Web Crypto call, which a test can reach in Node: no Svelte in it.
|
|
8
|
+
*/
|
|
9
|
+
import type { AttachmentInput } from '../types/attachment.js';
|
|
10
|
+
/** A file up to this size is read whole to make its fingerprint; a larger one (a video) is told by its two ends and its size. */
|
|
11
|
+
export declare const FULL_FINGERPRINT_MAX_BYTES: number;
|
|
12
|
+
/**
|
|
13
|
+
* What the file's bytes are, as a short text: its type and size, and a SHA-256 of its content (of the two ends of a large file).
|
|
14
|
+
* Two files with the same fingerprint are the same file for the tray. When the browser cannot hash (a page that is not a secure
|
|
15
|
+
* context has no `crypto.subtle`) or the file cannot be read, the fingerprint is made from the name, size and date instead: the
|
|
16
|
+
* same file picked again still matches, and nothing is ever refused because of it.
|
|
17
|
+
*/
|
|
18
|
+
export declare function fingerprintFile(file: File): Promise<string>;
|
|
19
|
+
/** The place, in the list given, of the file that has this fingerprint; -1 when the file is new (or has no fingerprint to compare). */
|
|
20
|
+
export declare function indexOfAttachmentWithFingerprint(attachments: readonly Pick<AttachmentInput, 'fingerprint'>[], fingerprint: string): number;
|
|
21
|
+
/** One file that arrives, with its fingerprint. */
|
|
22
|
+
export interface FingerprintedFile<Entry extends {
|
|
23
|
+
file: File;
|
|
24
|
+
}> {
|
|
25
|
+
entry: Entry;
|
|
26
|
+
fingerprint: string;
|
|
27
|
+
}
|
|
28
|
+
/** What `splitNewFromKnown` found. */
|
|
29
|
+
export interface KnownAndNewFiles<Entry extends {
|
|
30
|
+
file: File;
|
|
31
|
+
}> {
|
|
32
|
+
/** The files that are new, in the order they arrived, each with its fingerprint. */
|
|
33
|
+
fresh: FingerprintedFile<Entry>[];
|
|
34
|
+
/** The files whose bytes the tray already holds (or that came twice in this batch), with the place of the file in the list given. */
|
|
35
|
+
known: Array<{
|
|
36
|
+
entry: Entry;
|
|
37
|
+
fingerprint: string;
|
|
38
|
+
existingIndex: number;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Sorts what arrives into what is new and what the tray (or the batch itself, or a batch still being prepared: `pendingFingerprints`)
|
|
43
|
+
* already holds. A file that comes twice in one batch is new once. `existingIndex` of a file that came twice in a batch, or that is
|
|
44
|
+
* still being prepared, is -1: there is no tile to point at yet.
|
|
45
|
+
*/
|
|
46
|
+
export declare function splitNewFromKnown<Entry extends {
|
|
47
|
+
file: File;
|
|
48
|
+
}>(arrivals: readonly FingerprintedFile<Entry>[], attachments: readonly Pick<AttachmentInput, 'fingerprint'>[], pendingFingerprints?: ReadonlySet<string>): KnownAndNewFiles<Entry>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { chatWarn } from './logger.js';
|
|
2
|
+
/** A file up to this size is read whole to make its fingerprint; a larger one (a video) is told by its two ends and its size. */
|
|
3
|
+
export const FULL_FINGERPRINT_MAX_BYTES = 32 * 1024 * 1024;
|
|
4
|
+
/** How much of each end of a large file its fingerprint reads. */
|
|
5
|
+
const LARGE_FILE_SAMPLE_BYTES = 1024 * 1024;
|
|
6
|
+
/** The bytes as a hexadecimal text. */
|
|
7
|
+
function toHexadecimal(bytes) {
|
|
8
|
+
return Array.from(new Uint8Array(bytes), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* What the file's bytes are, as a short text: its type and size, and a SHA-256 of its content (of the two ends of a large file).
|
|
12
|
+
* Two files with the same fingerprint are the same file for the tray. When the browser cannot hash (a page that is not a secure
|
|
13
|
+
* context has no `crypto.subtle`) or the file cannot be read, the fingerprint is made from the name, size and date instead: the
|
|
14
|
+
* same file picked again still matches, and nothing is ever refused because of it.
|
|
15
|
+
*/
|
|
16
|
+
export async function fingerprintFile(file) {
|
|
17
|
+
const kind = `${file.type || 'unknown'}:${file.size}`;
|
|
18
|
+
try {
|
|
19
|
+
const digest = globalThis.crypto?.subtle?.digest;
|
|
20
|
+
if (typeof digest !== 'function')
|
|
21
|
+
throw new Error('no Web Crypto in this page');
|
|
22
|
+
let content;
|
|
23
|
+
if (file.size <= FULL_FINGERPRINT_MAX_BYTES) {
|
|
24
|
+
content = await file.arrayBuffer();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const head = await file.slice(0, LARGE_FILE_SAMPLE_BYTES).arrayBuffer();
|
|
28
|
+
const tail = await file.slice(file.size - LARGE_FILE_SAMPLE_BYTES).arrayBuffer();
|
|
29
|
+
const joined = new Uint8Array(head.byteLength + tail.byteLength);
|
|
30
|
+
joined.set(new Uint8Array(head), 0);
|
|
31
|
+
joined.set(new Uint8Array(tail), head.byteLength);
|
|
32
|
+
content = joined.buffer;
|
|
33
|
+
}
|
|
34
|
+
const hash = await globalThis.crypto.subtle.digest('SHA-256', content);
|
|
35
|
+
return `${kind}:${toHexadecimal(hash)}`;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
chatWarn('[attachmentDuplicates] The file could not be hashed, its name, size and date stand in for it:', file.name, error);
|
|
39
|
+
return `${kind}:name=${file.name}:modified=${file.lastModified}`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** The place, in the list given, of the file that has this fingerprint; -1 when the file is new (or has no fingerprint to compare). */
|
|
43
|
+
export function indexOfAttachmentWithFingerprint(attachments, fingerprint) {
|
|
44
|
+
if (!fingerprint)
|
|
45
|
+
return -1;
|
|
46
|
+
return attachments.findIndex((attachment) => attachment.fingerprint === fingerprint);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sorts what arrives into what is new and what the tray (or the batch itself, or a batch still being prepared: `pendingFingerprints`)
|
|
50
|
+
* already holds. A file that comes twice in one batch is new once. `existingIndex` of a file that came twice in a batch, or that is
|
|
51
|
+
* still being prepared, is -1: there is no tile to point at yet.
|
|
52
|
+
*/
|
|
53
|
+
export function splitNewFromKnown(arrivals, attachments, pendingFingerprints = new Set()) {
|
|
54
|
+
const result = { fresh: [], known: [] };
|
|
55
|
+
const seenInBatch = new Set();
|
|
56
|
+
for (const arrival of arrivals) {
|
|
57
|
+
const existingIndex = indexOfAttachmentWithFingerprint(attachments, arrival.fingerprint);
|
|
58
|
+
if (existingIndex >= 0) {
|
|
59
|
+
result.known.push({ ...arrival, existingIndex });
|
|
60
|
+
}
|
|
61
|
+
else if (seenInBatch.has(arrival.fingerprint) || pendingFingerprints.has(arrival.fingerprint)) {
|
|
62
|
+
result.known.push({ ...arrival, existingIndex: -1 });
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
seenInBatch.add(arrival.fingerprint);
|
|
66
|
+
result.fresh.push(arrival);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which files the message box takes, and how large. Kept apart from the components so Node can test it.
|
|
3
|
+
*
|
|
4
|
+
* It answers two questions, with the same rules: while files are dragged over the chat (only the types are known then, the
|
|
5
|
+
* browser tells nothing of the sizes until they are dropped), and when they are dropped, picked or pasted (the sizes are known).
|
|
6
|
+
*
|
|
7
|
+
* The kinds are the ones the "+" says it adds: images, video, PDF, CSV and text. Whether the builder can read them all is another
|
|
8
|
+
* matter (it reads pictures, CSV and text; see the creator's `codegenAttachments`): a file that is kept but not read is told so
|
|
9
|
+
* after it is sent, and the tooltip of the "+" says so before.
|
|
10
|
+
*/
|
|
11
|
+
/** The kind of a file the box takes. */
|
|
12
|
+
export type AttachmentKind = 'image' | 'video' | 'document';
|
|
13
|
+
/** Why a file was left out. */
|
|
14
|
+
export type AttachmentRefusalReason = 'type' | 'size' | 'folder';
|
|
15
|
+
/** A file that was left out, and why. `limitBytes` is the size it went over, for the sentence that says so. */
|
|
16
|
+
export interface RefusedAttachment {
|
|
17
|
+
file: File;
|
|
18
|
+
reason: AttachmentRefusalReason;
|
|
19
|
+
limitBytes?: number;
|
|
20
|
+
}
|
|
21
|
+
/** The largest file of each kind. A picture is made smaller before it is sent when it is large (see `prepareChatImageFileForUpload`), so the limit is checked on the picture that is sent. */
|
|
22
|
+
export declare const ATTACHMENT_SIZE_LIMITS_BYTES: Record<AttachmentKind, number>;
|
|
23
|
+
/**
|
|
24
|
+
* The kind of a file by its type, and by its extension when the type says nothing. Nothing (null) for a file the box does not
|
|
25
|
+
* take: a zip, an executable, a font, a sound, a Word file.
|
|
26
|
+
*/
|
|
27
|
+
export declare function attachmentKindOf(file: {
|
|
28
|
+
name?: string;
|
|
29
|
+
type?: string;
|
|
30
|
+
}): AttachmentKind | null;
|
|
31
|
+
/** Whether something dragged looks like a folder (it has no name, no type and no size: a file with those is empty, which nothing attaches). */
|
|
32
|
+
export declare function looksLikeFolder(file: {
|
|
33
|
+
size: number;
|
|
34
|
+
type?: string;
|
|
35
|
+
}): boolean;
|
|
36
|
+
/** What `checkAttachmentFile` found: the file is taken, or it is left out for a reason. */
|
|
37
|
+
export type AttachmentCheck = {
|
|
38
|
+
ok: true;
|
|
39
|
+
kind: AttachmentKind;
|
|
40
|
+
} | {
|
|
41
|
+
ok: false;
|
|
42
|
+
reason: AttachmentRefusalReason;
|
|
43
|
+
limitBytes?: number;
|
|
44
|
+
};
|
|
45
|
+
/** How a check is made. */
|
|
46
|
+
export interface AttachmentCheckOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Leave the size of a picture unchecked. A large picture is made smaller before it is sent (see `prepareChatImageFileForUpload`),
|
|
49
|
+
* so a file that arrives too large may still go; the picture that is sent is checked, once it is made.
|
|
50
|
+
*/
|
|
51
|
+
skipPictureSize?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** Whether one file is taken: a kind the box takes, and not larger than that kind's limit. */
|
|
54
|
+
export declare function checkAttachmentFile(file: File, options?: AttachmentCheckOptions): AttachmentCheck;
|
|
55
|
+
/** The files that are taken (in the order they came) and the ones that were left out, each with its reason. */
|
|
56
|
+
export declare function partitionAttachmentFiles(files: readonly File[], options?: AttachmentCheckOptions): {
|
|
57
|
+
accepted: File[];
|
|
58
|
+
refused: RefusedAttachment[];
|
|
59
|
+
};
|
|
60
|
+
/** What the file chooser of the "+" offers: the kinds the box takes (a person is not invited to pick what would be refused). */
|
|
61
|
+
export declare const ATTACHMENT_ACCEPT_ATTRIBUTE = "image/*,video/*,text/*,application/pdf,application/json,.pdf,.txt,.md,.markdown,.csv,.tsv,.json,.mermaid,.mmd";
|
|
62
|
+
/** One thing that is dragged: what the browser tells of it before the drop (`DataTransferItem`: its kind and its type, never its size or name). */
|
|
63
|
+
export interface DraggedItem {
|
|
64
|
+
kind: string;
|
|
65
|
+
type: string;
|
|
66
|
+
}
|
|
67
|
+
/** What a drag over the chat says about what is dragged. */
|
|
68
|
+
export type DragVerdict = 'accept' | 'refuse' | 'none';
|
|
69
|
+
/**
|
|
70
|
+
* Whether the files being dragged are ones the box takes, from what the browser tells while they are over the page: their types.
|
|
71
|
+
* 'none' when no file is dragged (text, a link: the chat is not a drop target for them); 'refuse' when every file is of a kind the
|
|
72
|
+
* box does not take; 'accept' otherwise: a file whose type the system does not tell (a `.md`, a `.mmd`) gets the benefit of the
|
|
73
|
+
* doubt, and the drop says what could not be taken. A mixed drag is 'accept': the files that can be taken are attached.
|
|
74
|
+
*/
|
|
75
|
+
export declare function judgeDraggedItems(items: ReadonlyArray<DraggedItem>): DragVerdict;
|
|
76
|
+
/** What a drag event says of what is dragged (`event.dataTransfer`): the kinds of data it holds and, per item, its kind and type. */
|
|
77
|
+
export interface DragTransferInfo {
|
|
78
|
+
types?: ArrayLike<string> | null;
|
|
79
|
+
items?: ArrayLike<DraggedItem> | null;
|
|
80
|
+
}
|
|
81
|
+
/** Whether files (and not a text or a link) are being dragged: the browser lists `Files` among the types of the drag. */
|
|
82
|
+
export declare function dragCarriesFiles(transfer: DragTransferInfo | null | undefined): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* The verdict on a drag event: 'none' when no file is dragged, otherwise what `judgeDraggedItems` says of the items. A browser that
|
|
85
|
+
* lists no items before the drop (Safari does not) says nothing that refuses the files, so the drag gets the benefit of the doubt
|
|
86
|
+
* and the drop says what could not be taken.
|
|
87
|
+
*/
|
|
88
|
+
export declare function judgeDragTransfer(transfer: DragTransferInfo | null | undefined): DragVerdict;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which files the message box takes, and how large. Kept apart from the components so Node can test it.
|
|
3
|
+
*
|
|
4
|
+
* It answers two questions, with the same rules: while files are dragged over the chat (only the types are known then, the
|
|
5
|
+
* browser tells nothing of the sizes until they are dropped), and when they are dropped, picked or pasted (the sizes are known).
|
|
6
|
+
*
|
|
7
|
+
* The kinds are the ones the "+" says it adds: images, video, PDF, CSV and text. Whether the builder can read them all is another
|
|
8
|
+
* matter (it reads pictures, CSV and text; see the creator's `codegenAttachments`): a file that is kept but not read is told so
|
|
9
|
+
* after it is sent, and the tooltip of the "+" says so before.
|
|
10
|
+
*/
|
|
11
|
+
/** The largest file of each kind. A picture is made smaller before it is sent when it is large (see `prepareChatImageFileForUpload`), so the limit is checked on the picture that is sent. */
|
|
12
|
+
export const ATTACHMENT_SIZE_LIMITS_BYTES = {
|
|
13
|
+
image: 10 * 1024 * 1024,
|
|
14
|
+
video: 100 * 1024 * 1024,
|
|
15
|
+
document: 25 * 1024 * 1024,
|
|
16
|
+
};
|
|
17
|
+
/** The extensions that make a file a document when its type does not say (a browser gives `.md` and `.mmd` no type on some systems). */
|
|
18
|
+
const DOCUMENT_EXTENSIONS = new Set(['pdf', 'txt', 'md', 'markdown', 'csv', 'tsv', 'json', 'mermaid', 'mmd']);
|
|
19
|
+
/** The extensions of the pictures and videos, for a file whose type is empty. */
|
|
20
|
+
const IMAGE_EXTENSIONS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'avif', 'bmp']);
|
|
21
|
+
const VIDEO_EXTENSIONS = new Set(['mp4', 'webm', 'mov', 'm4v']);
|
|
22
|
+
/** The documents the box takes besides every `text/*`. */
|
|
23
|
+
const DOCUMENT_MIME_TYPES = new Set([
|
|
24
|
+
'application/pdf',
|
|
25
|
+
'application/json',
|
|
26
|
+
'application/csv',
|
|
27
|
+
'application/vnd.ms-excel', // what some systems call a CSV
|
|
28
|
+
]);
|
|
29
|
+
/** The extension of a file name in lower case, or '' when it has none. */
|
|
30
|
+
function extensionOf(fileName) {
|
|
31
|
+
const lastDotIndex = (fileName ?? '').lastIndexOf('.');
|
|
32
|
+
return lastDotIndex > 0 && lastDotIndex < fileName.length - 1
|
|
33
|
+
? fileName.slice(lastDotIndex + 1).toLowerCase()
|
|
34
|
+
: '';
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The kind of a file by its type, and by its extension when the type says nothing. Nothing (null) for a file the box does not
|
|
38
|
+
* take: a zip, an executable, a font, a sound, a Word file.
|
|
39
|
+
*/
|
|
40
|
+
export function attachmentKindOf(file) {
|
|
41
|
+
const type = (file.type ?? '').toLowerCase();
|
|
42
|
+
const extension = extensionOf(file.name ?? '');
|
|
43
|
+
if (type.startsWith('image/'))
|
|
44
|
+
return 'image';
|
|
45
|
+
if (type.startsWith('video/'))
|
|
46
|
+
return 'video';
|
|
47
|
+
if (type.startsWith('text/') || DOCUMENT_MIME_TYPES.has(type))
|
|
48
|
+
return 'document';
|
|
49
|
+
// A type that says nothing (or says "binary") is judged by the name; a type that names another kind of file is left out
|
|
50
|
+
if (type === '' || type === 'application/octet-stream') {
|
|
51
|
+
if (IMAGE_EXTENSIONS.has(extension))
|
|
52
|
+
return 'image';
|
|
53
|
+
if (VIDEO_EXTENSIONS.has(extension))
|
|
54
|
+
return 'video';
|
|
55
|
+
if (DOCUMENT_EXTENSIONS.has(extension))
|
|
56
|
+
return 'document';
|
|
57
|
+
}
|
|
58
|
+
// A `.csv` or `.md` that a system types as something else (an Excel type, a markdown type of its own)
|
|
59
|
+
if (DOCUMENT_EXTENSIONS.has(extension) && extension !== 'json' && extension !== 'pdf' && /^(application|text)\//.test(type)) {
|
|
60
|
+
return 'document';
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/** Whether something dragged looks like a folder (it has no name, no type and no size: a file with those is empty, which nothing attaches). */
|
|
65
|
+
export function looksLikeFolder(file) {
|
|
66
|
+
return file.size === 0 && (file.type ?? '') === '';
|
|
67
|
+
}
|
|
68
|
+
/** Whether one file is taken: a kind the box takes, and not larger than that kind's limit. */
|
|
69
|
+
export function checkAttachmentFile(file, options = {}) {
|
|
70
|
+
if (looksLikeFolder(file))
|
|
71
|
+
return { ok: false, reason: 'folder' };
|
|
72
|
+
const kind = attachmentKindOf(file);
|
|
73
|
+
if (!kind)
|
|
74
|
+
return { ok: false, reason: 'type' };
|
|
75
|
+
if (kind === 'image' && options.skipPictureSize)
|
|
76
|
+
return { ok: true, kind };
|
|
77
|
+
const limitBytes = ATTACHMENT_SIZE_LIMITS_BYTES[kind];
|
|
78
|
+
if (file.size > limitBytes)
|
|
79
|
+
return { ok: false, reason: 'size', limitBytes };
|
|
80
|
+
return { ok: true, kind };
|
|
81
|
+
}
|
|
82
|
+
/** The files that are taken (in the order they came) and the ones that were left out, each with its reason. */
|
|
83
|
+
export function partitionAttachmentFiles(files, options = {}) {
|
|
84
|
+
const accepted = [];
|
|
85
|
+
const refused = [];
|
|
86
|
+
for (const file of files) {
|
|
87
|
+
const check = checkAttachmentFile(file, options);
|
|
88
|
+
if (check.ok)
|
|
89
|
+
accepted.push(file);
|
|
90
|
+
else
|
|
91
|
+
refused.push({ file, reason: check.reason, limitBytes: check.limitBytes });
|
|
92
|
+
}
|
|
93
|
+
return { accepted, refused };
|
|
94
|
+
}
|
|
95
|
+
/** What the file chooser of the "+" offers: the kinds the box takes (a person is not invited to pick what would be refused). */
|
|
96
|
+
export const ATTACHMENT_ACCEPT_ATTRIBUTE = 'image/*,video/*,text/*,application/pdf,application/json,.pdf,.txt,.md,.markdown,.csv,.tsv,.json,.mermaid,.mmd';
|
|
97
|
+
/**
|
|
98
|
+
* Whether the files being dragged are ones the box takes, from what the browser tells while they are over the page: their types.
|
|
99
|
+
* 'none' when no file is dragged (text, a link: the chat is not a drop target for them); 'refuse' when every file is of a kind the
|
|
100
|
+
* box does not take; 'accept' otherwise: a file whose type the system does not tell (a `.md`, a `.mmd`) gets the benefit of the
|
|
101
|
+
* doubt, and the drop says what could not be taken. A mixed drag is 'accept': the files that can be taken are attached.
|
|
102
|
+
*/
|
|
103
|
+
export function judgeDraggedItems(items) {
|
|
104
|
+
const files = items.filter((item) => item.kind === 'file');
|
|
105
|
+
if (files.length === 0)
|
|
106
|
+
return 'none';
|
|
107
|
+
const everyFileIsRefused = files.every((item) => {
|
|
108
|
+
const type = (item.type ?? '').toLowerCase();
|
|
109
|
+
if (type === '' || type === 'application/octet-stream')
|
|
110
|
+
return false;
|
|
111
|
+
return attachmentKindOf({ type, name: '' }) === null;
|
|
112
|
+
});
|
|
113
|
+
return everyFileIsRefused ? 'refuse' : 'accept';
|
|
114
|
+
}
|
|
115
|
+
/** Whether files (and not a text or a link) are being dragged: the browser lists `Files` among the types of the drag. */
|
|
116
|
+
export function dragCarriesFiles(transfer) {
|
|
117
|
+
if (!transfer?.types)
|
|
118
|
+
return false;
|
|
119
|
+
return Array.from(transfer.types).includes('Files');
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The verdict on a drag event: 'none' when no file is dragged, otherwise what `judgeDraggedItems` says of the items. A browser that
|
|
123
|
+
* lists no items before the drop (Safari does not) says nothing that refuses the files, so the drag gets the benefit of the doubt
|
|
124
|
+
* and the drop says what could not be taken.
|
|
125
|
+
*/
|
|
126
|
+
export function judgeDragTransfer(transfer) {
|
|
127
|
+
if (!dragCarriesFiles(transfer))
|
|
128
|
+
return 'none';
|
|
129
|
+
const items = Array.from(transfer?.items ?? []);
|
|
130
|
+
if (items.length === 0)
|
|
131
|
+
return 'accept';
|
|
132
|
+
const verdict = judgeDraggedItems(items);
|
|
133
|
+
// The types say files while the items do not list any as one (a browser that lists them in its own way): still files
|
|
134
|
+
return verdict === 'none' ? 'accept' : verdict;
|
|
135
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the preview of an attached file shows, and how big its card is. Kept apart from the components so Node can test it.
|
|
3
|
+
*
|
|
4
|
+
* Pressing a thumbnail or a file chip (in the message box, or in a sent message) opens the file in a card that looks and moves like
|
|
5
|
+
* the File Browser's preview: the name and the details of the file on top with the arrows to the file before and after it and the
|
|
6
|
+
* close button, and the file itself below. This file holds the rules of that card that do not need a screen: which kind of preview a
|
|
7
|
+
* file gets, the size of the card for a picture, a video, a PDF or a text, and what the arrow keys do. (The numbers of the card come
|
|
8
|
+
* from the File Browser's own `imagePreviewFrame`, so a picture opened from the chat has the room it has there.)
|
|
9
|
+
*/
|
|
10
|
+
import type { ContentItemOutput } from '../types/message.js';
|
|
11
|
+
/** The kinds of file that open in the preview. Any other file has no preview (a chip of it only names it). */
|
|
12
|
+
export type PreviewKind = 'image' | 'video' | 'pdf' | 'text';
|
|
13
|
+
/** The glyph a file chip carries in front of its name. */
|
|
14
|
+
export type FileGlyph = 'pdf' | 'video' | 'sheet' | 'text' | 'file';
|
|
15
|
+
/** What the preview needs to know about a file. */
|
|
16
|
+
export interface PreviewableFile {
|
|
17
|
+
/** Where the file can be read from (an address on the web, or a `blob:` address for a file that is still on this device). */
|
|
18
|
+
url: string;
|
|
19
|
+
/** The name the person sees on top of the card. */
|
|
20
|
+
name: string;
|
|
21
|
+
/** The type the file was saved with (`image/png`); empty when it is not known. */
|
|
22
|
+
type: string;
|
|
23
|
+
/** The size in bytes, or null when it is not known. */
|
|
24
|
+
size: number | null;
|
|
25
|
+
}
|
|
26
|
+
/** The facts the kind of a file is worked out from. */
|
|
27
|
+
export interface PreviewFacts {
|
|
28
|
+
url?: string | null;
|
|
29
|
+
name?: string | null;
|
|
30
|
+
type?: string | null;
|
|
31
|
+
}
|
|
32
|
+
/** The extension of a file name in lower case, or '' when the name has none (`archive.tar.gz` is `gz`; `.env` has none). */
|
|
33
|
+
export declare function extensionOf(name: string | null | undefined): string;
|
|
34
|
+
/** The extension as the details line of the card writes it (`PNG`), or '' when the name has none. */
|
|
35
|
+
export declare function extensionLabelOf(name: string | null | undefined): string;
|
|
36
|
+
/**
|
|
37
|
+
* Which kind of preview a file gets, or null when it gets none. The type of the file decides; when the type says nothing (an empty
|
|
38
|
+
* type, or the catch-all `application/octet-stream`) the extension of the name does; a picture is also known by its address.
|
|
39
|
+
*/
|
|
40
|
+
export declare function previewKindOf(facts: PreviewFacts): PreviewKind | null;
|
|
41
|
+
/** The glyph a file chip carries: it says at a glance what the file is, whether or not the file has a preview. */
|
|
42
|
+
export declare function fileGlyphOf(facts: PreviewFacts): FileGlyph;
|
|
43
|
+
/**
|
|
44
|
+
* A file of a sent message as the preview takes it, or null when it has none: a file without an address cannot be read, and a kind
|
|
45
|
+
* the preview does not show (a spreadsheet, an archive) has no card.
|
|
46
|
+
*/
|
|
47
|
+
export declare function previewFileOfAttachment(attachment: ContentItemOutput): PreviewableFile | null;
|
|
48
|
+
/** The files of one place (a message, the message box) that the preview can step through, and where each of them stands. */
|
|
49
|
+
export interface PreviewGroup {
|
|
50
|
+
/** The files the card steps through with its arrows, in the order they are drawn. */
|
|
51
|
+
files: PreviewableFile[];
|
|
52
|
+
/** The place of an attachment in `files`, or -1 when that attachment has no preview. */
|
|
53
|
+
indexOf: (attachment: ContentItemOutput) => number;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The files a card steps through when one of them is pressed: those that have a preview, in the order the screen draws them (so the
|
|
57
|
+
* right arrow goes to the file drawn after this one). The files that have none are left out; `indexOf` says so with -1.
|
|
58
|
+
*/
|
|
59
|
+
export declare function previewGroupOf(attachmentsInDrawnOrder: readonly ContentItemOutput[]): PreviewGroup;
|
|
60
|
+
/** What a key press does in the card: go to the file before this one, or to the file after it. */
|
|
61
|
+
export type PreviewKeyAction = 'previous' | 'next';
|
|
62
|
+
/**
|
|
63
|
+
* What an arrow key does in the card, or null for any other key. The arrow keys follow the reading direction: the key that points
|
|
64
|
+
* toward the start of the line goes to the previous file and the key that points toward the end goes to the next one. On a left to
|
|
65
|
+
* right screen that is the left arrow and the right arrow; on a right to left screen the two swap, so the arrow points the way the
|
|
66
|
+
* files are laid out.
|
|
67
|
+
*/
|
|
68
|
+
export declare function previewKeyAction(key: string, direction: 'ltr' | 'rtl'): PreviewKeyAction | null;
|
|
69
|
+
/**
|
|
70
|
+
* The place of the file an arrow leads to, or null when there is none (the first file has no one before it, the last none after it;
|
|
71
|
+
* the card does not wrap around, so the arrow is disabled at the ends).
|
|
72
|
+
*/
|
|
73
|
+
export declare function steppedPreviewIndex(currentIndex: number, direction: PreviewKeyAction, fileCount: number): number | null;
|
|
74
|
+
/** The room the card has to fit in: the window the chat is in. */
|
|
75
|
+
export interface PreviewHost {
|
|
76
|
+
width: number;
|
|
77
|
+
height: number;
|
|
78
|
+
}
|
|
79
|
+
/** The size of a card in px, and (for a picture or a video) of the file inside it. */
|
|
80
|
+
export interface PreviewFrame {
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
pictureWidth: number;
|
|
84
|
+
pictureHeight: number;
|
|
85
|
+
}
|
|
86
|
+
/** What the card takes for itself around the file, in px: the same numbers as the File Browser's card (the padding, the header row). */
|
|
87
|
+
export declare const PREVIEW_CARD: {
|
|
88
|
+
/** The room between the card and the edge of the window, at each side. */
|
|
89
|
+
readonly sideMargin: 24;
|
|
90
|
+
/** The padding of the card at both sides (24 each) and its two 1 px borders. */
|
|
91
|
+
readonly horizontalPad: 50;
|
|
92
|
+
/** The top row of the card (the name and details, 36 px) and the gap below it (16 px). */
|
|
93
|
+
readonly headerBlock: 52;
|
|
94
|
+
/** The padding of the card at the top and the bottom and its two borders. */
|
|
95
|
+
readonly verticalPad: 50;
|
|
96
|
+
/** The air kept between the file and the edge of the window. */
|
|
97
|
+
readonly contentVerticalGutter: 48;
|
|
98
|
+
/** The narrowest card: the name, the two arrows and the close button need this much. */
|
|
99
|
+
readonly minWidth: 320;
|
|
100
|
+
/** The most of the window a card takes. */
|
|
101
|
+
readonly maxHostFraction: 0.92;
|
|
102
|
+
/** The smallest side a picture is drawn at. */
|
|
103
|
+
readonly minPictureSide: 68;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* The card of a picture (or of a video, once the video tells its size): as big as the picture is, and no bigger than the window
|
|
107
|
+
* leaves room for. A small picture is never made larger. A picture that tells no size is drawn as a 4:3 one.
|
|
108
|
+
*/
|
|
109
|
+
export declare function computePictureFrame(naturalWidth: number, naturalHeight: number, hostSize: PreviewHost): PreviewFrame;
|
|
110
|
+
/** The cards that are not a picture: a file that cannot be shown, one that is on its way, a PDF, a text. */
|
|
111
|
+
export type DocumentCardKind = 'error' | 'loading' | 'pdf' | 'text';
|
|
112
|
+
/** The card of a file that is not a picture: a fixed size for a message or a text, the whole window for a PDF (which needs the room). */
|
|
113
|
+
export declare function computeDocumentFrame(kind: DocumentCardKind, hostSize: PreviewHost): {
|
|
114
|
+
width: number;
|
|
115
|
+
height: number;
|
|
116
|
+
};
|
|
117
|
+
/** The most characters of a text the card reads: the card shows a part of a very long text and says so, instead of freezing on it. */
|
|
118
|
+
export declare const PREVIEW_TEXT_MAX_CHARACTERS = 200000;
|
|
119
|
+
/** A text as the card shows it. */
|
|
120
|
+
export interface PreviewText {
|
|
121
|
+
text: string;
|
|
122
|
+
/** Whether the text was longer than the card reads, so the card says that the rest is not shown. */
|
|
123
|
+
truncated: boolean;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The text of a file as the card shows it: a JSON file is indented so it can be read (a text that is not valid JSON stays as it is),
|
|
127
|
+
* the line ends of a Windows file are the line ends of any other, and a text longer than `PREVIEW_TEXT_MAX_CHARACTERS` is cut.
|
|
128
|
+
*/
|
|
129
|
+
export declare function formatPreviewText(rawText: string, name: string | null | undefined): PreviewText;
|
|
130
|
+
/** A length of time as `m:ss` (a video of 75 seconds is `1:15`), or '' when it is not known. */
|
|
131
|
+
export declare function durationText(seconds: number | null | undefined): string;
|
|
132
|
+
/** What the details line of the card is made of, in its order: the extension, the size of a picture, the length of a video, the weight. */
|
|
133
|
+
export interface PreviewDetails {
|
|
134
|
+
name: string;
|
|
135
|
+
kind: PreviewKind | null;
|
|
136
|
+
size: number | null;
|
|
137
|
+
/** The size in px of a picture or a video, once it is known. */
|
|
138
|
+
dimensions?: {
|
|
139
|
+
width: number;
|
|
140
|
+
height: number;
|
|
141
|
+
} | null;
|
|
142
|
+
/** The length of a video in seconds, once it is known. */
|
|
143
|
+
durationSeconds?: number | null;
|
|
144
|
+
}
|
|
145
|
+
/** The parts of the details line (`PNG`, `1024 × 1280`, `1.2 MB`); the card joins them with a dot. A part that is not known is left out. */
|
|
146
|
+
export declare function previewDetailParts(details: PreviewDetails, sizeText: (bytes: number) => string): string[];
|