@widgetic/creator 0.3.81 → 0.4.1
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 +157 -0
- package/dist/CreatorApp.svelte +1593 -812
- package/dist/CreatorApp.svelte.d.ts +22 -0
- package/dist/components/BrandKitPanel.svelte +48 -40
- package/dist/components/BrandKitPanel.svelte.d.ts +1 -1
- package/dist/components/DesignQuickEdit.svelte +13 -4
- package/dist/components/EditableName.svelte +13 -8
- package/dist/components/EditableName.svelte.d.ts +2 -2
- package/dist/components/FileUrlSourceControl.svelte +21 -8
- package/dist/components/HoverTooltip.svelte +1 -1
- package/dist/components/SelectorDropdown.svelte +19 -15
- package/dist/components/SelectorDropdown.svelte.d.ts +2 -2
- package/dist/components/VersionDropdownRow.svelte +14 -11
- package/dist/components/WidgetDetails.svelte +118 -92
- package/dist/components/WidgetShapeLivePreviews.svelte +105 -46
- package/dist/creator-types.d.ts +96 -0
- package/dist/host/creatorHost.d.ts +176 -0
- package/dist/host/creatorHost.js +242 -0
- package/dist/host/limitRefusal.d.ts +64 -0
- package/dist/host/limitRefusal.js +109 -0
- package/dist/i18n/index.d.ts +5 -0
- package/dist/i18n/index.js +5 -0
- package/dist/i18n/labels.d.ts +14 -0
- package/dist/i18n/labels.js +39 -0
- package/dist/i18n/language.d.ts +559 -0
- package/dist/i18n/language.js +47 -0
- package/dist/i18n/languageKit.d.ts +52 -0
- package/dist/i18n/languageKit.js +45 -0
- package/dist/i18n/messages.d.ts +526 -0
- package/dist/i18n/messages.js +548 -0
- package/dist/i18n/slots.d.ts +22 -0
- package/dist/i18n/slots.js +42 -0
- package/dist/i18n/statusText.d.ts +22 -0
- package/dist/i18n/statusText.js +223 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/stores/userSession.d.ts +8 -1
- package/dist/stores/userSession.js +18 -25
- package/dist/stores/websocketStore.d.ts +6 -0
- package/dist/stores/websocketStore.js +39 -4
- package/dist/utils/designIntentTelemetry.d.ts +8 -0
- package/dist/utils/designIntentTelemetry.js +52 -34
- package/dist/utils/directionPlacement.d.ts +51 -0
- package/dist/utils/directionPlacement.js +55 -0
- package/dist/utils/embedCode.d.ts +0 -1
- package/dist/utils/embedCode.js +0 -4
- package/dist/utils/generationSharing.d.ts +122 -0
- package/dist/utils/generationSharing.js +188 -0
- package/dist/utils/nativeScrollbarPadding.js +1 -1
- package/dist/utils/pickedFile.d.ts +15 -0
- package/dist/utils/pickedFile.js +21 -0
- package/dist/utils/widgeticChatUpload.d.ts +9 -0
- package/dist/utils/widgeticChatUpload.js +24 -2
- package/package.json +5 -3
- package/dist/retiredSurface.test.d.ts +0 -1
- package/dist/retiredSurface.test.js +0 -113
- package/dist/utils/designIntentResolver.test.d.ts +0 -1
- package/dist/utils/designIntentResolver.test.js +0 -246
- package/dist/utils/designIntentTelemetry.test.d.ts +0 -1
- package/dist/utils/designIntentTelemetry.test.js +0 -385
- package/dist/utils/designQuickEditValues.test.d.ts +0 -1
- package/dist/utils/designQuickEditValues.test.js +0 -100
- package/dist/utils/widgeticChatUpload.test.d.ts +0 -1
- package/dist/utils/widgeticChatUpload.test.js +0 -257
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { TextDirection } from '@widgetic/design-system/components/textDirection';
|
|
2
|
+
/**
|
|
3
|
+
* Where things are put and how far they are dragged, on a screen that follows the direction of its language.
|
|
4
|
+
*
|
|
5
|
+
* The creator draws its panels with the start and the end of a line (so a right to left screen mirrors them by itself), but
|
|
6
|
+
* a distance measured on the screen, a pointer moving along it or a list put under a button, is measured from the left
|
|
7
|
+
* edge of the window. These helpers say what that means at the start and at the end of the line.
|
|
8
|
+
*/
|
|
9
|
+
/** The two edges of a box that are measured on the screen: how far its left and its right edge are from the left edge of the window. */
|
|
10
|
+
export interface HorizontalEdges {
|
|
11
|
+
left: number;
|
|
12
|
+
right: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* How far a pointer has moved toward the END of the line since it was pressed, in pixels: to the right on a left to right
|
|
16
|
+
* screen, to the left on a right to left one. A divider between two columns that follow the direction of the line is
|
|
17
|
+
* dragged by this, because the first column is the one at the start of the line, and the end of a right to left line is the
|
|
18
|
+
* left of the screen.
|
|
19
|
+
*/
|
|
20
|
+
export declare function distanceTowardEnd(direction: TextDirection, startX: number, currentX: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* The style that puts a list (a fixed box) under a cluster of controls, lined up with the END edge of the cluster and
|
|
23
|
+
* sticking out past it by `overhang` pixels: the right edge on a left to right screen, the left edge on a right to left one,
|
|
24
|
+
* where the cluster is at the other end of its row and the list grows toward the middle of the panel. `windowWidth` is the
|
|
25
|
+
* width the right edge is measured from.
|
|
26
|
+
*/
|
|
27
|
+
export declare function listAlignedToEnd(direction: TextDirection, cluster: HorizontalEdges, windowWidth: number, overhang: number): string;
|
|
28
|
+
/**
|
|
29
|
+
* How wide a name can be before it runs into the box that stands beyond it on the same line, in pixels. The name starts at
|
|
30
|
+
* its START edge and grows toward the END of the line, so the room is measured from the left edge of the name to the left
|
|
31
|
+
* edge of the neighbour on a left to right screen, and from the right edge of the name to the right edge of the neighbour on
|
|
32
|
+
* a right to left one, where the neighbour is on the left of the name. (Measured the left to right way it came out negative
|
|
33
|
+
* there, and the name was cut down to its smallest width: a title that showed as a dot.)
|
|
34
|
+
*/
|
|
35
|
+
export declare function roomBeforeNeighbour(direction: TextDirection, name: HorizontalEdges, neighbour: HorizontalEdges, gap: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* Which side of a shape a panel opens on, `true` for its right.
|
|
38
|
+
*
|
|
39
|
+
* On a left to right screen it opens on the side that has more room: on the right when the shape is left of the middle of the
|
|
40
|
+
* view, on the left otherwise (a shape exactly in the middle included, as it always was). A right to left screen is the mirror
|
|
41
|
+
* image of that: on the left when the shape is right of the middle, on the right otherwise. The panel with the properties of
|
|
42
|
+
* the shape is docked at the END of the line (the right of a left to right screen, the left of a right to left one), so a shape
|
|
43
|
+
* exactly in the middle is given its panel on the side away from the properties, in both directions.
|
|
44
|
+
*/
|
|
45
|
+
export declare function opensOnRightOfShape(direction: TextDirection, shapeCenterX: number, viewCenterX: number): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The left edge of a pop-up that hangs from a button, in pixels from the left edge of the window: its START edge lined up with
|
|
48
|
+
* the START edge of the button, so that it grows toward the end of the line the way the words in it do, and kept inside the
|
|
49
|
+
* window by `margin` pixels.
|
|
50
|
+
*/
|
|
51
|
+
export declare function popUpLeftFromAnchor(direction: TextDirection, anchor: HorizontalEdges, popUpWidth: number, windowWidth: number, margin: number): number;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How far a pointer has moved toward the END of the line since it was pressed, in pixels: to the right on a left to right
|
|
3
|
+
* screen, to the left on a right to left one. A divider between two columns that follow the direction of the line is
|
|
4
|
+
* dragged by this, because the first column is the one at the start of the line, and the end of a right to left line is the
|
|
5
|
+
* left of the screen.
|
|
6
|
+
*/
|
|
7
|
+
export function distanceTowardEnd(direction, startX, currentX) {
|
|
8
|
+
return direction === 'rtl' ? startX - currentX : currentX - startX;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The style that puts a list (a fixed box) under a cluster of controls, lined up with the END edge of the cluster and
|
|
12
|
+
* sticking out past it by `overhang` pixels: the right edge on a left to right screen, the left edge on a right to left one,
|
|
13
|
+
* where the cluster is at the other end of its row and the list grows toward the middle of the panel. `windowWidth` is the
|
|
14
|
+
* width the right edge is measured from.
|
|
15
|
+
*/
|
|
16
|
+
export function listAlignedToEnd(direction, cluster, windowWidth, overhang) {
|
|
17
|
+
if (direction === 'rtl') {
|
|
18
|
+
return `left: ${Math.round(cluster.left) - overhang}px;`; // rtl-ok: the end edge of a right to left cluster is its left edge
|
|
19
|
+
}
|
|
20
|
+
return `right: ${Math.round(windowWidth - cluster.right) - overhang}px;`; // rtl-ok: the end edge of a left to right cluster is its right edge
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* How wide a name can be before it runs into the box that stands beyond it on the same line, in pixels. The name starts at
|
|
24
|
+
* its START edge and grows toward the END of the line, so the room is measured from the left edge of the name to the left
|
|
25
|
+
* edge of the neighbour on a left to right screen, and from the right edge of the name to the right edge of the neighbour on
|
|
26
|
+
* a right to left one, where the neighbour is on the left of the name. (Measured the left to right way it came out negative
|
|
27
|
+
* there, and the name was cut down to its smallest width: a title that showed as a dot.)
|
|
28
|
+
*/
|
|
29
|
+
export function roomBeforeNeighbour(direction, name, neighbour, gap) {
|
|
30
|
+
const room = direction === 'rtl' ? name.right - neighbour.right : neighbour.left - name.left;
|
|
31
|
+
return Math.floor(room - gap);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Which side of a shape a panel opens on, `true` for its right.
|
|
35
|
+
*
|
|
36
|
+
* On a left to right screen it opens on the side that has more room: on the right when the shape is left of the middle of the
|
|
37
|
+
* view, on the left otherwise (a shape exactly in the middle included, as it always was). A right to left screen is the mirror
|
|
38
|
+
* image of that: on the left when the shape is right of the middle, on the right otherwise. The panel with the properties of
|
|
39
|
+
* the shape is docked at the END of the line (the right of a left to right screen, the left of a right to left one), so a shape
|
|
40
|
+
* exactly in the middle is given its panel on the side away from the properties, in both directions.
|
|
41
|
+
*/
|
|
42
|
+
export function opensOnRightOfShape(direction, shapeCenterX, viewCenterX) {
|
|
43
|
+
if (shapeCenterX === viewCenterX)
|
|
44
|
+
return direction === 'rtl';
|
|
45
|
+
return shapeCenterX < viewCenterX;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The left edge of a pop-up that hangs from a button, in pixels from the left edge of the window: its START edge lined up with
|
|
49
|
+
* the START edge of the button, so that it grows toward the end of the line the way the words in it do, and kept inside the
|
|
50
|
+
* window by `margin` pixels.
|
|
51
|
+
*/
|
|
52
|
+
export function popUpLeftFromAnchor(direction, anchor, popUpWidth, windowWidth, margin) {
|
|
53
|
+
const wanted = direction === 'rtl' ? anchor.right - popUpWidth : anchor.left;
|
|
54
|
+
return Math.max(margin, Math.min(wanted, windowWidth - popUpWidth - margin));
|
|
55
|
+
}
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export type EmbedFormat = 'iframe' | 'web-component';
|
|
8
8
|
export declare const EMBED_FORMATS: EmbedFormat[];
|
|
9
|
-
export declare const EMBED_FORMAT_LABELS: Record<EmbedFormat, string>;
|
|
10
9
|
export interface EmbedCodeParams {
|
|
11
10
|
/** Direct widget HTML URL on the CDN (or API proxy in dev). */
|
|
12
11
|
embedSrc: string;
|
package/dist/utils/embedCode.js
CHANGED
|
@@ -5,10 +5,6 @@
|
|
|
5
5
|
* web-component: embed-sdk.js + one-word `<widgetic composition-id>`.
|
|
6
6
|
*/
|
|
7
7
|
export const EMBED_FORMATS = ['iframe', 'web-component'];
|
|
8
|
-
export const EMBED_FORMAT_LABELS = {
|
|
9
|
-
iframe: 'iframe',
|
|
10
|
-
'web-component': 'Web Component'
|
|
11
|
-
};
|
|
12
8
|
export function generateIframeEmbedCode({ embedSrc, width, height }) {
|
|
13
9
|
return `<iframe src="${embedSrc}" width="${width}" height="${height}" frameborder="0" allowfullscreen></iframe>`;
|
|
14
10
|
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* generationSharing: how Stop reaches the server's job, and how the tabs of one browser share what a generation is doing.
|
|
3
|
+
*
|
|
4
|
+
* Two things the creator used to get wrong, both about a generation that runs on the server and outlives the click that
|
|
5
|
+
* started it:
|
|
6
|
+
*
|
|
7
|
+
* - Stop only walked away from the job. It stopped listening, and the server carried on writing (and billing) the whole
|
|
8
|
+
* run. `requestOperationStop` asks the server to stop it (`POST /operations/{id}/cancel`), which aborts the model call.
|
|
9
|
+
* - A second tab of the same customer never learned that the first was generating, so it offered Generate again and a
|
|
10
|
+
* widget could be generated twice at once. The tabs now tell each other which operation they are running
|
|
11
|
+
* (`openGenerationChannel`), and the one that hears it follows the operation by its status on the server, so what the
|
|
12
|
+
* server says is the state: no flag a closed tab left behind keeps a tab waiting.
|
|
13
|
+
*
|
|
14
|
+
* Why a channel and not only the browser's storage: in a partner's page the creator keeps what it remembers in memory (see
|
|
15
|
+
* `host/creatorHost.ts`), so two tabs share nothing there. A `BroadcastChannel` writes nothing anywhere: a message reaches
|
|
16
|
+
* the other tabs of the page's origin that are open right now and is gone. What travels is only a widget's id and an
|
|
17
|
+
* operation's id, and a tab that hears them checks with the server (with its own sign-in) whether the operation is one of
|
|
18
|
+
* its visitor's, so a message that reaches a tab of another visitor of the same browser changes nothing there.
|
|
19
|
+
*
|
|
20
|
+
* Kept free of Svelte and of the page so the rules are testable without a DOM.
|
|
21
|
+
*/
|
|
22
|
+
/** The name of the channel. Namespaced so it cannot be taken for a channel of the partner's own page. */
|
|
23
|
+
export declare const GENERATION_CHANNEL_NAME = "widgetic-creator:generation";
|
|
24
|
+
/** What one tab tells the others. */
|
|
25
|
+
export type GenerationNote =
|
|
26
|
+
/** "This tab is running operation `operationId` on widget `widgetId`." */
|
|
27
|
+
{
|
|
28
|
+
kind: 'started';
|
|
29
|
+
widgetId: string;
|
|
30
|
+
operationId: string;
|
|
31
|
+
}
|
|
32
|
+
/** "Is anyone generating on widget `widgetId`?" A tab that is answers with `started`. */
|
|
33
|
+
| {
|
|
34
|
+
kind: 'ask';
|
|
35
|
+
widgetId: string;
|
|
36
|
+
};
|
|
37
|
+
/** The note a message carries, or null when it is not one of ours (the channel is shared by the whole origin). */
|
|
38
|
+
export declare function readGenerationNote(data: unknown): GenerationNote | null;
|
|
39
|
+
/** What a tab does when it hears the others. */
|
|
40
|
+
export interface GenerationChannelHandlers {
|
|
41
|
+
/** Another tab runs `operationId` on `widgetId`. */
|
|
42
|
+
onStarted: (widgetId: string, operationId: string) => void;
|
|
43
|
+
/** Another tab wants to know whether this one is generating on `widgetId`. */
|
|
44
|
+
onAsk: (widgetId: string) => void;
|
|
45
|
+
}
|
|
46
|
+
/** The two things a tab says, and the end of listening. */
|
|
47
|
+
export interface GenerationChannel {
|
|
48
|
+
announceStarted: (widgetId: string, operationId: string) => void;
|
|
49
|
+
ask: (widgetId: string) => void;
|
|
50
|
+
close: () => void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Opens this tab's end of the channel. Returns null where the browser has no `BroadcastChannel` (the creator then works as
|
|
54
|
+
* before: the tabs do not hear each other, and the server's status is still what each tab follows once it knows the id).
|
|
55
|
+
* A handler that throws is logged and does not reach the channel.
|
|
56
|
+
*/
|
|
57
|
+
export declare function openGenerationChannel(handlers: GenerationChannelHandlers, channelFactory?: ((name: string) => BroadcastChannel) | null): GenerationChannel | null;
|
|
58
|
+
/** Whether an operation has ended, whichever way. */
|
|
59
|
+
export declare function isFinishedOperationStatus(status: unknown): boolean;
|
|
60
|
+
/** An operation as the creator reads it: the fields it uses, from the answer of the API or a frame of its stream. */
|
|
61
|
+
export interface OperationSnapshot {
|
|
62
|
+
id?: string;
|
|
63
|
+
type?: string;
|
|
64
|
+
status: string;
|
|
65
|
+
progress?: number;
|
|
66
|
+
error?: string;
|
|
67
|
+
metadata?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The operation inside an API answer. The API wraps what it answers in `{ success, data }`; a frame of the stream carries
|
|
71
|
+
* the operation itself. The generated client reads the first shape as an operation with every field empty, so the creator
|
|
72
|
+
* reads the raw body and takes the operation out of it here. Returns null for anything that is not an operation.
|
|
73
|
+
*/
|
|
74
|
+
export declare function readOperationBody(body: unknown): OperationSnapshot | null;
|
|
75
|
+
/** The HTTP status of a failed call of the generated client (`ResponseError.response.status`), when it has one. */
|
|
76
|
+
export declare function httpStatusOfFailure(error: unknown): number | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* Whether an operation is the code generation of this widget, finished or not (the operation names its widget once the
|
|
79
|
+
* generator has started, so an operation that does not name one yet is taken on the word of whoever pointed to it).
|
|
80
|
+
*/
|
|
81
|
+
export declare function isGenerationOfWidget(snapshot: OperationSnapshot | null, widgetId: string): boolean;
|
|
82
|
+
/** What asking the server to stop an operation came to. */
|
|
83
|
+
export type StopOutcome =
|
|
84
|
+
/** The server marked it cancelled: the job stops at its next checkpoint, and a run that had not begun to write is not charged. */
|
|
85
|
+
{
|
|
86
|
+
outcome: 'stopped';
|
|
87
|
+
}
|
|
88
|
+
/** It had ended before the stop arrived (finished, failed, or already cancelled): nothing was left to stop. */
|
|
89
|
+
| {
|
|
90
|
+
outcome: 'already-ended';
|
|
91
|
+
status: string;
|
|
92
|
+
}
|
|
93
|
+
/** The server does not know the operation (its record expired, or it is not this visitor's): nothing to stop from here. */
|
|
94
|
+
| {
|
|
95
|
+
outcome: 'gone';
|
|
96
|
+
}
|
|
97
|
+
/** The server could not be told. The job may still be running. */
|
|
98
|
+
| {
|
|
99
|
+
outcome: 'failed';
|
|
100
|
+
error: unknown;
|
|
101
|
+
};
|
|
102
|
+
/** The part of the generated client that `requestOperationStop` uses. */
|
|
103
|
+
export interface OperationStopClient {
|
|
104
|
+
operationsIdCancelPostRaw: (requestParameters: {
|
|
105
|
+
id: string;
|
|
106
|
+
}) => Promise<{
|
|
107
|
+
raw: Response;
|
|
108
|
+
}>;
|
|
109
|
+
}
|
|
110
|
+
export interface StopOptions {
|
|
111
|
+
/** How many times to ask before giving up (a network blip should not leave a job running). Default 2. */
|
|
112
|
+
attempts?: number;
|
|
113
|
+
/** How long to wait between two asks, in milliseconds. Default 1200. */
|
|
114
|
+
retryAfterMs?: number;
|
|
115
|
+
/** Waits; replaced in tests. */
|
|
116
|
+
wait?: (milliseconds: number) => Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Asks the server to stop an operation. The call is safe to repeat (stopping a stopped operation changes nothing), so a
|
|
120
|
+
* failure that may pass is retried once.
|
|
121
|
+
*/
|
|
122
|
+
export declare function requestOperationStop(client: OperationStopClient, operationId: string, options?: StopOptions): Promise<StopOutcome>;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* generationSharing: how Stop reaches the server's job, and how the tabs of one browser share what a generation is doing.
|
|
3
|
+
*
|
|
4
|
+
* Two things the creator used to get wrong, both about a generation that runs on the server and outlives the click that
|
|
5
|
+
* started it:
|
|
6
|
+
*
|
|
7
|
+
* - Stop only walked away from the job. It stopped listening, and the server carried on writing (and billing) the whole
|
|
8
|
+
* run. `requestOperationStop` asks the server to stop it (`POST /operations/{id}/cancel`), which aborts the model call.
|
|
9
|
+
* - A second tab of the same customer never learned that the first was generating, so it offered Generate again and a
|
|
10
|
+
* widget could be generated twice at once. The tabs now tell each other which operation they are running
|
|
11
|
+
* (`openGenerationChannel`), and the one that hears it follows the operation by its status on the server, so what the
|
|
12
|
+
* server says is the state: no flag a closed tab left behind keeps a tab waiting.
|
|
13
|
+
*
|
|
14
|
+
* Why a channel and not only the browser's storage: in a partner's page the creator keeps what it remembers in memory (see
|
|
15
|
+
* `host/creatorHost.ts`), so two tabs share nothing there. A `BroadcastChannel` writes nothing anywhere: a message reaches
|
|
16
|
+
* the other tabs of the page's origin that are open right now and is gone. What travels is only a widget's id and an
|
|
17
|
+
* operation's id, and a tab that hears them checks with the server (with its own sign-in) whether the operation is one of
|
|
18
|
+
* its visitor's, so a message that reaches a tab of another visitor of the same browser changes nothing there.
|
|
19
|
+
*
|
|
20
|
+
* Kept free of Svelte and of the page so the rules are testable without a DOM.
|
|
21
|
+
*/
|
|
22
|
+
/** The name of the channel. Namespaced so it cannot be taken for a channel of the partner's own page. */
|
|
23
|
+
export const GENERATION_CHANNEL_NAME = 'widgetic-creator:generation';
|
|
24
|
+
/** An id as the creator knows them (a UUID, or the temporary id of a widget being created): short, and without spaces. */
|
|
25
|
+
const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_:.-]{0,119}$/;
|
|
26
|
+
/** The note a message carries, or null when it is not one of ours (the channel is shared by the whole origin). */
|
|
27
|
+
export function readGenerationNote(data) {
|
|
28
|
+
if (!data || typeof data !== 'object')
|
|
29
|
+
return null;
|
|
30
|
+
const note = data;
|
|
31
|
+
const widgetId = note.widgetId;
|
|
32
|
+
if (typeof widgetId !== 'string' || !ID_PATTERN.test(widgetId))
|
|
33
|
+
return null;
|
|
34
|
+
if (note.kind === 'ask')
|
|
35
|
+
return { kind: 'ask', widgetId };
|
|
36
|
+
if (note.kind === 'started') {
|
|
37
|
+
const operationId = note.operationId;
|
|
38
|
+
if (typeof operationId !== 'string' || !ID_PATTERN.test(operationId))
|
|
39
|
+
return null;
|
|
40
|
+
return { kind: 'started', widgetId, operationId };
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Opens this tab's end of the channel. Returns null where the browser has no `BroadcastChannel` (the creator then works as
|
|
46
|
+
* before: the tabs do not hear each other, and the server's status is still what each tab follows once it knows the id).
|
|
47
|
+
* A handler that throws is logged and does not reach the channel.
|
|
48
|
+
*/
|
|
49
|
+
export function openGenerationChannel(handlers, channelFactory = typeof BroadcastChannel === 'function'
|
|
50
|
+
? (name) => new BroadcastChannel(name)
|
|
51
|
+
: null) {
|
|
52
|
+
if (!channelFactory)
|
|
53
|
+
return null;
|
|
54
|
+
let channel;
|
|
55
|
+
try {
|
|
56
|
+
channel = channelFactory(GENERATION_CHANNEL_NAME);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.warn('[GenerationSharing] The browser would not open the channel between tabs', error);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
channel.onmessage = (event) => {
|
|
63
|
+
const note = readGenerationNote(event.data);
|
|
64
|
+
if (!note)
|
|
65
|
+
return;
|
|
66
|
+
try {
|
|
67
|
+
if (note.kind === 'started')
|
|
68
|
+
handlers.onStarted(note.widgetId, note.operationId);
|
|
69
|
+
else
|
|
70
|
+
handlers.onAsk(note.widgetId);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error('[GenerationSharing] A handler of the channel between tabs threw', error);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const post = (note) => {
|
|
77
|
+
try {
|
|
78
|
+
channel.postMessage(note);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
// A closed channel, or a value the browser would not clone: the other tabs simply do not hear this one.
|
|
82
|
+
console.warn('[GenerationSharing] Could not tell the other tabs', error);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
announceStarted: (widgetId, operationId) => post({ kind: 'started', widgetId, operationId }),
|
|
87
|
+
ask: (widgetId) => post({ kind: 'ask', widgetId }),
|
|
88
|
+
close: () => {
|
|
89
|
+
channel.onmessage = null;
|
|
90
|
+
try {
|
|
91
|
+
channel.close();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
/* already closed */
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// ─── What the server says about an operation ──────────────────────────────────────────────────────────────────────────
|
|
100
|
+
/** The statuses after which nothing more happens to an operation. */
|
|
101
|
+
const FINISHED_STATUSES = new Set(['completed', 'failed', 'cancelled']);
|
|
102
|
+
/** Whether an operation has ended, whichever way. */
|
|
103
|
+
export function isFinishedOperationStatus(status) {
|
|
104
|
+
return typeof status === 'string' && FINISHED_STATUSES.has(status.toLowerCase());
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The operation inside an API answer. The API wraps what it answers in `{ success, data }`; a frame of the stream carries
|
|
108
|
+
* the operation itself. The generated client reads the first shape as an operation with every field empty, so the creator
|
|
109
|
+
* reads the raw body and takes the operation out of it here. Returns null for anything that is not an operation.
|
|
110
|
+
*/
|
|
111
|
+
export function readOperationBody(body) {
|
|
112
|
+
if (!body || typeof body !== 'object')
|
|
113
|
+
return null;
|
|
114
|
+
const record = body;
|
|
115
|
+
const inner = record.data && typeof record.data === 'object' && !Array.isArray(record.data) ? record.data : record;
|
|
116
|
+
if (typeof inner.status !== 'string')
|
|
117
|
+
return null;
|
|
118
|
+
return {
|
|
119
|
+
id: typeof inner.id === 'string' ? inner.id : undefined,
|
|
120
|
+
type: typeof inner.type === 'string' ? inner.type : undefined,
|
|
121
|
+
status: inner.status.toLowerCase(),
|
|
122
|
+
progress: typeof inner.progress === 'number' ? inner.progress : undefined,
|
|
123
|
+
error: typeof inner.error === 'string' ? inner.error : undefined,
|
|
124
|
+
metadata: inner.metadata && typeof inner.metadata === 'object' ? inner.metadata : undefined
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/** The HTTP status of a failed call of the generated client (`ResponseError.response.status`), when it has one. */
|
|
128
|
+
export function httpStatusOfFailure(error) {
|
|
129
|
+
const status = error?.response?.status;
|
|
130
|
+
return typeof status === 'number' ? status : undefined;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Whether an operation is the code generation of this widget, finished or not (the operation names its widget once the
|
|
134
|
+
* generator has started, so an operation that does not name one yet is taken on the word of whoever pointed to it).
|
|
135
|
+
*/
|
|
136
|
+
export function isGenerationOfWidget(snapshot, widgetId) {
|
|
137
|
+
if (!snapshot)
|
|
138
|
+
return false;
|
|
139
|
+
if (snapshot.type && snapshot.type !== 'code-generate')
|
|
140
|
+
return false;
|
|
141
|
+
const namedWidget = snapshot.metadata?.widgetId;
|
|
142
|
+
if (typeof namedWidget === 'string' && namedWidget.length > 0 && namedWidget !== widgetId)
|
|
143
|
+
return false;
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
/** A refusal that asking again cannot change (the request itself is wrong or not allowed); a timeout or a busy server can change. */
|
|
147
|
+
function refusalIsFinal(httpStatus) {
|
|
148
|
+
return httpStatus !== undefined && httpStatus >= 400 && httpStatus < 500 && httpStatus !== 408 && httpStatus !== 429;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Asks the server to stop an operation. The call is safe to repeat (stopping a stopped operation changes nothing), so a
|
|
152
|
+
* failure that may pass is retried once.
|
|
153
|
+
*/
|
|
154
|
+
export async function requestOperationStop(client, operationId, options = {}) {
|
|
155
|
+
const attempts = Math.max(1, options.attempts ?? 2);
|
|
156
|
+
const retryAfterMs = options.retryAfterMs ?? 1200;
|
|
157
|
+
const wait = options.wait ?? ((milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)));
|
|
158
|
+
let lastError = null;
|
|
159
|
+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
160
|
+
try {
|
|
161
|
+
const response = await client.operationsIdCancelPostRaw({ id: operationId });
|
|
162
|
+
let body = null;
|
|
163
|
+
try {
|
|
164
|
+
body = await response.raw.json();
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// An answer without a readable body still says the server took the stop (it answered 2xx).
|
|
168
|
+
}
|
|
169
|
+
const snapshot = readOperationBody(body);
|
|
170
|
+
// The answer is the operation as it stands: `cancelled` when this stop did it, or whatever it had already become.
|
|
171
|
+
if (snapshot && isFinishedOperationStatus(snapshot.status) && snapshot.status !== 'cancelled') {
|
|
172
|
+
return { outcome: 'already-ended', status: snapshot.status };
|
|
173
|
+
}
|
|
174
|
+
return { outcome: 'stopped' };
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
const httpStatus = httpStatusOfFailure(error);
|
|
178
|
+
if (httpStatus === 404)
|
|
179
|
+
return { outcome: 'gone' };
|
|
180
|
+
lastError = error;
|
|
181
|
+
if (refusalIsFinal(httpStatus))
|
|
182
|
+
break;
|
|
183
|
+
if (attempt < attempts)
|
|
184
|
+
await wait(retryAfterMs);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return { outcome: 'failed', error: lastError };
|
|
188
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export function nativeScrollbarPadding(node, padPx = 8) {
|
|
3
3
|
function check() {
|
|
4
4
|
const hasScrollbar = node.scrollHeight > node.clientHeight + 1;
|
|
5
|
-
node.style.setProperty('padding-
|
|
5
|
+
node.style.setProperty('padding-inline-end', hasScrollbar ? '0px' : `${padPx}px`, 'important');
|
|
6
6
|
}
|
|
7
7
|
const resizeObserver = new ResizeObserver(check);
|
|
8
8
|
resizeObserver.observe(node);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pickedFile — what the file control (`FileUrlSourceControl.svelte`) makes of the file a person picks.
|
|
3
|
+
*
|
|
4
|
+
* An archive or a font is binary: it is read as a data URL and sent as base64, so everything before the first comma of
|
|
5
|
+
* the data URL (`data:font/woff2;base64`) is cut off. Any other file is text and is sent as it was read.
|
|
6
|
+
*
|
|
7
|
+
* Kept pure so the rules are testable without a DOM.
|
|
8
|
+
*/
|
|
9
|
+
/** Whether the file with this name is read as binary (an archive or a font) rather than as text. */
|
|
10
|
+
export declare function isBinaryFileName(fileName: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The content to send for a picked file, from what the file reader answered. A binary file was read as a data URL, which
|
|
13
|
+
* loses everything up to and including its first comma (what is left is the base64); a text file is sent as it was read.
|
|
14
|
+
*/
|
|
15
|
+
export declare function contentOfPickedFile(isBinary: boolean, readerResult: string): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pickedFile — what the file control (`FileUrlSourceControl.svelte`) makes of the file a person picks.
|
|
3
|
+
*
|
|
4
|
+
* An archive or a font is binary: it is read as a data URL and sent as base64, so everything before the first comma of
|
|
5
|
+
* the data URL (`data:font/woff2;base64`) is cut off. Any other file is text and is sent as it was read.
|
|
6
|
+
*
|
|
7
|
+
* Kept pure so the rules are testable without a DOM.
|
|
8
|
+
*/
|
|
9
|
+
/** The endings of the files that are read as binary: archives and fonts. */
|
|
10
|
+
const BINARY_FILE_NAME_PATTERN = /\.(zip|tar|tgz|gz|woff2?|ttf|otf|eot)$/i;
|
|
11
|
+
/** Whether the file with this name is read as binary (an archive or a font) rather than as text. */
|
|
12
|
+
export function isBinaryFileName(fileName) {
|
|
13
|
+
return BINARY_FILE_NAME_PATTERN.test(fileName);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The content to send for a picked file, from what the file reader answered. A binary file was read as a data URL, which
|
|
17
|
+
* loses everything up to and including its first comma (what is left is the base64); a text file is sent as it was read.
|
|
18
|
+
*/
|
|
19
|
+
export function contentOfPickedFile(isBinary, readerResult) {
|
|
20
|
+
return isBinary ? readerResult.slice(readerResult.indexOf(',') + 1) : readerResult;
|
|
21
|
+
}
|
|
@@ -19,6 +19,15 @@ export type ChatUploadOptions = {
|
|
|
19
19
|
onProgress?: (percentage: number) => void;
|
|
20
20
|
abortSignal?: AbortSignal;
|
|
21
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* An upload the API turned down. Its message is the API's own (what the chat shows); the status and the body of the
|
|
24
|
+
* answer are kept as well, so the creator can tell a limit (the partner's storage is full) from an ordinary failure.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ChatUploadRefusedError extends Error {
|
|
27
|
+
readonly status: number;
|
|
28
|
+
readonly body: unknown;
|
|
29
|
+
constructor(message: string, status: number, body: unknown);
|
|
30
|
+
}
|
|
22
31
|
/**
|
|
23
32
|
* POST …/complete. A 409 `TUS_UPLOAD_BUSY` means another request still has the upload (the chunk that
|
|
24
33
|
* just finished, or a retried one), so ask again after a short pause. Every other answer is final: a
|
|
@@ -6,12 +6,32 @@ const DEFAULT_TUS_CHUNK_SIZE = 5 * 1024 * 1024;
|
|
|
6
6
|
const TUS_UPLOAD_BUSY_CODE = 'TUS_UPLOAD_BUSY';
|
|
7
7
|
/** Pauses before asking again after a "busy" answer: 0.5, 1, 2, 4 and 8 seconds, about 15 seconds in all. */
|
|
8
8
|
const COMPLETE_BUSY_RETRY_DELAYS_MS = [500, 1000, 2000, 4000, 8000];
|
|
9
|
+
/** The message in the JSON body of an error answer, or the fallback. */
|
|
10
|
+
function messageOfErrorBody(json, fallback) {
|
|
11
|
+
const body = json;
|
|
12
|
+
const message = body?.message || body?.error?.message;
|
|
13
|
+
return typeof message === 'string' && message ? message : fallback;
|
|
14
|
+
}
|
|
9
15
|
function parseErrorMessage(response, fallback) {
|
|
10
16
|
return response
|
|
11
17
|
.json()
|
|
12
|
-
.then((json) => json
|
|
18
|
+
.then((json) => messageOfErrorBody(json, fallback))
|
|
13
19
|
.catch(() => fallback);
|
|
14
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* An upload the API turned down. Its message is the API's own (what the chat shows); the status and the body of the
|
|
23
|
+
* answer are kept as well, so the creator can tell a limit (the partner's storage is full) from an ordinary failure.
|
|
24
|
+
*/
|
|
25
|
+
export class ChatUploadRefusedError extends Error {
|
|
26
|
+
status;
|
|
27
|
+
body;
|
|
28
|
+
constructor(message, status, body) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'ChatUploadRefusedError';
|
|
31
|
+
this.status = status;
|
|
32
|
+
this.body = body;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
15
35
|
/** The `error.code` of a failed answer, or undefined when the body is not the API's error envelope. */
|
|
16
36
|
function readErrorCode(response) {
|
|
17
37
|
return response
|
|
@@ -98,7 +118,9 @@ async function uploadMultipart(file, config) {
|
|
|
98
118
|
body: formData,
|
|
99
119
|
});
|
|
100
120
|
if (!response.ok) {
|
|
101
|
-
|
|
121
|
+
// Read once: the message is what the chat shows, the status and the body tell the creator whether it was a limit.
|
|
122
|
+
const body = await response.json().catch(() => undefined);
|
|
123
|
+
throw new ChatUploadRefusedError(messageOfErrorBody(body, `Upload failed (${response.status})`), response.status, body);
|
|
102
124
|
}
|
|
103
125
|
const json = await response.json();
|
|
104
126
|
const data = (json?.data ?? json);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@widgetic/creator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"@sveltejs/kit": "^2.20.0",
|
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
|
-
"dist"
|
|
32
|
+
"dist",
|
|
33
|
+
"!dist/**/*.test.*",
|
|
34
|
+
"!dist/**/*.spec.*"
|
|
33
35
|
],
|
|
34
36
|
"scripts": {
|
|
35
37
|
"i": "rm -rf node_modules && npm i && npm run lps",
|
|
@@ -97,7 +99,7 @@
|
|
|
97
99
|
"typescript-eslint": "^8.20.0",
|
|
98
100
|
"vite": "^6.2.6",
|
|
99
101
|
"vite-imagetools": "^7.0.4",
|
|
100
|
-
"vitest": "^
|
|
102
|
+
"vitest": "^5.0.3"
|
|
101
103
|
},
|
|
102
104
|
"dependencies": {
|
|
103
105
|
"@mdi/js": "^7.4.47",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|