@dxos/react-ui-chat 0.8.4-main.84f28bd
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/LICENSE +8 -0
- package/README.md +1 -0
- package/dist/lib/browser/index.mjs +480 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +482 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.d.ts +34 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.d.ts.map +1 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.stories.d.ts +8 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.stories.d.ts.map +1 -0
- package/dist/types/src/components/ChatDialog/index.d.ts +2 -0
- package/dist/types/src/components/ChatDialog/index.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.d.ts +21 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.stories.d.ts +10 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/autocomplete.d.ts +25 -0
- package/dist/types/src/components/ChatEditor/autocomplete.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/index.d.ts +3 -0
- package/dist/types/src/components/ChatEditor/index.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/references.d.ts +30 -0
- package/dist/types/src/components/ChatEditor/references.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +3 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +40 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +74 -0
- package/src/components/ChatDialog/ChatDialog.stories.tsx +65 -0
- package/src/components/ChatDialog/ChatDialog.tsx +193 -0
- package/src/components/ChatDialog/index.ts +5 -0
- package/src/components/ChatEditor/ChatEditor.stories.tsx +96 -0
- package/src/components/ChatEditor/ChatEditor.tsx +91 -0
- package/src/components/ChatEditor/autocomplete.ts +218 -0
- package/src/components/ChatEditor/index.ts +7 -0
- package/src/components/ChatEditor/references.ts +175 -0
- package/src/components/index.ts +6 -0
- package/src/index.ts +5 -0
- package/src/translations.ts +54 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Prec, type Extension } from '@codemirror/state';
|
|
6
|
+
import {
|
|
7
|
+
EditorView,
|
|
8
|
+
Decoration,
|
|
9
|
+
ViewPlugin,
|
|
10
|
+
keymap,
|
|
11
|
+
type DecorationSet,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
WidgetType,
|
|
14
|
+
} from '@codemirror/view';
|
|
15
|
+
|
|
16
|
+
export type AutocompleteOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* Callback triggered when Enter is pressed.
|
|
19
|
+
* @param text The current text in the editor
|
|
20
|
+
* @returns true if the editor should reset the document.
|
|
21
|
+
*/
|
|
22
|
+
onSubmit?: (text: string) => boolean | void;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Function that returns a list of suggestions based on the current text.
|
|
26
|
+
* @param text The current text before the cursor
|
|
27
|
+
* @returns Array of suggestion strings
|
|
28
|
+
*/
|
|
29
|
+
onSuggest?: (text: string) => string[];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* ESC pressed.
|
|
33
|
+
*/
|
|
34
|
+
onCancel?: () => void;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates an autocomplete extension that shows inline suggestions.
|
|
39
|
+
* Pressing Tab will complete the suggestion.
|
|
40
|
+
*/
|
|
41
|
+
// TODO(burdon): Move to react-ui-editor.
|
|
42
|
+
export const autocomplete = ({ onSubmit, onSuggest, onCancel }: AutocompleteOptions): Extension => {
|
|
43
|
+
const suggest = ViewPlugin.fromClass(
|
|
44
|
+
class {
|
|
45
|
+
_decorations: DecorationSet;
|
|
46
|
+
_currentSuggestion: string | null = null;
|
|
47
|
+
|
|
48
|
+
constructor(view: EditorView) {
|
|
49
|
+
this._decorations = this.computeDecorations(view);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
update(update: ViewUpdate) {
|
|
53
|
+
if (update.docChanged || update.selectionSet) {
|
|
54
|
+
this._decorations = this.computeDecorations(update.view);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private computeDecorations(view: EditorView): DecorationSet {
|
|
59
|
+
const text = view.state.doc.toString();
|
|
60
|
+
const suggestions = onSuggest?.(text) ?? [];
|
|
61
|
+
if (!suggestions.length) {
|
|
62
|
+
this._currentSuggestion = null;
|
|
63
|
+
return Decoration.none;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Get the first suggestion.
|
|
67
|
+
this._currentSuggestion = suggestions[0];
|
|
68
|
+
const suffix = this._currentSuggestion.slice(text.length);
|
|
69
|
+
if (!suffix) {
|
|
70
|
+
return Decoration.none;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Always show ghost text at the end of the document.
|
|
74
|
+
return Decoration.set([
|
|
75
|
+
Decoration.widget({
|
|
76
|
+
widget: new InlineSuggestionWidget(suffix),
|
|
77
|
+
side: 1,
|
|
78
|
+
}).range(view.state.doc.length),
|
|
79
|
+
]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
completeSuggestion(view: EditorView): boolean {
|
|
83
|
+
if (!this._currentSuggestion) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const text = view.state.doc.toString();
|
|
88
|
+
const suffix = this._currentSuggestion.slice(text.length);
|
|
89
|
+
if (!suffix) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
view.dispatch({
|
|
94
|
+
changes: {
|
|
95
|
+
from: view.state.doc.length,
|
|
96
|
+
insert: suffix,
|
|
97
|
+
},
|
|
98
|
+
selection: {
|
|
99
|
+
anchor: view.state.doc.length + suffix.length,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
decorations: (v) => v._decorations,
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
return [
|
|
112
|
+
suggest,
|
|
113
|
+
EditorView.theme({
|
|
114
|
+
'.cm-inline-suggestion': {
|
|
115
|
+
opacity: 0.4,
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
|
|
119
|
+
Prec.highest(
|
|
120
|
+
keymap.of([
|
|
121
|
+
{
|
|
122
|
+
key: 'Tab',
|
|
123
|
+
preventDefault: true,
|
|
124
|
+
run: (view) => {
|
|
125
|
+
const plugin = view.plugin(suggest);
|
|
126
|
+
return plugin?.completeSuggestion(view) ?? false;
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
key: 'ArrowRight',
|
|
131
|
+
preventDefault: true,
|
|
132
|
+
run: (view) => {
|
|
133
|
+
// Only complete if cursor is at the end
|
|
134
|
+
if (view.state.selection.main.head !== view.state.doc.length) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const plugin = view.plugin(suggest);
|
|
139
|
+
return plugin?.completeSuggestion(view) ?? false;
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
key: 'Enter',
|
|
144
|
+
preventDefault: true,
|
|
145
|
+
run: (view) => {
|
|
146
|
+
const text = view.state.doc.toString().trim();
|
|
147
|
+
if (text.length > 0 && onSubmit) {
|
|
148
|
+
const reset = onSubmit(text);
|
|
149
|
+
|
|
150
|
+
// Clear the document after calling onEnter.
|
|
151
|
+
if (reset) {
|
|
152
|
+
view.dispatch({
|
|
153
|
+
changes: {
|
|
154
|
+
from: 0,
|
|
155
|
+
to: view.state.doc.length,
|
|
156
|
+
insert: '',
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return true;
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
key: 'Shift-Enter',
|
|
167
|
+
preventDefault: true,
|
|
168
|
+
run: (view) => {
|
|
169
|
+
view.dispatch({
|
|
170
|
+
changes: {
|
|
171
|
+
from: view.state.selection.main.head,
|
|
172
|
+
insert: '\n',
|
|
173
|
+
},
|
|
174
|
+
selection: {
|
|
175
|
+
anchor: view.state.selection.main.head + 1,
|
|
176
|
+
head: view.state.selection.main.head + 1,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
return true;
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
key: 'Escape',
|
|
184
|
+
preventDefault: true,
|
|
185
|
+
run: (view) => {
|
|
186
|
+
// Clear the entire document.
|
|
187
|
+
view.dispatch({
|
|
188
|
+
changes: {
|
|
189
|
+
from: 0,
|
|
190
|
+
to: view.state.doc.length,
|
|
191
|
+
insert: '',
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
onCancel?.();
|
|
195
|
+
return true;
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
]),
|
|
199
|
+
),
|
|
200
|
+
];
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
class InlineSuggestionWidget extends WidgetType {
|
|
204
|
+
constructor(private suffix: string) {
|
|
205
|
+
super();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
override toDOM(): HTMLSpanElement {
|
|
209
|
+
const span = document.createElement('span');
|
|
210
|
+
span.textContent = this.suffix;
|
|
211
|
+
span.className = 'cm-inline-suggestion';
|
|
212
|
+
return span;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
override eq(other: InlineSuggestionWidget): boolean {
|
|
216
|
+
return other.suffix === this.suffix;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { autocompletion, completionKeymap, type CompletionResult } from '@codemirror/autocomplete';
|
|
6
|
+
import { type Extension, RangeSet } from '@codemirror/state';
|
|
7
|
+
import {
|
|
8
|
+
Decoration,
|
|
9
|
+
type DecorationSet,
|
|
10
|
+
EditorView,
|
|
11
|
+
ViewPlugin,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
WidgetType,
|
|
14
|
+
keymap,
|
|
15
|
+
} from '@codemirror/view';
|
|
16
|
+
|
|
17
|
+
import { Mutex } from '@dxos/async';
|
|
18
|
+
import { invariant } from '@dxos/invariant';
|
|
19
|
+
|
|
20
|
+
// TODO(dmaretskyi): Consider adding details renderer for when you hover over the reference.
|
|
21
|
+
export type ReferenceData = {
|
|
22
|
+
uri: string;
|
|
23
|
+
label: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface ReferencesProvider {
|
|
27
|
+
getReferences({ query }: { query: string }): Promise<ReferenceData[]>;
|
|
28
|
+
resolveReference({ uri }: { uri: string }): Promise<ReferenceData | null>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ReferencesOptions = {
|
|
32
|
+
provider: ReferencesProvider;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @default '@'
|
|
36
|
+
*/
|
|
37
|
+
triggerCharacter?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Prevent the autocomplete from closing when the user blurs the editor.
|
|
41
|
+
* @default false
|
|
42
|
+
*/
|
|
43
|
+
debug?: boolean;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Lookup object references.
|
|
48
|
+
*/
|
|
49
|
+
export const references = ({ provider, triggerCharacter = '@', debug = false }: ReferencesOptions): Extension => {
|
|
50
|
+
invariant(triggerCharacter.length === 1);
|
|
51
|
+
|
|
52
|
+
const decorationField = ViewPlugin.fromClass(
|
|
53
|
+
class ReferenceView {
|
|
54
|
+
// TODO(burdon): Why?
|
|
55
|
+
private _mutex = new Mutex();
|
|
56
|
+
|
|
57
|
+
decorations: DecorationSet = Decoration.set([]);
|
|
58
|
+
|
|
59
|
+
constructor(view: EditorView) {
|
|
60
|
+
queueMicrotask(async () => {
|
|
61
|
+
const guard = await this._mutex.acquire();
|
|
62
|
+
try {
|
|
63
|
+
this.decorations = await this._computeDecorations(view);
|
|
64
|
+
} finally {
|
|
65
|
+
guard.release();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
update(update: ViewUpdate) {
|
|
71
|
+
if (update.docChanged) {
|
|
72
|
+
queueMicrotask(async () => {
|
|
73
|
+
const guard = await this._mutex.acquire();
|
|
74
|
+
try {
|
|
75
|
+
this.decorations = await this._computeDecorations(update.view);
|
|
76
|
+
} finally {
|
|
77
|
+
guard.release();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private async _computeDecorations(view: EditorView): Promise<DecorationSet> {
|
|
84
|
+
const text = view.state.doc.toString();
|
|
85
|
+
const references = text.matchAll(new RegExp(`${triggerCharacter}[a-zA-Z0-9@:]+\\s`, 'g'));
|
|
86
|
+
|
|
87
|
+
const decorations = [];
|
|
88
|
+
for (const match of references) {
|
|
89
|
+
const reference = match[0];
|
|
90
|
+
const uri = reference.slice(1, -1);
|
|
91
|
+
const data = await provider.resolveReference({ uri });
|
|
92
|
+
if (data) {
|
|
93
|
+
decorations.push(
|
|
94
|
+
Decoration.replace({
|
|
95
|
+
widget: new ReferenceWidget(data),
|
|
96
|
+
}).range(match.index!, match.index! + reference.length),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return Decoration.set(decorations);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
decorations: (v) => v.decorations,
|
|
106
|
+
provide: (plugin) => [
|
|
107
|
+
EditorView.atomicRanges.of(
|
|
108
|
+
(view): DecorationSet => view.plugin(decorationField)?.decorations ?? RangeSet.empty,
|
|
109
|
+
),
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return [
|
|
115
|
+
decorationField,
|
|
116
|
+
|
|
117
|
+
EditorView.theme({
|
|
118
|
+
'.cm-reference-pill': {
|
|
119
|
+
borderRadius: '0.25rem',
|
|
120
|
+
borderWidth: '1px',
|
|
121
|
+
marginRight: '0.25rem',
|
|
122
|
+
marginLeft: '0.25rem',
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
|
|
126
|
+
autocompletion({
|
|
127
|
+
tooltipClass: () => 'shadow rounded',
|
|
128
|
+
activateOnTyping: true,
|
|
129
|
+
aboveCursor: true,
|
|
130
|
+
closeOnBlur: !debug,
|
|
131
|
+
override: [
|
|
132
|
+
async (context): Promise<CompletionResult | null> => {
|
|
133
|
+
const match = context.matchBefore(new RegExp(`${triggerCharacter}[a-zA-Z0-9]+`));
|
|
134
|
+
if (!match || match?.to === match?.from) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const query = match.text.slice(1);
|
|
139
|
+
const references = await provider.getReferences({ query });
|
|
140
|
+
return {
|
|
141
|
+
from: match.from,
|
|
142
|
+
filter: false,
|
|
143
|
+
options: references.map((reference) => ({
|
|
144
|
+
label: reference.label,
|
|
145
|
+
apply: `${triggerCharacter}${reference.uri} `,
|
|
146
|
+
})),
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
}),
|
|
151
|
+
|
|
152
|
+
keymap.of(completionKeymap),
|
|
153
|
+
];
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
class ReferenceWidget extends WidgetType {
|
|
157
|
+
constructor(private data: ReferenceData) {
|
|
158
|
+
super();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
override toDOM(): HTMLSpanElement {
|
|
162
|
+
const span = document.createElement('span');
|
|
163
|
+
span.textContent = `@ ${this.data.label}`;
|
|
164
|
+
span.className = 'cm-reference-pill';
|
|
165
|
+
return span;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
override eq(other: ReferenceWidget): boolean {
|
|
169
|
+
return other.data.uri === this.data.uri;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
override ignoreEvent(): boolean {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Resource } from '@dxos/react-ui';
|
|
6
|
+
|
|
7
|
+
export const translationKey = 'react-ui-chat';
|
|
8
|
+
|
|
9
|
+
export const translations = [
|
|
10
|
+
{
|
|
11
|
+
'en-US': {
|
|
12
|
+
[translationKey]: {
|
|
13
|
+
'templates label': 'Templates',
|
|
14
|
+
'open ambient chat label': 'Open Assistant',
|
|
15
|
+
'assistant chat label': 'Assistant',
|
|
16
|
+
'plugin name': 'Assistant',
|
|
17
|
+
'object placeholder': 'New prompt',
|
|
18
|
+
'create object label': 'Create prompt',
|
|
19
|
+
'create trigger label': 'Create trigger',
|
|
20
|
+
'create stack section label': 'Create prompt',
|
|
21
|
+
'command placeholder': 'Enter slash command...',
|
|
22
|
+
'template placeholder': 'Enter template...',
|
|
23
|
+
'value placeholder': 'Enter value...',
|
|
24
|
+
'prompt rules label': 'Prompt Rules',
|
|
25
|
+
'typename placeholder': 'Enter typename of objects which this template is for',
|
|
26
|
+
'description placeholder': 'Enter description of when this template should be used',
|
|
27
|
+
'select preset template placeholder': 'Select preset',
|
|
28
|
+
'service registry label': 'Service Registry',
|
|
29
|
+
|
|
30
|
+
'sequence logs label': 'Logs',
|
|
31
|
+
|
|
32
|
+
'assistant dialog title': 'Assistant',
|
|
33
|
+
'open assistant label': 'Open assistant',
|
|
34
|
+
|
|
35
|
+
'search input placeholder': 'Search...',
|
|
36
|
+
'chat input placeholder': 'Ask a question...',
|
|
37
|
+
'chat stop': 'Cancel request.',
|
|
38
|
+
|
|
39
|
+
'button save': 'Save',
|
|
40
|
+
'button run': 'Run',
|
|
41
|
+
|
|
42
|
+
'prompt placeholder': 'Ask a question...',
|
|
43
|
+
'microphone button': 'Click to speak',
|
|
44
|
+
'cancel processing button': 'Stop processing',
|
|
45
|
+
|
|
46
|
+
'settings default label': 'Default',
|
|
47
|
+
'settings custom prompts label': 'Use custom prompts',
|
|
48
|
+
'settings llm provider label': 'LLM provider',
|
|
49
|
+
'settings edge llm model label': 'Remote language model',
|
|
50
|
+
'settings ollama llm model label': 'Ollama language model',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
] as const satisfies Resource[];
|