@widgetic/editor 4.1.13 → 4.1.15
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/dist/components/PreviewChromeControls.svelte +15 -21
- package/dist/components/WidgetPreview.svelte +136 -105
- package/dist/components/color-select/ColorSelect.svelte +203 -224
- package/dist/components/color-select/constants.d.ts +4 -2
- package/dist/components/color-select/constants.js +4 -2
- package/dist/components/color-select/render.js +10 -9
- package/dist/components/editors/CompositionEditorApp.svelte +55 -40
- package/dist/components/editors/EditLeaseNotice.svelte +44 -33
- package/dist/components/editors/EmbedEditor.svelte +133 -259
- package/dist/components/editors/PropsEditor.svelte +426 -1333
- package/dist/components/editors/props-editor.css +5 -68
- package/dist/components/ic/AngleInputController.svelte +202 -277
- package/dist/components/ic/BrowserInputController.svelte +813 -803
- package/dist/components/ic/CheckboxInputController.svelte +92 -169
- package/dist/components/ic/CheckboxInputController.svelte.d.ts +2 -8
- package/dist/components/ic/ColorPickerInputController.svelte +1589 -1587
- package/dist/components/ic/ColorPickerInputController.svelte.d.ts +3 -3
- package/dist/components/ic/DateFieldInputController.svelte +455 -465
- package/dist/components/ic/DatePickerInputController.svelte +355 -368
- package/dist/components/ic/DatePickerInputController.svelte.d.ts +1 -1
- package/dist/components/ic/DropdownInputController.svelte +130 -200
- package/dist/components/ic/FixedPositionInputController.svelte +255 -276
- package/dist/components/ic/FixedPositionInputController.svelte.d.ts +5 -1
- package/dist/components/ic/FontInputController.svelte +2110 -2231
- package/dist/components/ic/FontInputController.svelte.d.ts +11 -11
- package/dist/components/ic/InputControllerHeader.svelte +141 -147
- package/dist/components/ic/InputControllerRenderer.svelte +5 -1
- package/dist/components/ic/InputControllerRenderer.svelte.d.ts +2 -1
- package/dist/components/ic/InputControllerRow.svelte +74 -0
- package/dist/components/ic/InputControllerRow.svelte.d.ts +14 -0
- package/dist/components/ic/KnowledgeDocumentsInputController.svelte +401 -0
- package/dist/components/ic/KnowledgeDocumentsInputController.svelte.d.ts +11 -0
- package/dist/components/ic/KnowledgeTextForm.svelte +109 -0
- package/dist/components/ic/KnowledgeTextForm.svelte.d.ts +15 -0
- package/dist/components/ic/NumberControls.svelte +303 -269
- package/dist/components/ic/NumberControls.svelte.d.ts +4 -1
- package/dist/components/ic/NumberInputController.svelte +361 -301
- package/dist/components/ic/NumberInputController.svelte.d.ts +2 -0
- package/dist/components/ic/PositionInputController.svelte +253 -176
- package/dist/components/ic/PositionInputController.svelte.d.ts +2 -0
- package/dist/components/ic/RadioGroupInputController.svelte +71 -137
- package/dist/components/ic/RadioGroupInputController.svelte.d.ts +2 -8
- package/dist/components/ic/RangeCalendarInputController.svelte +233 -270
- package/dist/components/ic/RangeCalendarInputController.svelte.d.ts +2 -2
- package/dist/components/ic/RoundedCornersInputController.svelte +412 -435
- package/dist/components/ic/RoundedCornersInputController.svelte.d.ts +2 -0
- package/dist/components/ic/SliderInputController.svelte +263 -332
- package/dist/components/ic/SliderInputController.svelte.d.ts +1 -1
- package/dist/components/ic/SliderRangeInputController.svelte +140 -144
- package/dist/components/ic/SliderRangeInputController.svelte.d.ts +3 -3
- package/dist/components/ic/SwitchInputController.svelte +47 -109
- package/dist/components/ic/TagsInputController.svelte +95 -110
- package/dist/components/ic/TextAreaInputController.svelte +1794 -1935
- package/dist/components/ic/TextAreaPreviewInputController.svelte +436 -438
- package/dist/components/ic/TextAreaPreviewInputController.svelte.d.ts +2 -2
- package/dist/components/ic/TextInputController.svelte +65 -75
- package/dist/components/ic/TextInputController.svelte.d.ts +2 -0
- package/dist/components/ic/TimeInputController.svelte +433 -431
- package/dist/components/ic/VectorInputController.svelte +1833 -1831
- package/dist/components/ic/VectorInputController.svelte.d.ts +10 -10
- package/dist/components/ic/index.d.ts +2 -0
- package/dist/components/ic/index.js +2 -0
- package/dist/components/ic/shared/CategoryTabs.svelte +28 -35
- package/dist/components/ic/shared/ItemGrid.svelte +145 -179
- package/dist/components/ic/shared/RecentItemsSection.svelte +208 -271
- package/dist/components/ic/shared/RecentItemsSection.svelte.d.ts +1 -1
- package/dist/components/ui/arrows/OpacitySliderArrows.svelte +23 -38
- package/dist/components/ui/arrows/OpacitySliderArrows.svelte.d.ts +1 -4
- package/dist/components/ui/calendar-editor-fit/EditorCalendarStyles.svelte +45 -0
- package/dist/components/ui/calendar-editor-fit/EditorCalendarStyles.svelte.d.ts +26 -0
- package/dist/components/ui/checkerboard-background/CheckerboardBackground.svelte +20 -12
- package/dist/components/ui/shadow-picker-host/ShadowPickerHost.svelte +39 -0
- package/dist/components/ui/shadow-picker-host/ShadowPickerHost.svelte.d.ts +10 -0
- package/dist/composition/compositionApi.d.ts +21 -1
- package/dist/composition/compositionApi.js +64 -5
- package/dist/composition/knowledgeDocuments.d.ts +90 -0
- package/dist/composition/knowledgeDocuments.js +94 -0
- package/dist/composition/knowledgeDocumentsPanel.d.ts +152 -0
- package/dist/composition/knowledgeDocumentsPanel.js +443 -0
- package/dist/i18n/language.d.ts +86 -36
- package/dist/i18n/messages.d.ts +97 -34
- package/dist/i18n/messages.js +104 -34
- package/dist/i18n/translate.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/knowledgeDocumentsContext.d.ts +10 -0
- package/dist/knowledgeDocumentsContext.js +31 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +26 -2
- package/package.json +4 -4
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
|
|
2
|
+
export let className = '';
|
|
3
3
|
</script>
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
<!-- Content paint: checker tile uses the input surface token, not a legacy grey hex. -->
|
|
6
|
+
<div class="checkerboard-bg h-full w-full {className}" aria-hidden="true"></div>
|
|
7
|
+
|
|
8
|
+
<style>
|
|
9
|
+
.checkerboard-bg {
|
|
10
|
+
background-color: var(--background);
|
|
11
|
+
background-image:
|
|
12
|
+
linear-gradient(45deg, var(--input) 25%, transparent 25%),
|
|
13
|
+
linear-gradient(-45deg, var(--input) 25%, transparent 25%),
|
|
14
|
+
linear-gradient(45deg, transparent 75%, var(--input) 75%),
|
|
15
|
+
linear-gradient(-45deg, transparent 75%, var(--input) 75%);
|
|
16
|
+
background-size: 8px 8px;
|
|
17
|
+
background-position:
|
|
18
|
+
0 0,
|
|
19
|
+
0 4px,
|
|
20
|
+
4px -4px,
|
|
21
|
+
-4px 0;
|
|
22
|
+
}
|
|
23
|
+
</style>
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onDestroy, onMount } from 'svelte';
|
|
3
|
+
import { mount, unmount, type Component } from 'svelte';
|
|
4
|
+
|
|
5
|
+
type HostProps = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
component: Component;
|
|
9
|
+
props?: HostProps;
|
|
10
|
+
class?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let { component, props = {}, class: className = '' }: Props = $props();
|
|
14
|
+
|
|
15
|
+
let hostEl: HTMLDivElement | undefined = $state();
|
|
16
|
+
let mounted: ReturnType<typeof mount> | null = $state(null);
|
|
17
|
+
|
|
18
|
+
function adoptStyles(shadow: ShadowRoot) {
|
|
19
|
+
document.querySelectorAll('link[rel="stylesheet"]').forEach((link) => {
|
|
20
|
+
shadow.appendChild(link.cloneNode(true));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
onMount(() => {
|
|
25
|
+
if (!hostEl) return;
|
|
26
|
+
const shadow = hostEl.attachShadow({ mode: 'open' });
|
|
27
|
+
adoptStyles(shadow);
|
|
28
|
+
const surface = document.createElement('div');
|
|
29
|
+
surface.className = `bg-background text-foreground ${className}`.trim();
|
|
30
|
+
shadow.appendChild(surface);
|
|
31
|
+
mounted = mount(component, { target: surface, props });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
onDestroy(() => {
|
|
35
|
+
if (mounted) unmount(mounted);
|
|
36
|
+
});
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<div bind:this={hostEl} class="shadow-picker-host w-full"></div>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Component } from 'svelte';
|
|
2
|
+
type HostProps = Record<string, unknown>;
|
|
3
|
+
interface Props {
|
|
4
|
+
component: Component;
|
|
5
|
+
props?: HostProps;
|
|
6
|
+
class?: string;
|
|
7
|
+
}
|
|
8
|
+
declare const ShadowPickerHost: Component<Props, {}, "">;
|
|
9
|
+
type ShadowPickerHost = ReturnType<typeof ShadowPickerHost>;
|
|
10
|
+
export default ShadowPickerHost;
|
|
@@ -17,9 +17,13 @@
|
|
|
17
17
|
* - every failure is a `CompositionApiError` with the API's own code, so the host can tell an ended session
|
|
18
18
|
* (`EDITOR_SESSION_EXPIRED`...) from a missing composition (`NOT_FOUND`) from a widget that was never
|
|
19
19
|
* published (`WIDGET_NOT_PUBLISHED`);
|
|
20
|
-
* - a request that gets no answer does not hang the editor: it fails with `TIMEOUT` after a set time
|
|
20
|
+
* - a request that gets no answer does not hang the editor: it fails with `TIMEOUT` after a set time;
|
|
21
|
+
* - the documents of an AI Answers widget (`/compositions/{id}/adapters/ai-answers/knowledge`) are listed, added (as typed
|
|
22
|
+
* text, or as a file sent as a multipart form) and removed through the same four rules above. Adding one waits for the API
|
|
23
|
+
* to read and embed the document, which takes longer than any other call, so it has a limit of its own.
|
|
21
24
|
*/
|
|
22
25
|
import { type JsonRecord } from './compositionHelpers.js';
|
|
26
|
+
import { type KnowledgeDocument, type KnowledgeFileInput, type KnowledgeListing, type KnowledgeTextInput } from './knowledgeDocuments.js';
|
|
23
27
|
/** Why the token is being asked for. `expired` means the API just refused the previous one. */
|
|
24
28
|
export type TokenReason = 'initial' | 'expired';
|
|
25
29
|
/** The fields of a composition an editor may change. Anything else is refused by the API for an editor session. */
|
|
@@ -162,6 +166,22 @@ export interface CompositionApi {
|
|
|
162
166
|
peekEditLease(compositionId: string, options?: CompositionRequestOptions): Promise<EditLeaseAnswer>;
|
|
163
167
|
/** Gives up this tab's claim (nothing happens when it holds none, or somebody else does now). */
|
|
164
168
|
releaseEditLease(compositionId: string, sessionId: string, options?: ReleaseEditLeaseOptions): Promise<void>;
|
|
169
|
+
/** The documents the widget's AI answers from (AI Answers), and how much of the knowledge base they use. */
|
|
170
|
+
listKnowledgeDocuments(compositionId: string, options?: CompositionRequestOptions): Promise<KnowledgeListing>;
|
|
171
|
+
/**
|
|
172
|
+
* Adds a document made of text. Answers once the AI can use it, which takes a few seconds, longer for a long text.
|
|
173
|
+
* Costs no credits. The same text cannot be added twice (`KNOWLEDGE_DUPLICATE_DOCUMENT`).
|
|
174
|
+
*/
|
|
175
|
+
addKnowledgeText(compositionId: string, input: KnowledgeTextInput, options?: CompositionRequestOptions): Promise<KnowledgeDocument>;
|
|
176
|
+
/**
|
|
177
|
+
* Adds a document made of a file: a text, Markdown or CSV file, or a picture of a page (a PDF is refused by the API for
|
|
178
|
+
* now). The file is sent as a multipart form (field `file`, and `title` when one is given) and is read on the API's side,
|
|
179
|
+
* which keeps no copy of it. Answers once the AI can use the document, which can take up to a minute and a half for a
|
|
180
|
+
* long one.
|
|
181
|
+
*/
|
|
182
|
+
addKnowledgeFile(compositionId: string, input: KnowledgeFileInput, options?: CompositionRequestOptions): Promise<KnowledgeDocument>;
|
|
183
|
+
/** Takes a document out of the knowledge base, with everything the AI made from it. */
|
|
184
|
+
removeKnowledgeDocument(compositionId: string, documentId: string, options?: CompositionRequestOptions): Promise<void>;
|
|
165
185
|
}
|
|
166
186
|
/** A key that is unique, not secret: the page's `crypto.randomUUID`, else random bytes, else the clock and `Math.random`. */
|
|
167
187
|
export declare function createRandomIdempotencyKey(): string;
|
|
@@ -17,9 +17,13 @@
|
|
|
17
17
|
* - every failure is a `CompositionApiError` with the API's own code, so the host can tell an ended session
|
|
18
18
|
* (`EDITOR_SESSION_EXPIRED`...) from a missing composition (`NOT_FOUND`) from a widget that was never
|
|
19
19
|
* published (`WIDGET_NOT_PUBLISHED`);
|
|
20
|
-
* - a request that gets no answer does not hang the editor: it fails with `TIMEOUT` after a set time
|
|
20
|
+
* - a request that gets no answer does not hang the editor: it fails with `TIMEOUT` after a set time;
|
|
21
|
+
* - the documents of an AI Answers widget (`/compositions/{id}/adapters/ai-answers/knowledge`) are listed, added (as typed
|
|
22
|
+
* text, or as a file sent as a multipart form) and removed through the same four rules above. Adding one waits for the API
|
|
23
|
+
* to read and embed the document, which takes longer than any other call, so it has a limit of its own.
|
|
21
24
|
*/
|
|
22
25
|
import { unwrapApiData } from './compositionHelpers.js';
|
|
26
|
+
import { KNOWLEDGE_ADD_TIMEOUT_MS, readKnowledgeDocument, readKnowledgeListing } from './knowledgeDocuments.js';
|
|
23
27
|
export const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
24
28
|
/** What the API accepts as an idempotency key. */
|
|
25
29
|
const IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9_-]{8,50}$/;
|
|
@@ -205,18 +209,22 @@ export function createCompositionApi(options) {
|
|
|
205
209
|
const fetchImplementation = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
206
210
|
const controller = new AbortController();
|
|
207
211
|
let timedOut = false;
|
|
212
|
+
const limitMs = extras.timeoutMs ?? requestTimeoutMs;
|
|
208
213
|
const timer = setTimeout(() => {
|
|
209
214
|
timedOut = true;
|
|
210
215
|
controller.abort();
|
|
211
|
-
},
|
|
216
|
+
}, limitMs);
|
|
212
217
|
const forwardAbort = () => controller.abort();
|
|
213
218
|
extras.signal?.addEventListener('abort', forwardAbort, { once: true });
|
|
214
219
|
const headers = {
|
|
215
220
|
Accept: 'application/json',
|
|
216
221
|
Authorization: `Bearer ${token}`
|
|
217
222
|
};
|
|
218
|
-
|
|
223
|
+
// A form carries its own `Content-Type`, boundary included, which `fetch` writes when it is given one: setting it here
|
|
224
|
+
// would leave the API a form it cannot split into its parts.
|
|
225
|
+
if (extras.form === undefined && extras.body !== undefined) {
|
|
219
226
|
headers['Content-Type'] = 'application/json';
|
|
227
|
+
}
|
|
220
228
|
if (extras.idempotencyKey)
|
|
221
229
|
headers['Idempotency-Key'] = extras.idempotencyKey;
|
|
222
230
|
if (extras.ifMatch)
|
|
@@ -225,7 +233,7 @@ export function createCompositionApi(options) {
|
|
|
225
233
|
const response = await fetchImplementation(url, {
|
|
226
234
|
method,
|
|
227
235
|
headers,
|
|
228
|
-
body: extras.body !== undefined ? JSON.stringify(extras.body) : undefined,
|
|
236
|
+
body: extras.form ?? (extras.body !== undefined ? JSON.stringify(extras.body) : undefined),
|
|
229
237
|
// The token is the credential; the page's cookies for the API's domain play no part.
|
|
230
238
|
credentials: 'omit',
|
|
231
239
|
// What a composition holds changes with every save; never serve it from a cache.
|
|
@@ -239,7 +247,7 @@ export function createCompositionApi(options) {
|
|
|
239
247
|
}
|
|
240
248
|
catch (error) {
|
|
241
249
|
if (timedOut) {
|
|
242
|
-
throw new CompositionApiError(`The API did not answer within ${describeWait(
|
|
250
|
+
throw new CompositionApiError(`The API did not answer within ${describeWait(limitMs)}.`, {
|
|
243
251
|
status: 0,
|
|
244
252
|
code: 'TIMEOUT'
|
|
245
253
|
});
|
|
@@ -334,6 +342,17 @@ export function createCompositionApi(options) {
|
|
|
334
342
|
}
|
|
335
343
|
return sessionId;
|
|
336
344
|
}
|
|
345
|
+
/** The address of the documents of a composition's AI Answers widget, with `suffix` after it. */
|
|
346
|
+
function knowledgePath(compositionId, suffix = '') {
|
|
347
|
+
return compositionPath(compositionId, `/adapters/ai-answers/knowledge${suffix}`);
|
|
348
|
+
}
|
|
349
|
+
/** The id of a document, which goes into an address; a missing one is a bug of the caller, never sent. */
|
|
350
|
+
function checkedDocumentId(documentId) {
|
|
351
|
+
if (typeof documentId !== 'string' || documentId.trim() === '') {
|
|
352
|
+
throw new TypeError('A document id is needed');
|
|
353
|
+
}
|
|
354
|
+
return documentId;
|
|
355
|
+
}
|
|
337
356
|
return {
|
|
338
357
|
async getComposition(compositionId, requestOptions = {}) {
|
|
339
358
|
const record = await request('GET', compositionPath(compositionId), requestOptions);
|
|
@@ -409,6 +428,46 @@ export function createCompositionApi(options) {
|
|
|
409
428
|
},
|
|
410
429
|
async releaseEditLease(compositionId, sessionId, releaseOptions = {}) {
|
|
411
430
|
await request('DELETE', leasePath(compositionId, `?sessionId=${encodeURIComponent(checkedSessionId(sessionId))}`), { signal: releaseOptions.signal, keepalive: releaseOptions.keepalive });
|
|
431
|
+
},
|
|
432
|
+
async listKnowledgeDocuments(compositionId, requestOptions = {}) {
|
|
433
|
+
const listing = readKnowledgeListing(await request('GET', knowledgePath(compositionId), requestOptions));
|
|
434
|
+
if (!listing)
|
|
435
|
+
throw invalidResponse('The API gave no list of documents.');
|
|
436
|
+
return listing;
|
|
437
|
+
},
|
|
438
|
+
async addKnowledgeText(compositionId, input, requestOptions = {}) {
|
|
439
|
+
const document = readKnowledgeDocument(await request('POST', knowledgePath(compositionId, '/documents'), {
|
|
440
|
+
body: { title: input.title, text: input.text },
|
|
441
|
+
signal: requestOptions.signal,
|
|
442
|
+
timeoutMs: KNOWLEDGE_ADD_TIMEOUT_MS
|
|
443
|
+
}));
|
|
444
|
+
if (!document)
|
|
445
|
+
throw invalidResponse('The API gave no document.');
|
|
446
|
+
return document;
|
|
447
|
+
},
|
|
448
|
+
async addKnowledgeFile(compositionId, input, requestOptions = {}) {
|
|
449
|
+
if (typeof Blob === 'undefined' || !(input?.file instanceof Blob)) {
|
|
450
|
+
throw new TypeError('A file is needed');
|
|
451
|
+
}
|
|
452
|
+
const form = new FormData();
|
|
453
|
+
if (typeof input.title === 'string' && input.title.trim() !== '') {
|
|
454
|
+
form.append('title', input.title);
|
|
455
|
+
}
|
|
456
|
+
// A `File` carries its own name; a plain `Blob` has none, so the caller names it (or it is just "document").
|
|
457
|
+
const ownName = input.file.name;
|
|
458
|
+
const fileName = input.fileName || (typeof ownName === 'string' ? ownName : '') || 'document';
|
|
459
|
+
form.append('file', input.file, fileName);
|
|
460
|
+
const document = readKnowledgeDocument(await request('POST', knowledgePath(compositionId, '/documents/file'), {
|
|
461
|
+
form,
|
|
462
|
+
signal: requestOptions.signal,
|
|
463
|
+
timeoutMs: KNOWLEDGE_ADD_TIMEOUT_MS
|
|
464
|
+
}));
|
|
465
|
+
if (!document)
|
|
466
|
+
throw invalidResponse('The API gave no document.');
|
|
467
|
+
return document;
|
|
468
|
+
},
|
|
469
|
+
async removeKnowledgeDocument(compositionId, documentId, requestOptions = {}) {
|
|
470
|
+
await request('DELETE', knowledgePath(compositionId, `/documents/${encodeURIComponent(checkedDocumentId(documentId))}`), requestOptions);
|
|
412
471
|
}
|
|
413
472
|
};
|
|
414
473
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The documents of the AI Answers adapter: what an owner gives a widget's AI to write its answers from.
|
|
3
|
+
*
|
|
4
|
+
* This file holds what is true of them whatever draws them: the shape the API answers with (and a reader that checks it,
|
|
5
|
+
* because an answer the editor cannot read must not break the settings panel), the few limits the editor itself knows,
|
|
6
|
+
* and `createKnowledgeDocumentsClient`, which binds the four calls to ONE composition so a control that is given the client
|
|
7
|
+
* never needs to know which composition it is in, or how a request is made.
|
|
8
|
+
*
|
|
9
|
+
* It imports only types from `compositionApi.ts`, which in turn reads its answers with the readers of this file: nothing
|
|
10
|
+
* of `compositionApi.ts` is loaded when this file is, so the two do not wait for each other.
|
|
11
|
+
*/
|
|
12
|
+
import type { CompositionApi, CompositionRequestOptions } from './compositionApi.js';
|
|
13
|
+
/** A document is `processing` while its text is being read and turned into numbers, and `ready` once the AI can use it. */
|
|
14
|
+
export type KnowledgeDocumentStatus = 'processing' | 'ready';
|
|
15
|
+
/** One document of a widget's knowledge base, as the API lists it. The text itself is never sent back. */
|
|
16
|
+
export interface KnowledgeDocument {
|
|
17
|
+
id: string;
|
|
18
|
+
/** What the owner called it (a file's own name when none was given). Text typed by a person: shown as written. */
|
|
19
|
+
title: string;
|
|
20
|
+
status: KnowledgeDocumentStatus;
|
|
21
|
+
/** How many characters of text the document holds. */
|
|
22
|
+
charCount: number;
|
|
23
|
+
/** How many passages the text was cut into. */
|
|
24
|
+
chunkCount: number;
|
|
25
|
+
/** When it was added (an ISO date). */
|
|
26
|
+
createdAt: string;
|
|
27
|
+
/** When it became ready, or `null` while it is not. */
|
|
28
|
+
readyAt: string | null;
|
|
29
|
+
}
|
|
30
|
+
/** How much of the knowledge base is used, and how much it can hold. A limit is `null` when the API did not say. */
|
|
31
|
+
export interface KnowledgeTotals {
|
|
32
|
+
documents: number;
|
|
33
|
+
characters: number;
|
|
34
|
+
maxDocuments: number | null;
|
|
35
|
+
maxTotalChars: number | null;
|
|
36
|
+
}
|
|
37
|
+
/** Everything the API says about a widget's knowledge base. */
|
|
38
|
+
export interface KnowledgeListing {
|
|
39
|
+
documents: KnowledgeDocument[];
|
|
40
|
+
totals: KnowledgeTotals;
|
|
41
|
+
}
|
|
42
|
+
/** What goes into a document typed or pasted as text. */
|
|
43
|
+
export interface KnowledgeTextInput {
|
|
44
|
+
title: string;
|
|
45
|
+
text: string;
|
|
46
|
+
}
|
|
47
|
+
/** What goes into a document made from a file. Without a `title` the API uses the file's name. */
|
|
48
|
+
export interface KnowledgeFileInput {
|
|
49
|
+
file: Blob;
|
|
50
|
+
/** The name the file is sent under. A `File` has its own; a plain `Blob` needs one. */
|
|
51
|
+
fileName?: string;
|
|
52
|
+
title?: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The four calls of a widget's knowledge base, for one composition. Each one fails with a `CompositionApiError` whose `code`
|
|
56
|
+
* is the API's own (`KNOWLEDGE_DUPLICATE_DOCUMENT`, `KNOWLEDGE_FILE_TOO_LARGE`...), or one of the client's.
|
|
57
|
+
*/
|
|
58
|
+
export interface KnowledgeDocumentsClient {
|
|
59
|
+
list(options?: CompositionRequestOptions): Promise<KnowledgeListing>;
|
|
60
|
+
addText(input: KnowledgeTextInput, options?: CompositionRequestOptions): Promise<KnowledgeDocument>;
|
|
61
|
+
addFile(input: KnowledgeFileInput, options?: CompositionRequestOptions): Promise<KnowledgeDocument>;
|
|
62
|
+
remove(documentId: string, options?: CompositionRequestOptions): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/** The longest title the API takes, in characters. A hint for the box a title is typed in; the API holds the limit. */
|
|
65
|
+
export declare const KNOWLEDGE_TITLE_MAX_LENGTH = 120;
|
|
66
|
+
/**
|
|
67
|
+
* What the file chooser offers: a text file (plain, Markdown or CSV) or a picture of a page. Only a hint to the person
|
|
68
|
+
* choosing: what a file IS comes from its bytes, on the API's side, and the API says so when it is not one of these.
|
|
69
|
+
*
|
|
70
|
+
* A PDF is NOT offered: the API does not read PDFs yet (28 Oct 2026; its `PDF_READING_ENABLED` is off, because reading one in
|
|
71
|
+
* a process of its own can take more memory than the instance has). A PDF that is dropped on the panel or chosen anyway is
|
|
72
|
+
* refused by the API (`KNOWLEDGE_FILE_TYPE_UNSUPPORTED`) and the person is told so in words (`fileTypeUnsupported`). When the
|
|
73
|
+
* API reads PDFs, `.pdf` and `application/pdf` come back to this list in the release that goes with it.
|
|
74
|
+
*/
|
|
75
|
+
export declare const KNOWLEDGE_FILE_ACCEPT: string;
|
|
76
|
+
/**
|
|
77
|
+
* How long a document may take to add before the editor gives up, in milliseconds. The API ends an add by itself after 90
|
|
78
|
+
* seconds (a long document is read, cut up and embedded in that time); the editor waits a little longer so that the API's own
|
|
79
|
+
* answer, not the editor's timeout, tells the person what happened.
|
|
80
|
+
*/
|
|
81
|
+
export declare const KNOWLEDGE_ADD_TIMEOUT_MS = 120000;
|
|
82
|
+
/** One document as the API wrote it, or `null` when what came is no document (no id, no title). */
|
|
83
|
+
export declare function readKnowledgeDocument(value: unknown): KnowledgeDocument | null;
|
|
84
|
+
/**
|
|
85
|
+
* What the API listed, or `null` when it is no list. A document that cannot be read is left out rather than failing the list:
|
|
86
|
+
* the rest are still the owner's, and still usable. When the API gives no totals they are worked out from the documents.
|
|
87
|
+
*/
|
|
88
|
+
export declare function readKnowledgeListing(value: unknown): KnowledgeListing | null;
|
|
89
|
+
/** Binds the four calls to one composition. */
|
|
90
|
+
export declare function createKnowledgeDocumentsClient(api: Pick<CompositionApi, 'listKnowledgeDocuments' | 'addKnowledgeText' | 'addKnowledgeFile' | 'removeKnowledgeDocument'>, compositionId: string): KnowledgeDocumentsClient;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** The longest title the API takes, in characters. A hint for the box a title is typed in; the API holds the limit. */
|
|
2
|
+
export const KNOWLEDGE_TITLE_MAX_LENGTH = 120;
|
|
3
|
+
/**
|
|
4
|
+
* What the file chooser offers: a text file (plain, Markdown or CSV) or a picture of a page. Only a hint to the person
|
|
5
|
+
* choosing: what a file IS comes from its bytes, on the API's side, and the API says so when it is not one of these.
|
|
6
|
+
*
|
|
7
|
+
* A PDF is NOT offered: the API does not read PDFs yet (28 Oct 2026; its `PDF_READING_ENABLED` is off, because reading one in
|
|
8
|
+
* a process of its own can take more memory than the instance has). A PDF that is dropped on the panel or chosen anyway is
|
|
9
|
+
* refused by the API (`KNOWLEDGE_FILE_TYPE_UNSUPPORTED`) and the person is told so in words (`fileTypeUnsupported`). When the
|
|
10
|
+
* API reads PDFs, `.pdf` and `application/pdf` come back to this list in the release that goes with it.
|
|
11
|
+
*/
|
|
12
|
+
export const KNOWLEDGE_FILE_ACCEPT = [
|
|
13
|
+
'.txt',
|
|
14
|
+
'.text',
|
|
15
|
+
'.md',
|
|
16
|
+
'.markdown',
|
|
17
|
+
'.csv',
|
|
18
|
+
'.png',
|
|
19
|
+
'.jpg',
|
|
20
|
+
'.jpeg',
|
|
21
|
+
'.webp',
|
|
22
|
+
'text/plain',
|
|
23
|
+
'text/markdown',
|
|
24
|
+
'text/csv',
|
|
25
|
+
'image/png',
|
|
26
|
+
'image/jpeg',
|
|
27
|
+
'image/webp'
|
|
28
|
+
].join(',');
|
|
29
|
+
/**
|
|
30
|
+
* How long a document may take to add before the editor gives up, in milliseconds. The API ends an add by itself after 90
|
|
31
|
+
* seconds (a long document is read, cut up and embedded in that time); the editor waits a little longer so that the API's own
|
|
32
|
+
* answer, not the editor's timeout, tells the person what happened.
|
|
33
|
+
*/
|
|
34
|
+
export const KNOWLEDGE_ADD_TIMEOUT_MS = 120_000;
|
|
35
|
+
function isRecord(value) {
|
|
36
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
37
|
+
}
|
|
38
|
+
/** A count worth believing: a finite number of zero or more. Anything else is `null`. */
|
|
39
|
+
function readCount(value) {
|
|
40
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.floor(value) : null;
|
|
41
|
+
}
|
|
42
|
+
/** One document as the API wrote it, or `null` when what came is no document (no id, no title). */
|
|
43
|
+
export function readKnowledgeDocument(value) {
|
|
44
|
+
if (!isRecord(value))
|
|
45
|
+
return null;
|
|
46
|
+
if (typeof value.id !== 'string' || value.id === '' || typeof value.title !== 'string')
|
|
47
|
+
return null;
|
|
48
|
+
return {
|
|
49
|
+
id: value.id,
|
|
50
|
+
title: value.title,
|
|
51
|
+
// Any status but `ready` is a document the AI cannot use yet, so it is shown as still being read.
|
|
52
|
+
status: value.status === 'ready' ? 'ready' : 'processing',
|
|
53
|
+
charCount: readCount(value.charCount) ?? 0,
|
|
54
|
+
chunkCount: readCount(value.chunkCount) ?? 0,
|
|
55
|
+
createdAt: typeof value.createdAt === 'string' ? value.createdAt : '',
|
|
56
|
+
readyAt: typeof value.readyAt === 'string' ? value.readyAt : null
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* What the API listed, or `null` when it is no list. A document that cannot be read is left out rather than failing the list:
|
|
61
|
+
* the rest are still the owner's, and still usable. When the API gives no totals they are worked out from the documents.
|
|
62
|
+
*/
|
|
63
|
+
export function readKnowledgeListing(value) {
|
|
64
|
+
if (!isRecord(value) || !Array.isArray(value.documents))
|
|
65
|
+
return null;
|
|
66
|
+
const documents = [];
|
|
67
|
+
for (const entry of value.documents) {
|
|
68
|
+
const document = readKnowledgeDocument(entry);
|
|
69
|
+
if (document)
|
|
70
|
+
documents.push(document);
|
|
71
|
+
else
|
|
72
|
+
console.warn('[knowledgeDocuments] A document the API listed could not be read and was left out');
|
|
73
|
+
}
|
|
74
|
+
const totals = isRecord(value.totals) ? value.totals : {};
|
|
75
|
+
return {
|
|
76
|
+
documents,
|
|
77
|
+
totals: {
|
|
78
|
+
documents: readCount(totals.documents) ?? documents.length,
|
|
79
|
+
characters: readCount(totals.characters) ??
|
|
80
|
+
documents.reduce((sum, document) => sum + document.charCount, 0),
|
|
81
|
+
maxDocuments: readCount(totals.maxDocuments),
|
|
82
|
+
maxTotalChars: readCount(totals.maxTotalChars)
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Binds the four calls to one composition. */
|
|
87
|
+
export function createKnowledgeDocumentsClient(api, compositionId) {
|
|
88
|
+
return {
|
|
89
|
+
list: (options) => api.listKnowledgeDocuments(compositionId, options),
|
|
90
|
+
addText: (input, options) => api.addKnowledgeText(compositionId, input, options),
|
|
91
|
+
addFile: (input, options) => api.addKnowledgeFile(compositionId, input, options),
|
|
92
|
+
remove: (documentId, options) => api.removeKnowledgeDocument(compositionId, documentId, options)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What happens in the Documents panel of AI Answers, without anything to look at: loading the list, adding files and text,
|
|
3
|
+
* taking a document out, and telling the person what went wrong in words they can act on.
|
|
4
|
+
*
|
|
5
|
+
* The panel (`KnowledgeDocumentsInputController.svelte`) only draws this state and forwards what the person presses. The logic
|
|
6
|
+
* is here so that it can be tested in Node, with a fake client and a fake clock, where a component cannot be pressed:
|
|
7
|
+
*
|
|
8
|
+
* - the list is loaded once when the panel starts, and again on "try again". A document that is still being read (another
|
|
9
|
+
* tab of the same owner added it a moment ago) is looked at again every few seconds, a limited number of times;
|
|
10
|
+
* - files are sent one after the other, never together: each one is read and embedded on the API's side, and the API allows
|
|
11
|
+
* a few adds in a few minutes, so sending five at once would only make four of them wait or fail. One that fails is
|
|
12
|
+
* reported by name and the next one is tried, unless the failure would be the same for all of them (the session ended,
|
|
13
|
+
* the connection is down, the knowledge base is full, too many adds): then the rest are not sent and the person is told;
|
|
14
|
+
* - removing a document takes two presses, so a slip of the hand does not lose a document that took a minute to add. The
|
|
15
|
+
* first press asks, the second one removes; leaving it alone for a few seconds takes the question back;
|
|
16
|
+
* - whatever is in flight is cancelled when the panel goes away, and its answer is ignored.
|
|
17
|
+
*
|
|
18
|
+
* The state it keeps is a Svelte store (`subscribe` calls the listener at once with the state, then at every change), so the
|
|
19
|
+
* panel can read it as `$panel`.
|
|
20
|
+
*/
|
|
21
|
+
import type { EditorMessageKey } from '../i18n/messages.js';
|
|
22
|
+
import { type EditorFailure } from './compositionEditorController.js';
|
|
23
|
+
import { type KnowledgeDocument, type KnowledgeDocumentsClient, type KnowledgeTotals } from './knowledgeDocuments.js';
|
|
24
|
+
/**
|
|
25
|
+
* A sentence for the person, as the catalog's key and the values that go into it, so the panel says it in the language of
|
|
26
|
+
* the page. `verbatim` is what the API itself said, kept for a failure the editor has no sentence of its own for.
|
|
27
|
+
*/
|
|
28
|
+
export interface PanelMessage {
|
|
29
|
+
messageKey: EditorMessageKey;
|
|
30
|
+
messageValues?: Record<string, string | number>;
|
|
31
|
+
verbatim?: string;
|
|
32
|
+
}
|
|
33
|
+
/** A problem to show below the list, next to the buttons that were pressed, until the person dismisses it or starts something else. */
|
|
34
|
+
export interface PanelNotice {
|
|
35
|
+
/** Tells one notice from another, so a list of them can be drawn by key. */
|
|
36
|
+
id: number;
|
|
37
|
+
message: PanelMessage;
|
|
38
|
+
}
|
|
39
|
+
/** `unavailable`: no client (the control is not inside a composition editor). */
|
|
40
|
+
export type KnowledgePanelPhase = 'unavailable' | 'loading' | 'ready' | 'failed';
|
|
41
|
+
/** What is being sent right now: the name of the file (or the title of the text), and where it is in the batch. */
|
|
42
|
+
export interface PanelSending {
|
|
43
|
+
name: string;
|
|
44
|
+
/** 1-based place of this one in the batch. */
|
|
45
|
+
position: number;
|
|
46
|
+
count: number;
|
|
47
|
+
}
|
|
48
|
+
export interface KnowledgePanelState {
|
|
49
|
+
phase: KnowledgePanelPhase;
|
|
50
|
+
/** The documents, oldest first. */
|
|
51
|
+
documents: readonly KnowledgeDocument[];
|
|
52
|
+
/** How much of the knowledge base is used, or `null` before the first list. */
|
|
53
|
+
totals: KnowledgeTotals | null;
|
|
54
|
+
/** Why the list could not be loaded (`phase` is `failed`). */
|
|
55
|
+
loadFailure: PanelMessage | null;
|
|
56
|
+
sending: PanelSending | null;
|
|
57
|
+
/** The ids of the documents being removed. */
|
|
58
|
+
removing: readonly string[];
|
|
59
|
+
/** The document whose removal was asked once: pressing remove again takes it out. */
|
|
60
|
+
confirmingRemoval: string | null;
|
|
61
|
+
notices: readonly PanelNotice[];
|
|
62
|
+
}
|
|
63
|
+
export interface PanelTimers {
|
|
64
|
+
set(callback: () => void, delayMs: number): unknown;
|
|
65
|
+
clear(handle: unknown): void;
|
|
66
|
+
}
|
|
67
|
+
export interface KnowledgePanelOptions {
|
|
68
|
+
/** The four calls for this composition; `undefined` when the control is used outside a composition editor. */
|
|
69
|
+
client: KnowledgeDocumentsClient | undefined;
|
|
70
|
+
/** How long to wait before looking again at a list that has a document still being read. Default: 4 seconds. */
|
|
71
|
+
pollDelayMs?: number;
|
|
72
|
+
/** How many times to look again before giving up on a document that never becomes ready. Default: 15 (a minute). */
|
|
73
|
+
maxPolls?: number;
|
|
74
|
+
/** How long a first press on "remove" waits for the second one. Default: 6 seconds. */
|
|
75
|
+
removalConfirmationMs?: number;
|
|
76
|
+
/** The clock to wait with. Default: the page's `setTimeout`. Tests give one they can wind forward. */
|
|
77
|
+
timers?: PanelTimers;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The sentence for a failure of a call about the knowledge base. `about` names what was being added (a file's name, a
|
|
81
|
+
* document's title), for the sentences that say which one it was. The API's own sentence is kept only when the editor has
|
|
82
|
+
* none of its own for the code: those are written for the owner and say what to do, but only in English.
|
|
83
|
+
*/
|
|
84
|
+
export declare function describeKnowledgeFailure(failure: EditorFailure, about?: {
|
|
85
|
+
name?: string;
|
|
86
|
+
}): PanelMessage;
|
|
87
|
+
export declare class KnowledgeDocumentsPanel {
|
|
88
|
+
private readonly client;
|
|
89
|
+
private readonly pollDelayMs;
|
|
90
|
+
private readonly maxPolls;
|
|
91
|
+
private readonly removalConfirmationMs;
|
|
92
|
+
private readonly timers;
|
|
93
|
+
private readonly listeners;
|
|
94
|
+
/** Cancels every request that is on its way when the panel goes away. */
|
|
95
|
+
private readonly cancellation;
|
|
96
|
+
private state;
|
|
97
|
+
private started;
|
|
98
|
+
private destroyed;
|
|
99
|
+
private loading;
|
|
100
|
+
private pollsSoFar;
|
|
101
|
+
private latestRefresh;
|
|
102
|
+
private pollHandle;
|
|
103
|
+
private confirmationHandle;
|
|
104
|
+
private nextNoticeId;
|
|
105
|
+
constructor(options: KnowledgePanelOptions);
|
|
106
|
+
getState(): KnowledgePanelState;
|
|
107
|
+
/** The store contract: the listener hears the state now and at every change; the returned function stops it. */
|
|
108
|
+
subscribe(listener: (state: KnowledgePanelState) => void): () => void;
|
|
109
|
+
private setState;
|
|
110
|
+
/** Loads the list. Called once, when the panel is on the screen; calling it again does nothing. */
|
|
111
|
+
start(): Promise<void>;
|
|
112
|
+
/** Loads the list again after it failed ("try again"). */
|
|
113
|
+
retry(): Promise<void>;
|
|
114
|
+
private load;
|
|
115
|
+
private loadList;
|
|
116
|
+
/**
|
|
117
|
+
* Looks at the list again, without any sign of it on the screen, to bring the totals up to date after a change, or to see
|
|
118
|
+
* a document become ready. A failure is only logged: the list that is on the screen is still the best the panel has.
|
|
119
|
+
*/
|
|
120
|
+
private refresh;
|
|
121
|
+
/** While a document is being read, look again every few seconds (a limited number of times), so it turns ready on its own. */
|
|
122
|
+
private watchDocumentsBeingRead;
|
|
123
|
+
private stopWatching;
|
|
124
|
+
/** True while the panel is busy with something that must end before the next thing starts. */
|
|
125
|
+
private isBusy;
|
|
126
|
+
/** Sends the files one after the other (see the top of this file). Resolves when the last one is done. */
|
|
127
|
+
addFiles(files: readonly File[]): Promise<void>;
|
|
128
|
+
/**
|
|
129
|
+
* Adds a document made of text the person typed or pasted. Resolves `true` when it was added, so the form can close, and
|
|
130
|
+
* `false` when it was not (the reason is in `notices`) or the panel was busy.
|
|
131
|
+
*/
|
|
132
|
+
addText(input: {
|
|
133
|
+
title: string;
|
|
134
|
+
text: string;
|
|
135
|
+
}): Promise<boolean>;
|
|
136
|
+
/** Ends a batch: the problems become notices (a few, and a line for the rest), and nothing is being sent any more. */
|
|
137
|
+
private finishSending;
|
|
138
|
+
private noticeFor;
|
|
139
|
+
/** Takes the problems off the screen. */
|
|
140
|
+
dismissNotices(): void;
|
|
141
|
+
/** The first press on "remove": asks the person to press it again. */
|
|
142
|
+
askToRemove(documentId: string): void;
|
|
143
|
+
/** Takes the question back (the person chose to keep the document, or went on to something else). */
|
|
144
|
+
cancelRemoval(): void;
|
|
145
|
+
private clearConfirmationTimer;
|
|
146
|
+
/** The second press: takes the document out. Does nothing unless the first press came for this very document. */
|
|
147
|
+
confirmRemoval(documentId: string): Promise<void>;
|
|
148
|
+
/** Takes a document off the list and its share off the totals, so the screen is right before the API is asked again. */
|
|
149
|
+
private dropDocument;
|
|
150
|
+
/** Cancels what is on its way and stops every timer. The panel says nothing more after this. */
|
|
151
|
+
destroy(): void;
|
|
152
|
+
}
|