@widgetic/creator 0.4.37 → 0.4.38

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.
@@ -194,6 +194,13 @@
194
194
  type OperationSnapshot
195
195
  } from './utils/generationSharing.js';
196
196
  import { failureNoticeChanges } from './utils/failureNotice.js';
197
+ import {
198
+ loadRememberedVersionRatings,
199
+ rateVersionThroughApi,
200
+ rememberVersionRating,
201
+ saveRememberedVersionRatings,
202
+ type VersionRating
203
+ } from './utils/versionRatings.js';
197
204
  import { generationVersionAndDocumentFields } from './utils/generationRequestFields.js';
198
205
  import {
199
206
  generationRefusalText,
@@ -227,6 +234,7 @@
227
234
  mayUseAddressBar,
228
235
  mayShowAccountBilling,
229
236
  maySuggestWidgetName,
237
+ mayRateVersions,
230
238
  mayListenToSiteEvents,
231
239
  announceToSite,
232
240
  eventIsFromCreator,
@@ -3498,6 +3506,32 @@
3498
3506
  /** One restore at a time. A second click while GitLab is still answering used to be told a generation was running. */
3499
3507
  let _restoreCheckpointInFlight = false;
3500
3508
 
3509
+ /**
3510
+ * The thumbs the person gave to versions, by the id of the message that announced the version. The API keeps the word for Widget
3511
+ * Lessons (what the code generator learns across people) but has no call that lists them, so the creator remembers them and hands
3512
+ * them back to the chat (`versionRatings`) when it opens again. In a partner's page the storage is memory, and no thumbs is offered.
3513
+ */
3514
+ let versionRatingsByMessageId: Record<string, VersionRating> = loadRememberedVersionRatings();
3515
+
3516
+ /**
3517
+ * A thumbs the person pressed next to a version in the chat: tells the API, then remembers it. It throws when the API could not be
3518
+ * told, and the chat then puts the thumbs back as it was (nothing is remembered). The same conversation and message ids the chat
3519
+ * lists are the ones the API knows: a version is the saved message that announced a commit.
3520
+ */
3521
+ async function handleRateVersion(conversationId: string, messageId: string, rating: VersionRating): Promise<void> {
3522
+ if (!messagesClient || !conversationId || isLocalOnlyConversationId(conversationId)) {
3523
+ console.warn('[Chat→Backend] A version cannot be rated before its conversation is saved', { conversationId, messageId });
3524
+ throw new Error('The conversation of this version is not saved yet');
3525
+ }
3526
+ const outcome = await rateVersionThroughApi(messagesClient, { conversationId, messageId, rating });
3527
+ if (outcome.recorded === false) {
3528
+ // Not a failure: the version has no trace to attach the word to (it is older than the tracing), or the learning is off.
3529
+ console.debug('[Chat→Backend] The API did not keep the thumbs', { messageId, rating, reason: outcome.reason });
3530
+ }
3531
+ versionRatingsByMessageId = rememberVersionRating(versionRatingsByMessageId, messageId, rating);
3532
+ saveRememberedVersionRatings(versionRatingsByMessageId);
3533
+ }
3534
+
3501
3535
  /** Restore conversation to a checkpoint message via backend (deletes messages + git rollback) */
3502
3536
  async function handleRestoreCheckpoint(
3503
3537
  conversationId: string,
@@ -20035,6 +20069,8 @@ $: if (codeGenerationPrompt && selectedWidgetId && promptLoadedForWidgetId === s
20035
20069
  onRestoreCheckpointBackend={(conversationId, checkpointMessageId) =>
20036
20070
  handleRestoreCheckpoint(conversationId, checkpointMessageId, panelWidgetId)
20037
20071
  }
20072
+ onRateVersion={mayRateVersions() ? handleRateVersion : undefined}
20073
+ versionRatings={versionRatingsByMessageId}
20038
20074
  onChatReady={(methods) => handleChatReady(methods, panelWidgetId)}
20039
20075
  onDraftChange={() => handleChatDraftChange(panelWidgetId)}
20040
20076
  onConversationChange={(conversationId) => handleChatConversationChange(conversationId, panelWidgetId)}
@@ -100,6 +100,13 @@ export declare function mayShowAccountBilling(): boolean;
100
100
  * partner's page the creator does not ask, and the widget keeps the name of its draft.
101
101
  */
102
102
  export declare function maySuggestWidgetName(): boolean;
103
+ /**
104
+ * Whether the creator may offer a person the thumbs next to a version of their widget (`PUT
105
+ * /conversations/{conversationId}/messages/{messageId}/rating`). That call is for Widgetic's own apps: it is not on the gateway's
106
+ * list of what a partner's visitor may reach (the editor session), so in a partner's page every press would be refused. There the
107
+ * creator draws no thumbs at all.
108
+ */
109
+ export declare function mayRateVersions(): boolean;
103
110
  /** Whether the creator may take part in the page-wide `widgetic:*` events it exchanges with the Widgetic site's own page chrome. */
104
111
  export declare function mayListenToSiteEvents(): boolean;
105
112
  /**
@@ -108,6 +108,15 @@ export function mayShowAccountBilling() {
108
108
  export function maySuggestWidgetName() {
109
109
  return !state.partnerMode;
110
110
  }
111
+ /**
112
+ * Whether the creator may offer a person the thumbs next to a version of their widget (`PUT
113
+ * /conversations/{conversationId}/messages/{messageId}/rating`). That call is for Widgetic's own apps: it is not on the gateway's
114
+ * list of what a partner's visitor may reach (the editor session), so in a partner's page every press would be refused. There the
115
+ * creator draws no thumbs at all.
116
+ */
117
+ export function mayRateVersions() {
118
+ return !state.partnerMode;
119
+ }
111
120
  /** Whether the creator may take part in the page-wide `widgetic:*` events it exchanges with the Widgetic site's own page chrome. */
112
121
  export function mayListenToSiteEvents() {
113
122
  return !state.partnerMode;
@@ -593,6 +593,9 @@ export declare const CREATOR_CATALOGS: {
593
593
  readonly 'chat.messages.restore': "Restore Checkpoint";
594
594
  readonly 'chat.messages.restoreTitle': "Restore widget code to this prompt's commit";
595
595
  readonly 'chat.messages.alreadyHead': "This checkpoint is already HEAD";
596
+ readonly 'chat.messages.rateGroup': "Rate this version";
597
+ readonly 'chat.messages.rateUp': "This version is good";
598
+ readonly 'chat.messages.rateDown': "This version is not good";
596
599
  readonly 'chat.messages.version': "Version {number}";
597
600
  readonly 'chat.messages.restoredFrom': "Restored from {label}";
598
601
  readonly 'chat.messages.previousVersion': "a previous version";
@@ -0,0 +1,65 @@
1
+ /**
2
+ * versionRatings — the thumbs a person gives to the versions of a widget in the creator's chat.
3
+ *
4
+ * The chat (`@widgetic/chat`) draws two thumbs next to each version and tells the creator what the person pressed (`onRateVersion`).
5
+ * The creator does two things with it:
6
+ *
7
+ * 1. It tells the API (`PUT /conversations/{conversationId}/messages/{messageId}/rating`, the generated `rateMessageVersionRaw`),
8
+ * which keeps the word for Widget Lessons (what the code generator learns across people). The API takes three words: `up`, `down`
9
+ * and `none` (a rating taken back).
10
+ * 2. It remembers the thumbs here, in the creator's own storage, so the thumbs is still showing when the chat opens again: the API
11
+ * has no call that lists ratings, so the chat is handed what the creator remembers (`versionRatings`).
12
+ *
13
+ * Nothing here throws into the chat except the call to the API: when it fails the chat puts the thumbs back as it was, and nothing is
14
+ * remembered. A storage that is full or blocked is never a reason to fail a press.
15
+ */
16
+ /** What a person can say about a version: a thumbs up, a thumbs down, or nothing (a rating taken back). */
17
+ export type VersionRating = 'up' | 'down' | 'none';
18
+ /** Where the creator remembers the thumbs: one record of the id of the message that announced a version, to `up` or `down`. */
19
+ export declare const VERSION_RATINGS_STORAGE_KEY = "widgetCreator_versionRatings_v1";
20
+ /** How many thumbs are remembered: the newest ones, because an old version is rarely looked at again. */
21
+ export declare const MAX_REMEMBERED_VERSION_RATINGS = 500;
22
+ /** What the creator asks of the API client: the generated `MessagesApi` has this call once the SDK is regenerated. */
23
+ export interface VersionRatingClient {
24
+ rateMessageVersionRaw(requestParameters: {
25
+ conversationId: string;
26
+ messageId: string;
27
+ rateMessageVersionRequest: {
28
+ rating: VersionRating;
29
+ };
30
+ }): Promise<{
31
+ raw: {
32
+ json(): Promise<unknown>;
33
+ };
34
+ }>;
35
+ }
36
+ /** Which version of which conversation is rated, and how. */
37
+ export interface VersionRatingRequest {
38
+ conversationId: string;
39
+ messageId: string;
40
+ rating: VersionRating;
41
+ }
42
+ /** What the API said about the word it was sent. */
43
+ export interface VersionRatingOutcome {
44
+ /** Whether the API kept the word: `null` when its answer could not be read. */
45
+ recorded: boolean | null;
46
+ /** Why it did not (`switched_off`, `no_record`, `unavailable`), when it said. */
47
+ reason: string | null;
48
+ }
49
+ /**
50
+ * Tells the API what the person pressed. The API answers 200 for anyone who may rate, and says in the answer when it could not keep the
51
+ * word (`recorded: false`: the version is older than the tracing, or the learning is switched off); that is no failure for the person,
52
+ * so it is handed back for the caller to note and nothing more. A call that fails (offline, refused) throws, and the chat undoes the press.
53
+ */
54
+ export declare function rateVersionThroughApi(client: VersionRatingClient, request: VersionRatingRequest): Promise<VersionRatingOutcome>;
55
+ /** The thumbs found in what the storage held: a record of message id to `up` or `down`, and nothing else. Anything unreadable is nothing. */
56
+ export declare function parseRememberedVersionRatings(stored: string | null | undefined): Record<string, VersionRating>;
57
+ /**
58
+ * The thumbs after a version was rated: the rating replaces the one the version had and is put last, `none` takes it away, and past
59
+ * `limit` the oldest are dropped. The record that was given is not changed.
60
+ */
61
+ export declare function rememberVersionRating(ratings: Readonly<Record<string, VersionRating>>, messageId: string, rating: VersionRating, limit?: number): Record<string, VersionRating>;
62
+ /** The thumbs the creator remembers. A storage that cannot be read is no thumbs. */
63
+ export declare function loadRememberedVersionRatings(): Record<string, VersionRating>;
64
+ /** Remembers the thumbs. A storage that is full or blocked is never a reason to fail a press. */
65
+ export declare function saveRememberedVersionRatings(ratings: Readonly<Record<string, VersionRating>>): void;
@@ -0,0 +1,97 @@
1
+ /**
2
+ * versionRatings — the thumbs a person gives to the versions of a widget in the creator's chat.
3
+ *
4
+ * The chat (`@widgetic/chat`) draws two thumbs next to each version and tells the creator what the person pressed (`onRateVersion`).
5
+ * The creator does two things with it:
6
+ *
7
+ * 1. It tells the API (`PUT /conversations/{conversationId}/messages/{messageId}/rating`, the generated `rateMessageVersionRaw`),
8
+ * which keeps the word for Widget Lessons (what the code generator learns across people). The API takes three words: `up`, `down`
9
+ * and `none` (a rating taken back).
10
+ * 2. It remembers the thumbs here, in the creator's own storage, so the thumbs is still showing when the chat opens again: the API
11
+ * has no call that lists ratings, so the chat is handed what the creator remembers (`versionRatings`).
12
+ *
13
+ * Nothing here throws into the chat except the call to the API: when it fails the chat puts the thumbs back as it was, and nothing is
14
+ * remembered. A storage that is full or blocked is never a reason to fail a press.
15
+ */
16
+ import { creatorStorage } from '../host/creatorHost.js';
17
+ /** Where the creator remembers the thumbs: one record of the id of the message that announced a version, to `up` or `down`. */
18
+ export const VERSION_RATINGS_STORAGE_KEY = 'widgetCreator_versionRatings_v1';
19
+ /** How many thumbs are remembered: the newest ones, because an old version is rarely looked at again. */
20
+ export const MAX_REMEMBERED_VERSION_RATINGS = 500;
21
+ /**
22
+ * Tells the API what the person pressed. The API answers 200 for anyone who may rate, and says in the answer when it could not keep the
23
+ * word (`recorded: false`: the version is older than the tracing, or the learning is switched off); that is no failure for the person,
24
+ * so it is handed back for the caller to note and nothing more. A call that fails (offline, refused) throws, and the chat undoes the press.
25
+ */
26
+ export async function rateVersionThroughApi(client, request) {
27
+ const { conversationId, messageId, rating } = request;
28
+ const response = await client.rateMessageVersionRaw({ conversationId, messageId, rateMessageVersionRequest: { rating } });
29
+ try {
30
+ const body = (await response.raw.json());
31
+ const recorded = typeof body?.data?.recorded === 'boolean' ? body.data.recorded : null;
32
+ const reason = typeof body?.data?.reason === 'string' ? body.data.reason : null;
33
+ return { recorded, reason };
34
+ }
35
+ catch {
36
+ /* an answer that cannot be read changes nothing for the person: the word was sent */
37
+ return { recorded: null, reason: null };
38
+ }
39
+ }
40
+ /** The thumbs found in what the storage held: a record of message id to `up` or `down`, and nothing else. Anything unreadable is nothing. */
41
+ export function parseRememberedVersionRatings(stored) {
42
+ if (!stored)
43
+ return {};
44
+ try {
45
+ const parsed = JSON.parse(stored);
46
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed))
47
+ return {};
48
+ const remembered = {};
49
+ for (const [messageId, rating] of Object.entries(parsed)) {
50
+ if (rating === 'up' || rating === 'down')
51
+ remembered[messageId] = rating;
52
+ }
53
+ return remembered;
54
+ }
55
+ catch {
56
+ return {};
57
+ }
58
+ }
59
+ /**
60
+ * The thumbs after a version was rated: the rating replaces the one the version had and is put last, `none` takes it away, and past
61
+ * `limit` the oldest are dropped. The record that was given is not changed.
62
+ */
63
+ export function rememberVersionRating(ratings, messageId, rating, limit = MAX_REMEMBERED_VERSION_RATINGS) {
64
+ const others = {};
65
+ for (const [otherId, otherRating] of Object.entries(ratings)) {
66
+ if (otherId !== messageId)
67
+ others[otherId] = otherRating;
68
+ }
69
+ if (rating === 'none')
70
+ return others;
71
+ const next = { ...others, [messageId]: rating };
72
+ const ids = Object.keys(next);
73
+ if (ids.length <= limit)
74
+ return next;
75
+ const kept = {};
76
+ for (const id of ids.slice(ids.length - Math.max(1, limit)))
77
+ kept[id] = next[id];
78
+ return kept;
79
+ }
80
+ /** The thumbs the creator remembers. A storage that cannot be read is no thumbs. */
81
+ export function loadRememberedVersionRatings() {
82
+ try {
83
+ return parseRememberedVersionRatings(creatorStorage.getItem(VERSION_RATINGS_STORAGE_KEY));
84
+ }
85
+ catch {
86
+ return {};
87
+ }
88
+ }
89
+ /** Remembers the thumbs. A storage that is full or blocked is never a reason to fail a press. */
90
+ export function saveRememberedVersionRatings(ratings) {
91
+ try {
92
+ creatorStorage.setItem(VERSION_RATINGS_STORAGE_KEY, JSON.stringify(ratings));
93
+ }
94
+ catch {
95
+ /* a full or blocked store must never break a press */
96
+ }
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/creator",
3
- "version": "0.4.37",
3
+ "version": "0.4.38",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "type": "module",
6
6
  "peerDependencies": {
@@ -8,7 +8,7 @@
8
8
  "@widgetic/api-sdk": "^2.0.0",
9
9
  "@widgetic/cache-layer": "^0.1.2",
10
10
  "@widgetic/canvas": "^0.5.27",
11
- "@widgetic/chat": "^0.1.17",
11
+ "@widgetic/chat": "^0.1.34",
12
12
  "@widgetic/design-system": "^0.5.15",
13
13
  "@widgetic/editor": "^4.0.10",
14
14
  "@widgetic/file-browser": "^0.5.24",
@@ -75,7 +75,7 @@
75
75
  "@widgetic/api-sdk": "^2.0.0",
76
76
  "@widgetic/cache-layer": "^0.1.2",
77
77
  "@widgetic/canvas": "^0.5.27",
78
- "@widgetic/chat": "^0.1.17",
78
+ "@widgetic/chat": "^0.1.34",
79
79
  "@widgetic/design-system": "^0.5.15",
80
80
  "@widgetic/dev-tools": "^0.3.0",
81
81
  "@widgetic/editor": "^4.0.10",