@miso.ai/client-sdk-ui 1.14.0-beta.2 → 1.14.0-beta.3
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/cjs/asset/svgs.js +4 -2
- package/dist/cjs/constants.js +2 -0
- package/dist/cjs/defaults.js +100 -10
- package/dist/cjs/element/container/index.js +14 -0
- package/dist/cjs/element/container/miso-message-item.js +91 -0
- package/dist/cjs/element/container/miso-thread-item.js +91 -0
- package/dist/cjs/element/role/index.js +7 -0
- package/dist/cjs/element/role/miso-delete.js +20 -0
- package/dist/cjs/layout/button/button.js +45 -5
- package/dist/cjs/layout/button/checkbox.js +2 -1
- package/dist/cjs/layout/container/item-container.js +99 -0
- package/dist/cjs/layout/index.js +14 -0
- package/dist/cjs/layout/input/search-box.js +9 -4
- package/dist/cjs/layout/list/collection.js +10 -3
- package/dist/cjs/layout/list/messages.js +239 -226
- package/dist/cjs/layout/list/threads.js +97 -155
- package/dist/cjs/layout/templates.js +65 -38
- package/dist/cjs/layout/text/question.js +71 -0
- package/dist/cjs/layout/text/typewriter/index.js +14 -21
- package/dist/cjs/util/dom.js +21 -0
- package/dist/cjs/version.js +1 -1
- package/package.json +3 -3
- package/src/CLAUDE.md +6 -2
- package/src/asset/svgs.js +3 -1
- package/src/constants.js +2 -0
- package/src/defaults.js +71 -4
- package/src/element/container/index.js +2 -0
- package/src/element/container/miso-message-item.js +94 -0
- package/src/element/container/miso-thread-item.js +94 -0
- package/src/element/role/index.js +1 -0
- package/src/element/role/miso-delete.js +16 -0
- package/src/layout/button/button.js +40 -4
- package/src/layout/button/checkbox.js +2 -1
- package/src/layout/container/item-container.js +99 -0
- package/src/layout/index.js +2 -0
- package/src/layout/input/search-box.js +4 -2
- package/src/layout/list/collection.js +12 -4
- package/src/layout/list/messages.js +202 -204
- package/src/layout/list/threads.js +90 -130
- package/src/layout/templates.js +74 -39
- package/src/layout/text/question.js +58 -0
- package/src/layout/text/typewriter/index.js +10 -15
- package/src/util/dom.js +16 -0
- package/src/version.js +1 -1
|
@@ -1,35 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isThreadUnread } from '@miso.ai/client-sdk-workflow';
|
|
2
2
|
import { LAYOUT_TYPE } from '../../constants.js';
|
|
3
3
|
import CollectionLayout from './collection.js';
|
|
4
4
|
import { setOrRemoveAttribute } from '../../util/dom.js';
|
|
5
|
-
import confirm from '../../util/confirm.js';
|
|
6
|
-
import prompt from '../../util/prompt.js';
|
|
7
5
|
|
|
8
6
|
const TYPE = LAYOUT_TYPE.THREADS;
|
|
9
7
|
const DEFAULT_CLASSNAME = 'miso-threads';
|
|
10
8
|
|
|
11
|
-
// the thread template renders the <li> itself (it is the item, carrying the
|
|
12
|
-
// item role and state), so the stock <li> wrapping is skipped
|
|
13
|
-
function item(layout, state, value, index) {
|
|
14
|
-
const { templates, options } = layout;
|
|
15
|
-
return templates[options.itemType](layout, state, value, { index });
|
|
16
|
-
}
|
|
17
|
-
|
|
18
9
|
/**
|
|
19
|
-
* The thread list of the chat history interface: a
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
10
|
+
* The thread list of the chat history interface: a shell that renders one
|
|
11
|
+
* <miso-thread-item> container element per thread item, incrementally —
|
|
12
|
+
* fresh items land at either end of the list: a newly created thread
|
|
13
|
+
* renders as a prepended item (the list is newest-first), while an older
|
|
14
|
+
* page fetched by the more flow (infinite scroll: the collection layout's
|
|
15
|
+
* stock trigger, viewable at the list's bottom, fires the `more` hub
|
|
16
|
+
* field) renders as appended items — told apart by which boundary the
|
|
17
|
+
* already-rendered records line up with. The content
|
|
18
|
+
* of an item is not rendered here: each <miso-thread-item> is assigned its item
|
|
19
|
+
* subworkflow (workflow._getThreadWorkflow, off the item binding) in the
|
|
20
|
+
* post-render sync pass, and the role elements inside (the title, and the
|
|
21
|
+
* context menu's rename/delete buttons) render through that workflow's own
|
|
22
|
+
* layouts — so a record change re-renders the affected item's roles alone.
|
|
28
23
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
24
|
+
* The shell keeps the list-level behaviors. Selection comes with the data:
|
|
25
|
+
* the history workflow stamps `selectedThreadId` into its value and
|
|
26
|
+
* decorates each thread record with a `selected` flag, so any selection
|
|
27
|
+
* change flows down the regular data path; in-place state changes on
|
|
28
|
+
* existing items (selected, unread, the thread identity of a settled
|
|
29
|
+
* placeholder) are applied by `_syncItems` after each render, reading the
|
|
30
|
+
* fresh values off the item bindings — no item re-render needed. Each item
|
|
31
|
+
* carries a context menu (vertical dots), but that is the thread workflow's
|
|
32
|
+
* too: its item-container layout inserts and toggles it — the shell only
|
|
33
|
+
* keeps menu clicks (by their item-menu data-roles) from selecting.
|
|
33
34
|
*/
|
|
34
35
|
export default class ThreadsLayout extends CollectionLayout {
|
|
35
36
|
|
|
@@ -41,45 +42,63 @@ export default class ThreadsLayout extends CollectionLayout {
|
|
|
41
42
|
return DEFAULT_CLASSNAME;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
constructor({ className = DEFAULT_CLASSNAME,
|
|
45
|
-
super({
|
|
46
|
-
className,
|
|
47
|
-
templates: { item, ...templates },
|
|
48
|
-
...options,
|
|
49
|
-
});
|
|
45
|
+
constructor({ className = DEFAULT_CLASSNAME, ...options } = {}) {
|
|
46
|
+
super({ className, ...options });
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
// item identity: the thread id, or the local placeholder id standing in
|
|
50
|
+
// for it while a thread is being created — the workflow adopts the thread
|
|
51
|
+
// id at the settle, so the element and its workflow survive it
|
|
52
|
+
_getItemKey(thread) {
|
|
53
|
+
return thread.thread_id || thread.placeholder_id || thread;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// incremental renders cover a pure prepend (a created thread) or a pure
|
|
57
|
+
// append (an older page) — anything else re-renders in full
|
|
58
|
+
_shallRenderIncrementally(state, rendered) {
|
|
59
|
+
if (!super._shallRenderIncrementally(state, rendered)) {
|
|
60
|
+
return false;
|
|
64
61
|
}
|
|
62
|
+
const values = this._getItems(state) || [];
|
|
63
|
+
const renderedValues = rendered.value;
|
|
64
|
+
const count = values.length - renderedValues.length;
|
|
65
|
+
return count === 0 || // in-place changes only
|
|
66
|
+
this._getItemKey(values[0]) === this._getItemKey(renderedValues[0]) || // appended
|
|
67
|
+
this._getItemKey(values[count]) === this._getItemKey(renderedValues[0]); // prepended
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
// the
|
|
68
|
-
//
|
|
70
|
+
// the fresh items land at the head (a newly created thread — the list is
|
|
71
|
+
// newest-first) or at the tail (an older page fetched by the more flow):
|
|
72
|
+
// told apart by which boundary the already-rendered records line up with
|
|
69
73
|
_html(state, rendered, incremental) {
|
|
70
|
-
if (incremental) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
if (!incremental) {
|
|
75
|
+
return this.templates.root(this, state);
|
|
76
|
+
}
|
|
77
|
+
const values = this._getItems(state) || [];
|
|
78
|
+
const renderedValues = rendered.value;
|
|
79
|
+
const count = values.length - renderedValues.length;
|
|
80
|
+
if (count === 0) {
|
|
81
|
+
return '';
|
|
74
82
|
}
|
|
75
|
-
|
|
83
|
+
if (this._getItemKey(values[0]) === this._getItemKey(renderedValues[0])) {
|
|
84
|
+
const fresh = values.slice(renderedValues.length);
|
|
85
|
+
return {
|
|
86
|
+
position: 'beforeend',
|
|
87
|
+
items: this.templates.items(this, state, fresh, { offset: renderedValues.length }),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const fresh = values.slice(0, count);
|
|
91
|
+
return {
|
|
92
|
+
position: 'afterbegin',
|
|
93
|
+
items: this.templates.items(this, state, fresh, { offset: 0 }),
|
|
94
|
+
};
|
|
76
95
|
}
|
|
77
96
|
|
|
78
97
|
_render(element, { state }, { notifyUpdate }) {
|
|
79
98
|
const { incremental, html } = state;
|
|
80
99
|
if (incremental) {
|
|
81
100
|
if (html) {
|
|
82
|
-
this._getListElement(element).insertAdjacentHTML(
|
|
101
|
+
this._getListElement(element).insertAdjacentHTML(html.position, html.items);
|
|
83
102
|
} else {
|
|
84
103
|
notifyUpdate(false);
|
|
85
104
|
}
|
|
@@ -90,12 +109,28 @@ export default class ThreadsLayout extends CollectionLayout {
|
|
|
90
109
|
|
|
91
110
|
_afterRender(element, state) {
|
|
92
111
|
super._afterRender(element, state); // syncs bindings to the latest values
|
|
112
|
+
this._syncWorkflows(element);
|
|
93
113
|
this._syncItems(element);
|
|
94
114
|
}
|
|
95
115
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
116
|
+
// assign each <miso-thread-item> its item subworkflow, off the item binding
|
|
117
|
+
_syncWorkflows(element) {
|
|
118
|
+
const workflow = this._view && this._view.workflow;
|
|
119
|
+
if (!workflow || typeof workflow._getThreadWorkflow !== 'function') {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
for (const item of this._getItemElements(element)) {
|
|
123
|
+
const binding = this._bindings.get(item);
|
|
124
|
+
if (binding && item.isContainer) {
|
|
125
|
+
item.workflow = workflow._getThreadWorkflow(binding.value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// sync in-place item state changes from the bound values onto the existing
|
|
131
|
+
// item elements: selection/unread state, and the thread identity of a
|
|
132
|
+
// placeholder that settled into its real record — the item contents render
|
|
133
|
+
// through the thread workflows, outside this layout's render cycle
|
|
99
134
|
_syncItems(element) {
|
|
100
135
|
for (const item of this._getItemElements(element)) {
|
|
101
136
|
const binding = this._bindings.get(item);
|
|
@@ -105,38 +140,16 @@ export default class ThreadsLayout extends CollectionLayout {
|
|
|
105
140
|
const { value } = binding;
|
|
106
141
|
setOrRemoveAttribute(item, 'data-selected', value.selected ? '' : undefined);
|
|
107
142
|
setOrRemoveAttribute(item, 'data-unread', isThreadUnread(value) ? '' : undefined);
|
|
108
|
-
setOrRemoveAttribute(item, 'data-thread-id',
|
|
109
|
-
const titleElement = item.querySelector(`.${this.className}__title`);
|
|
110
|
-
const title = value.title || 'Untitled';
|
|
111
|
-
if (titleElement && titleElement.textContent !== title) {
|
|
112
|
-
titleElement.textContent = title;
|
|
113
|
-
}
|
|
143
|
+
setOrRemoveAttribute(item, 'data-thread-id', value.thread_id || undefined);
|
|
114
144
|
}
|
|
115
145
|
}
|
|
116
146
|
|
|
117
147
|
// a click on a thread item means selection — a navigation action, not a
|
|
118
148
|
// content-engagement click: emit a select view event and skip click tracking
|
|
119
149
|
_onClick(event) {
|
|
120
|
-
if (event.target.closest(`[data-role="
|
|
121
|
-
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
if (event.target.closest(`[data-role="thread-rename"]`)) {
|
|
125
|
-
this._closeMenus();
|
|
126
|
-
const binding = this._bindings.get(event.target.closest(`[data-role="item"]`));
|
|
127
|
-
binding && this._requestRename(binding);
|
|
128
|
-
return;
|
|
150
|
+
if (event.target.closest(`[data-role="item-menu-button"], [data-role="item-menu"]`)) {
|
|
151
|
+
return; // the item's own context menu, owned by its container layout
|
|
129
152
|
}
|
|
130
|
-
if (event.target.closest(`[data-role="thread-delete"]`)) {
|
|
131
|
-
this._closeMenus();
|
|
132
|
-
const binding = this._bindings.get(event.target.closest(`[data-role="item"]`));
|
|
133
|
-
binding && this._requestDelete(binding);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (event.target.closest(`[data-role="thread-menu"]`)) {
|
|
137
|
-
return; // other clicks inside the menu don't select
|
|
138
|
-
}
|
|
139
|
-
this._closeMenus();
|
|
140
153
|
const element = event.target.closest(`[data-role="item"]`);
|
|
141
154
|
if (!element) {
|
|
142
155
|
return;
|
|
@@ -150,57 +163,4 @@ export default class ThreadsLayout extends CollectionLayout {
|
|
|
150
163
|
this._view._emit('select', { session, value, element: binding.element, domEvent: event });
|
|
151
164
|
}
|
|
152
165
|
|
|
153
|
-
// context menu //
|
|
154
|
-
_toggleMenu(item) {
|
|
155
|
-
const menu = item && item.querySelector(`[data-role="thread-menu"]`);
|
|
156
|
-
if (!menu) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
const open = menu.hidden;
|
|
160
|
-
this._closeMenus();
|
|
161
|
-
menu.hidden = !open;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
_closeMenus() {
|
|
165
|
-
const element = this._element;
|
|
166
|
-
if (!element) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
for (const menu of element.querySelectorAll(`[data-role="thread-menu"]:not([hidden])`)) {
|
|
170
|
-
menu.hidden = true;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// rename //
|
|
175
|
-
async _requestRename(binding) {
|
|
176
|
-
const { value, element } = binding;
|
|
177
|
-
const title = await prompt({
|
|
178
|
-
title: 'Rename thread',
|
|
179
|
-
value: value.title || '',
|
|
180
|
-
placeholder: 'Thread name',
|
|
181
|
-
confirmText: 'Rename',
|
|
182
|
-
});
|
|
183
|
-
if (!title || title === value.title) {
|
|
184
|
-
return; // cancelled, cleared, or unchanged
|
|
185
|
-
}
|
|
186
|
-
const { session } = this._view._state;
|
|
187
|
-
this._view._emit('rename', { session, value, element, title });
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// delete //
|
|
191
|
-
async _requestDelete(binding) {
|
|
192
|
-
const { value, element } = binding;
|
|
193
|
-
const confirmed = await confirm({
|
|
194
|
-
title: 'Delete thread',
|
|
195
|
-
message: `Are you sure you want to delete "${value.title || 'this thread'}"? This cannot be undone.`,
|
|
196
|
-
confirmText: 'Delete',
|
|
197
|
-
danger: true,
|
|
198
|
-
});
|
|
199
|
-
if (!confirmed) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
const { session } = this._view._state;
|
|
203
|
-
this._view._emit('delete', { session, value, element });
|
|
204
|
-
}
|
|
205
|
-
|
|
206
166
|
}
|
package/src/layout/templates.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { escapeHtml } from '@miso.ai/commons';
|
|
2
|
-
import {
|
|
2
|
+
import { isThreadUnread } from '@miso.ai/client-sdk-workflow';
|
|
3
3
|
import { getIcon } from '../asset/svgs.js';
|
|
4
4
|
import { ATTR_DATA_MISO_PRODUCT_ID } from '../constants.js';
|
|
5
5
|
|
|
@@ -32,65 +32,100 @@ export function question(layout, state, data) {
|
|
|
32
32
|
return `${openTag}${data.value || data.text || data}${closeTag}`;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
/**
|
|
36
|
+
* A thread item is a <miso-thread-item> container element hosting the thread
|
|
37
|
+
* item subworkflow — the threads layout is a shell: it assigns the workflow
|
|
38
|
+
* (and syncs the item-level attributes) in its post-render pass, and the
|
|
39
|
+
* content renders through the thread workflow's own layouts: the title role
|
|
40
|
+
* element in the body, plus the context menu (with the
|
|
41
|
+
* rename/subscription/delete role elements) inserted as chrome by the workflow's item-container layout. The
|
|
42
|
+
* item state renders as attributes right in the template (and stays in
|
|
43
|
+
* sync thereafter): the thread identity, the selection, and the unread
|
|
44
|
+
* mark.
|
|
45
|
+
*/
|
|
38
46
|
export function thread(layout, state, data) {
|
|
39
47
|
const { className, templates } = layout;
|
|
40
|
-
const threadId =
|
|
48
|
+
const threadId = data.thread_id;
|
|
41
49
|
const threadIdAttr = threadId ? ` data-thread-id="${threadId}"` : '';
|
|
42
50
|
const unreadAttr = isThreadUnread(data) ? ' data-unread' : '';
|
|
43
51
|
const selectedAttr = data.selected ? ' data-selected' : '';
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
return [
|
|
53
|
+
`<miso-thread-item class="${className}__item-body" data-role="item"${threadIdAttr}${unreadAttr}${selectedAttr}>`,
|
|
54
|
+
(templates.threadBody || threadBody)(layout, data),
|
|
55
|
+
`</miso-thread-item>`,
|
|
56
|
+
].join('');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function threadBody(layout, data) {
|
|
60
|
+
const { className, templates } = layout;
|
|
61
|
+
return `<miso-title class="${className}__title"></miso-title>` +
|
|
62
|
+
(templates.threadMenuBlock || threadMenuBlock)(layout, data);
|
|
46
63
|
}
|
|
47
64
|
|
|
65
|
+
// the item-menu data-roles are the contract with the thread workflow's
|
|
66
|
+
// item-container layout, which drives the menu's open/close behavior; the
|
|
67
|
+
// menu items are the thread workflow's own controls, rendered by the
|
|
68
|
+
// generic button layout with a prompt (rename) or a confirm (delete) dialog,
|
|
69
|
+
// and by the checkbox layout (the subscription toggle)
|
|
48
70
|
export function threadMenuBlock({ className }) {
|
|
49
|
-
return `<button type="button" class="${className}__menu-button" data-role="
|
|
50
|
-
`<div class="${className}__menu" data-role="
|
|
51
|
-
`<
|
|
52
|
-
`<
|
|
71
|
+
return `<button type="button" class="${className}__menu-button" data-role="item-menu-button" aria-label="Thread actions" aria-haspopup="menu">${getIcon('dots-vertical')}</button>` +
|
|
72
|
+
`<div class="${className}__menu" data-role="item-menu" role="menu" hidden>` +
|
|
73
|
+
`<miso-rename class="${className}__menu-item"></miso-rename>` +
|
|
74
|
+
`<miso-subscription class="${className}__menu-item"></miso-subscription>` +
|
|
75
|
+
`<miso-delete class="${className}__menu-item ${className}__menu-item--danger"></miso-delete>` +
|
|
53
76
|
`</div>`;
|
|
54
77
|
}
|
|
55
78
|
|
|
79
|
+
/**
|
|
80
|
+
* A message item is a <miso-message-item> container element hosting the message
|
|
81
|
+
* item subworkflow — the messages layout is a shell: it assigns the workflow
|
|
82
|
+
* (and the item-level attributes) in its post-render sync pass, and the role
|
|
83
|
+
* elements inside render the actual content through the message workflow's
|
|
84
|
+
* own layouts.
|
|
85
|
+
*/
|
|
56
86
|
export function message(layout, state, data) {
|
|
57
87
|
const { className, templates } = layout;
|
|
88
|
+
// miso-circled-citation-index covers both the citation links in the answer
|
|
89
|
+
// and the indexes on the source cards, as the hybrid-search UI scopes it
|
|
58
90
|
return [
|
|
59
|
-
`<
|
|
60
|
-
(templates.
|
|
61
|
-
|
|
62
|
-
`</div>`,
|
|
91
|
+
`<miso-message-item class="${className}__item-body miso-circled-citation-index" data-role="item">`,
|
|
92
|
+
(templates.messageBody || messageBody)(layout, data),
|
|
93
|
+
`</miso-message-item>`,
|
|
63
94
|
].join('');
|
|
64
95
|
}
|
|
65
96
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
97
|
+
// the regular content is packed together, so the erroneous presentation
|
|
98
|
+
// swaps it out wholesale for the error notice (the content wrapper hides by
|
|
99
|
+
// a class rule off the item's status — visible-when suits the notice, but
|
|
100
|
+
// not a wrapper: it forces `display: block !important` and only negates in
|
|
101
|
+
// combination with positive terms)
|
|
102
|
+
export function messageBody(layout) {
|
|
103
|
+
const { className, templates } = layout;
|
|
104
|
+
return `<div class="${className}__content">` +
|
|
105
|
+
`<miso-question class="${className}__question" hidden></miso-question>` +
|
|
106
|
+
`<miso-answer class="${className}__answer"></miso-answer>` +
|
|
107
|
+
`<miso-feedback class="${className}__feedback"></miso-feedback>` +
|
|
108
|
+
(templates.messageSourcesBlock || messageSourcesBlock)(layout) +
|
|
109
|
+
`</div>` +
|
|
110
|
+
(templates.messageErrorBlock || messageErrorBlock)(layout);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// the sources with their heading, as the hybrid-search UI presents them —
|
|
114
|
+
// shown once the item carries content (visible-when nonempty), so no
|
|
115
|
+
// orphan heading sits over a message still loading
|
|
116
|
+
export function messageSourcesBlock({ className }) {
|
|
117
|
+
return `<div class="${className}__sources-container" visible-when="nonempty">` +
|
|
118
|
+
`<h3 class="${className}__sources-phrase">Sources</h3>` +
|
|
119
|
+
`<miso-sources class="${className}__sources"></miso-sources>` +
|
|
120
|
+
`</div>`;
|
|
75
121
|
}
|
|
76
122
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
*/
|
|
82
|
-
export function messageAuthor(message) {
|
|
83
|
-
return getGeneratedBy(message) === 'answer_update_monitor' ? 'Written by Miso' : undefined;
|
|
123
|
+
// shown by the item's own status (visible-when): a message whose content
|
|
124
|
+
// fetch failed for good presents this in place of its content
|
|
125
|
+
export function messageErrorBlock({ className }) {
|
|
126
|
+
return `<div class="${className}__error" visible-when="erroneous">This message could not be loaded.</div>`;
|
|
84
127
|
}
|
|
85
128
|
|
|
86
|
-
export function messageAnswerBlock({ className }, { answer }) {
|
|
87
|
-
// the content is left blank here: the layout fills it in a second pass
|
|
88
|
-
// (markdown -> HTML); a spinner shows while the answer body is still
|
|
89
|
-
// being fetched from the answers API
|
|
90
|
-
const content = answer === undefined ?
|
|
91
|
-
`<div class="miso-loading" aria-label="Loading" data-role="loading"></div>` : '';
|
|
92
|
-
return `<div class="${className}__answer miso-markdown miso-circled-citation-index" data-role="answer">${content}</div>`;
|
|
93
|
-
}
|
|
94
129
|
|
|
95
130
|
export function article(layout, state, data, meta) {
|
|
96
131
|
const { templates } = layout;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { escapeHtml } from '@miso.ai/commons';
|
|
2
|
+
import { isUpdateMessage } from '@miso.ai/client-sdk-workflow';
|
|
3
|
+
import { LAYOUT_TYPE } from '../../constants.js';
|
|
4
|
+
import { setOrRemoveAttribute } from '../../util/dom.js';
|
|
5
|
+
import TextLayout from './text.js';
|
|
6
|
+
|
|
7
|
+
const TYPE = LAYOUT_TYPE.QUESTION;
|
|
8
|
+
const DEFAULT_CLASSNAME = 'miso-question';
|
|
9
|
+
|
|
10
|
+
// the author label: only a question written by the answer-updates monitor is
|
|
11
|
+
// labeled — the user's own questions carry none
|
|
12
|
+
function defaultAuthor(message) {
|
|
13
|
+
return isUpdateMessage(message) ? 'Written by Miso' : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function content(layout, { value }) {
|
|
17
|
+
const question = value && value.question;
|
|
18
|
+
return question ? escapeHtml(question) : '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The question of a message: a text layout taking the whole message record —
|
|
23
|
+
* the text is the `question` field, and the authorship is stamped as data
|
|
24
|
+
* attributes on the host element (`data-author` drives the CSS label badge,
|
|
25
|
+
* `data-generated-by` the bubble color), re-applied on every render, since
|
|
26
|
+
* the metadata arrives with the answers response, after the stub render. The
|
|
27
|
+
* host element hides while there is no question text to show. The author
|
|
28
|
+
* label is configurable via the `author` option (message -> label text).
|
|
29
|
+
*/
|
|
30
|
+
export default class QuestionLayout extends TextLayout {
|
|
31
|
+
|
|
32
|
+
static get type() {
|
|
33
|
+
return TYPE;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static get defaultClassName() {
|
|
37
|
+
return DEFAULT_CLASSNAME;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
constructor({ className = DEFAULT_CLASSNAME, tag = 'div', author = defaultAuthor, templates, ...options } = {}) {
|
|
41
|
+
super({
|
|
42
|
+
className,
|
|
43
|
+
tag,
|
|
44
|
+
author,
|
|
45
|
+
templates: { content, ...templates },
|
|
46
|
+
...options,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_afterRender(element, state) {
|
|
51
|
+
super._afterRender(element, state);
|
|
52
|
+
const message = state.value || {};
|
|
53
|
+
element.hidden = !message.question;
|
|
54
|
+
setOrRemoveAttribute(element, 'data-author', this.options.author(message));
|
|
55
|
+
setOrRemoveAttribute(element, 'data-generated-by', (message.metadata && message.metadata.miso_generated_by) || undefined);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { trimObj, defineValues, Resolution, pacer } from '@miso.ai/commons';
|
|
2
2
|
import { STATUS, LAYOUT_TYPE } from '../../../constants.js';
|
|
3
|
+
import { dumpElementAttributes } from '../../../util/dom.js';
|
|
3
4
|
import ProgressiveLayout from '../../progressive.js';
|
|
4
5
|
import PlaintextRenderer from './plaintext.js';
|
|
5
6
|
import { containerElement, cursorClassName, fromSameSession, normalizeOnDebug } from './utils.js';
|
|
@@ -137,6 +138,15 @@ export default class TypewriterLayout extends ProgressiveLayout {
|
|
|
137
138
|
// new typing streak
|
|
138
139
|
result = this._renderer.clear(container, rendered);
|
|
139
140
|
} else {
|
|
141
|
+
result = rendered;
|
|
142
|
+
}
|
|
143
|
+
if (this.options.instant && state.done && !result.cursor) {
|
|
144
|
+
// instant mode: content that finishes before any of it was typed —
|
|
145
|
+
// arrived complete (e.g. a loaded conversation message), or completed
|
|
146
|
+
// while the cursor was still at the start — renders in one shot; only
|
|
147
|
+
// content streaming in as it is generated types out
|
|
148
|
+
result = this._renderer.update(container, result, { ...state, cursor: Infinity });
|
|
149
|
+
} else if (!newStreak) {
|
|
140
150
|
const cursor = this._getNextCursor(rendered.cursor, state.doneAt, rendered.timestamp, state.timestamp);
|
|
141
151
|
result = this._renderer.update(container, rendered, { ...state, cursor });
|
|
142
152
|
}
|
|
@@ -195,18 +205,3 @@ export default class TypewriterLayout extends ProgressiveLayout {
|
|
|
195
205
|
}
|
|
196
206
|
}
|
|
197
207
|
}
|
|
198
|
-
|
|
199
|
-
function dumpElementAttributes(element) {
|
|
200
|
-
const { attributes } = element;
|
|
201
|
-
const attrs = {};
|
|
202
|
-
for (const { name, value } of attributes) {
|
|
203
|
-
if (name.startsWith('data-')) {
|
|
204
|
-
attrs[name] = value;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
try {
|
|
208
|
-
return JSON.stringify(attrs);
|
|
209
|
-
} catch (e) {
|
|
210
|
-
return '{}';
|
|
211
|
-
}
|
|
212
|
-
}
|
package/src/util/dom.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
// data-* attributes of an element as a JSON string, for click interactions
|
|
2
|
+
export function dumpElementAttributes(element) {
|
|
3
|
+
const { attributes } = element;
|
|
4
|
+
const attrs = {};
|
|
5
|
+
for (const { name, value } of attributes) {
|
|
6
|
+
if (name.startsWith('data-')) {
|
|
7
|
+
attrs[name] = value;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
return JSON.stringify(attrs);
|
|
12
|
+
} catch (e) {
|
|
13
|
+
return '{}';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
1
17
|
export function setOrRemoveAttribute(element, name, value) {
|
|
2
18
|
if (value === undefined) {
|
|
3
19
|
element.removeAttribute(name);
|
package/src/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '1.14.0-beta.
|
|
1
|
+
export default '1.14.0-beta.3';
|