@gramlot/gramlot 0.3.41 → 0.5.0
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/assets/icons/align-center.svg +4 -0
- package/assets/icons/align-justify.svg +4 -0
- package/assets/icons/align-left.svg +4 -0
- package/assets/icons/align-right.svg +4 -0
- package/assets/icons/applications.svg +4 -0
- package/assets/icons/archive.svg +4 -0
- package/assets/icons/attachment.svg +4 -0
- package/assets/icons/bold.svg +4 -0
- package/assets/icons/book.svg +4 -0
- package/assets/icons/bookmark.svg +4 -0
- package/assets/icons/calendar.svg +4 -0
- package/assets/icons/cancel.svg +4 -0
- package/assets/icons/chart.svg +4 -1
- package/assets/icons/chat.svg +4 -0
- package/assets/icons/clock.svg +4 -0
- package/assets/icons/code.svg +4 -0
- package/assets/icons/collapse.svg +4 -0
- package/assets/icons/columns.svg +4 -0
- package/assets/icons/confirm.svg +4 -0
- package/assets/icons/copy.svg +4 -0
- package/assets/icons/cut.svg +4 -0
- package/assets/icons/database.svg +4 -0
- package/assets/icons/delete.svg +4 -0
- package/assets/icons/detach.svg +4 -0
- package/assets/icons/document.svg +4 -0
- package/assets/icons/download.svg +4 -0
- package/assets/icons/duplicate.svg +4 -0
- package/assets/icons/edit.svg +4 -0
- package/assets/icons/eraser.svg +4 -0
- package/assets/icons/expand.svg +4 -0
- package/assets/icons/export.svg +4 -0
- package/assets/icons/eye.svg +4 -0
- package/assets/icons/filter.svg +4 -0
- package/assets/icons/first.svg +4 -0
- package/assets/icons/folder.svg +4 -0
- package/assets/icons/group.svg +4 -0
- package/assets/icons/help.svg +4 -0
- package/assets/icons/home.svg +4 -0
- package/assets/icons/horizontal-rule.svg +4 -0
- package/assets/icons/image.svg +4 -0
- package/assets/icons/import.svg +4 -0
- package/assets/icons/indent.svg +4 -0
- package/assets/icons/info.svg +4 -0
- package/assets/icons/inspector.svg +4 -0
- package/assets/icons/italic.svg +4 -0
- package/assets/icons/last.svg +4 -0
- package/assets/icons/layers.svg +4 -0
- package/assets/icons/link.svg +4 -0
- package/assets/icons/list-bulleted.svg +4 -0
- package/assets/icons/list-numbered.svg +4 -0
- package/assets/icons/list.svg +4 -0
- package/assets/icons/lock.svg +4 -0
- package/assets/icons/logout.svg +4 -0
- package/assets/icons/navigation.svg +4 -0
- package/assets/icons/new.svg +4 -0
- package/assets/icons/next.svg +4 -0
- package/assets/icons/open.svg +4 -0
- package/assets/icons/outdent.svg +4 -0
- package/assets/icons/palette.svg +4 -0
- package/assets/icons/panel-bottom.svg +4 -0
- package/assets/icons/panel-left.svg +4 -0
- package/assets/icons/panel-right.svg +4 -0
- package/assets/icons/panel-top.svg +4 -0
- package/assets/icons/paragraph.svg +4 -0
- package/assets/icons/paste.svg +4 -0
- package/assets/icons/pause.svg +4 -0
- package/assets/icons/pin.svg +4 -0
- package/assets/icons/play.svg +4 -0
- package/assets/icons/previous.svg +4 -0
- package/assets/icons/print.svg +4 -0
- package/assets/icons/redo.svg +4 -0
- package/assets/icons/refresh.svg +4 -0
- package/assets/icons/save.svg +4 -0
- package/assets/icons/search.svg +4 -0
- package/assets/icons/select-all.svg +4 -0
- package/assets/icons/send.svg +4 -0
- package/assets/icons/settings.svg +4 -0
- package/assets/icons/sort-ascending.svg +4 -0
- package/assets/icons/sort-descending.svg +4 -0
- package/assets/icons/stop.svg +4 -0
- package/assets/icons/strikethrough.svg +4 -0
- package/assets/icons/table.svg +4 -0
- package/assets/icons/tree.svg +4 -0
- package/assets/icons/underline.svg +4 -0
- package/assets/icons/undo.svg +4 -0
- package/assets/icons/unlock.svg +4 -0
- package/assets/icons/upload.svg +4 -0
- package/assets/icons/user.svg +4 -0
- package/assets/icons/warning.svg +4 -0
- package/assets/icons/zoom-in.svg +4 -0
- package/assets/icons/zoom-out.svg +4 -0
- package/dist/components/charts/__init__.py +1 -1
- package/dist/components/charts/assets/index.js +1 -1
- package/dist/components/charts/assets/libraries/vega.js +11 -11
- package/dist/components/charts/grammar.json +2032 -72
- package/dist/components/editors/__init__.py +1 -1
- package/dist/components/editors/assets/collection.js +1 -1
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/editors/assets/libraries/codemirror.js +30 -31
- package/dist/components/editors/assets/libraries/jodit.js +453 -447
- package/dist/components/editors/commands.json +113 -1
- package/dist/components/editors/grammar.json +12 -5
- package/dist/components/grid/__init__.py +1 -1
- package/dist/components/grid/assets/index.js +1 -1
- package/dist/components/grid/commands.json +4 -6
- package/dist/components/grid/grammar.json +112 -0
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/inputs/grammar.json +5165 -359
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +1 -0
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/layout/grammar.json +433 -4
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/components/tree/grammar.json +51 -0
- package/dist/components/typst/__init__.py +1 -1
- package/dist/components/typst/assets/collection.js +6 -6
- package/dist/components/typst/assets/index.js +1 -1
- package/dist/components/uploads/__init__.py +1 -1
- package/dist/components/uploads/assets/index.js +1 -1
- package/dist/components/uploads/grammar.json +66 -0
- package/dist/declared-collections.js +1 -1
- package/dist/gramlot.js +5486 -2029
- package/dist/gramlot.min.js +361 -138
- package/dist/libraries.json +6 -4
- package/dist/standard-collections.js +1 -1
- package/dist/standard-grammar.js +1 -1
- package/package.json +6 -2
- package/src/binding/ask-dialog.js +17 -3
- package/src/binding/providers.js +3 -2
- package/src/binding/remote.js +41 -6
- package/src/binding/runtime.js +30 -3
- package/src/builder/gramlot-builder.d.ts +3 -1
- package/src/builder/gramlot-builder.js +77 -26
- package/src/builder/source.js +78 -15
- package/src/builder/xml-recipe.js +144 -0
- package/src/chat-file-types.js +13 -0
- package/src/chat.d.ts +1 -0
- package/src/chat.js +35 -25
- package/src/commands.d.ts +1 -1
- package/src/commands.js +1 -1
- package/src/components/border-container.js +64 -6
- package/src/components/buttons.js +26 -31
- package/src/components/chart-bar3d-options.js +0 -1
- package/src/components/chart-boxplot-options.js +1 -1
- package/src/components/chart-candlestick-options.js +1 -1
- package/src/components/chart-engines/echarts-options.js +6 -5
- package/src/components/chart-engines/echarts.js +27 -10
- package/src/components/chart-engines/plotly-map.js +1 -1
- package/src/components/chart-engines/plotly.js +21 -14
- package/src/components/chart-engines/uplot.js +17 -11
- package/src/components/chart-engines/vega.js +17 -11
- package/src/components/chart-funnel-options.js +1 -1
- package/src/components/chart-geographic-options.js +2 -2
- package/src/components/chart-heatmap-options.js +1 -1
- package/src/components/chart-map-bar3d-options.js +2 -2
- package/src/components/chart-map-options.js +2 -2
- package/src/components/chart-pane.js +5 -5
- package/src/components/chart-parallel-options.js +1 -1
- package/src/components/chart-radar-options.js +1 -1
- package/src/components/chart-renderer.js +6 -1
- package/src/components/chart-sankey-options.js +1 -1
- package/src/components/chart-spatial-options.js +6 -2
- package/src/components/chart-sunburst-options.js +13 -1
- package/src/components/chart-theme.js +14 -0
- package/src/components/chart-treemap-options.js +1 -1
- package/src/components/chart-type-data.js +24 -4
- package/src/components/charts.js +7 -6
- package/src/components/check-box-text-editor.js +1 -0
- package/src/components/check-box-text.js +2 -0
- package/src/components/check-box-tree.js +1 -1
- package/src/components/choice-editor.js +5 -4
- package/src/components/choice-field-styles.js +9 -1
- package/src/components/code-mirror-engine.js +19 -5
- package/src/components/code-mirror-gramlot.js +85 -0
- package/src/components/code-mirror.js +3 -2
- package/src/components/drawing.js +43 -14
- package/src/components/editor-commands.js +69 -0
- package/src/components/editor-options.js +1 -1
- package/src/components/editors.js +5 -3
- package/src/components/form-tray-pane.js +18 -0
- package/src/components/graphical-ranges.js +190 -0
- package/src/components/grid-check-sets.js +39 -3
- package/src/components/grid-footer.js +6 -1
- package/src/components/grid-header-interaction.js +3 -3
- package/src/components/grid-styles.js +30 -5
- package/src/components/grid-tools.js +15 -7
- package/src/components/grid.js +109 -15
- package/src/components/html-editor-commands.js +61 -0
- package/src/components/html-editor.js +18 -3
- package/src/components/inputs.js +94 -23
- package/src/components/jodit-engine.js +110 -7
- package/src/components/layout.js +3 -2
- package/src/components/menus.js +89 -26
- package/src/components/numeric-range.js +39 -0
- package/src/components/selected-container.js +123 -64
- package/src/components/tab-bar.js +389 -0
- package/src/components/toolbar-icon-style.js +22 -0
- package/src/components/toolbar-overflow.js +74 -12
- package/src/components/tray-base.js +528 -0
- package/src/components/tray-pane.js +41 -518
- package/src/components/tree.js +56 -4
- package/src/components/typed-editor.js +5 -2
- package/src/components/typst-preview.js +3 -3
- package/src/components/upload-blocks.js +88 -0
- package/src/components/upload-engine.js +21 -31
- package/src/components/uploader.js +168 -47
- package/src/editing-session.js +2 -1
- package/src/gramlot.js +6 -3
- package/src/handlers/dom-handler.d.ts +2 -0
- package/src/handlers/dom-handler.js +9 -0
- package/src/handlers/rpc-handler.d.ts +1 -1
- package/src/handlers/rpc-handler.js +2 -0
- package/src/handlers/source-handler.d.ts +5 -3
- package/src/handlers/source-handler.js +31 -13
- package/src/icons.js +84 -0
- package/src/ide-runtime.js +3 -1
- package/src/index-page-runtime.js +20 -5
- package/src/index-page-styles.js +1 -1
- package/src/index-theme.js +210 -3
- package/src/index.d.ts +7 -2
- package/src/index.js +7 -2
- package/src/libraries.js +4 -4
- package/src/object-extract.d.ts +7 -0
- package/src/object-extract.js +31 -0
- package/src/renderer/attributes.js +1 -1
- package/src/renderer/gramlot-renderer.d.ts +2 -2
- package/src/renderer/gramlot-renderer.js +4 -1
- package/src/server/chat-attachments.js +139 -0
- package/src/server/chat.d.ts +33 -0
- package/src/server/chat.js +168 -0
- package/src/server/database-selectors.d.ts +6 -0
- package/src/server/database-selectors.js +13 -0
- package/src/server/gramlot-file-server.js +6 -1
- package/src/server/gramlot-server.d.ts +19 -3
- package/src/server/gramlot-server.js +149 -19
- package/src/server/index-page.d.ts +10 -0
- package/src/server/index-page.js +92 -8
- package/src/server/index.d.ts +1 -1
- package/src/server/index.js +1 -1
- package/src/server/page-base.js +4 -0
- package/src/server/page-store.d.ts +21 -0
- package/src/server/page-store.js +219 -0
- package/src/server/page.d.ts +6 -1
- package/src/server/page.js +5 -1
- package/src/server/store-recipe.js +102 -0
- package/src/server/toolbar-icon-name.d.ts +93 -0
- package/src/server/upload.d.ts +2 -0
- package/src/server/upload.js +36 -18
- package/src/server/visual-editor.js +4 -2
- package/src/tree-store.d.ts +5 -1
- package/src/tree-store.js +11 -2
- package/src/view/choice-focus.js +17 -0
- package/src/view/html.js +21 -1
- package/src/view/labelbox.js +6 -1
- package/src/view/menu.js +9 -3
- package/src/visual-editor.d.ts +1 -0
- package/src/visual-editor.js +10 -5
- package/themes/gramlot-base/theme.css +60 -28
- package/themes/gramlot-compact/theme.css +2 -0
- package/themes/gramlot-index/icons/align-center.svg +4 -0
- package/themes/gramlot-index/icons/align-justify.svg +4 -0
- package/themes/gramlot-index/icons/align-left.svg +4 -0
- package/themes/gramlot-index/icons/align-right.svg +4 -0
- package/themes/gramlot-index/icons/applications.svg +4 -0
- package/themes/gramlot-index/icons/archive.svg +4 -0
- package/themes/gramlot-index/icons/attachment.svg +4 -0
- package/themes/gramlot-index/icons/bold.svg +4 -0
- package/themes/gramlot-index/icons/book.svg +4 -0
- package/themes/gramlot-index/icons/bookmark.svg +4 -0
- package/themes/gramlot-index/icons/calendar.svg +4 -0
- package/themes/gramlot-index/icons/cancel.svg +4 -0
- package/themes/gramlot-index/icons/chart.svg +4 -0
- package/themes/gramlot-index/icons/chat.svg +4 -0
- package/themes/gramlot-index/icons/clock.svg +4 -0
- package/themes/gramlot-index/icons/code.svg +4 -0
- package/themes/gramlot-index/icons/collapse.svg +4 -0
- package/themes/gramlot-index/icons/columns.svg +4 -0
- package/themes/gramlot-index/icons/confirm.svg +4 -0
- package/themes/gramlot-index/icons/copy.svg +4 -0
- package/themes/gramlot-index/icons/cut.svg +4 -0
- package/themes/gramlot-index/icons/database.svg +4 -0
- package/themes/gramlot-index/icons/delete.svg +4 -0
- package/themes/gramlot-index/icons/detach.svg +4 -0
- package/themes/gramlot-index/icons/document.svg +4 -0
- package/themes/gramlot-index/icons/download.svg +4 -0
- package/themes/gramlot-index/icons/duplicate.svg +4 -0
- package/themes/gramlot-index/icons/edit.svg +4 -0
- package/themes/gramlot-index/icons/eraser.svg +4 -0
- package/themes/gramlot-index/icons/expand.svg +4 -0
- package/themes/gramlot-index/icons/export.svg +4 -0
- package/themes/gramlot-index/icons/eye.svg +4 -0
- package/themes/gramlot-index/icons/filter.svg +4 -0
- package/themes/gramlot-index/icons/first.svg +4 -0
- package/themes/gramlot-index/icons/folder.svg +4 -0
- package/themes/gramlot-index/icons/group.svg +4 -0
- package/themes/gramlot-index/icons/help.svg +4 -0
- package/themes/gramlot-index/icons/home.svg +4 -0
- package/themes/gramlot-index/icons/horizontal-rule.svg +4 -0
- package/themes/gramlot-index/icons/image.svg +4 -0
- package/themes/gramlot-index/icons/import.svg +4 -0
- package/themes/gramlot-index/icons/indent.svg +4 -0
- package/themes/gramlot-index/icons/info.svg +4 -0
- package/themes/gramlot-index/icons/inspector.svg +4 -0
- package/themes/gramlot-index/icons/italic.svg +4 -0
- package/themes/gramlot-index/icons/last.svg +4 -0
- package/themes/gramlot-index/icons/layers.svg +4 -0
- package/themes/gramlot-index/icons/link.svg +4 -0
- package/themes/gramlot-index/icons/list-bulleted.svg +4 -0
- package/themes/gramlot-index/icons/list-numbered.svg +4 -0
- package/themes/gramlot-index/icons/list.svg +4 -0
- package/themes/gramlot-index/icons/lock.svg +4 -0
- package/themes/gramlot-index/icons/logout.svg +4 -0
- package/themes/gramlot-index/icons/navigation.svg +4 -0
- package/themes/gramlot-index/icons/new.svg +4 -0
- package/themes/gramlot-index/icons/next.svg +4 -0
- package/themes/gramlot-index/icons/open.svg +4 -0
- package/themes/gramlot-index/icons/outdent.svg +4 -0
- package/themes/gramlot-index/icons/palette.svg +4 -0
- package/themes/gramlot-index/icons/panel-bottom.svg +4 -0
- package/themes/gramlot-index/icons/panel-left.svg +4 -0
- package/themes/gramlot-index/icons/panel-right.svg +4 -0
- package/themes/gramlot-index/icons/panel-top.svg +4 -0
- package/themes/gramlot-index/icons/paragraph.svg +4 -0
- package/themes/gramlot-index/icons/paste.svg +4 -0
- package/themes/gramlot-index/icons/pause.svg +4 -0
- package/themes/gramlot-index/icons/pin.svg +4 -0
- package/themes/gramlot-index/icons/play.svg +4 -0
- package/themes/gramlot-index/icons/previous.svg +4 -0
- package/themes/gramlot-index/icons/print.svg +4 -0
- package/themes/gramlot-index/icons/redo.svg +4 -0
- package/themes/gramlot-index/icons/refresh.svg +4 -0
- package/themes/gramlot-index/icons/save.svg +4 -0
- package/themes/gramlot-index/icons/search.svg +4 -0
- package/themes/gramlot-index/icons/select-all.svg +4 -0
- package/themes/gramlot-index/icons/send.svg +4 -0
- package/themes/gramlot-index/icons/settings.svg +4 -0
- package/themes/gramlot-index/icons/sort-ascending.svg +4 -0
- package/themes/gramlot-index/icons/sort-descending.svg +4 -0
- package/themes/gramlot-index/icons/stop.svg +4 -0
- package/themes/gramlot-index/icons/strikethrough.svg +4 -0
- package/themes/gramlot-index/icons/table.svg +4 -0
- package/themes/gramlot-index/icons/tree.svg +4 -0
- package/themes/gramlot-index/icons/underline.svg +4 -0
- package/themes/gramlot-index/icons/undo.svg +4 -0
- package/themes/gramlot-index/icons/unlock.svg +4 -0
- package/themes/gramlot-index/icons/upload.svg +4 -0
- package/themes/gramlot-index/icons/user.svg +4 -0
- package/themes/gramlot-index/icons/warning.svg +4 -0
- package/themes/gramlot-index/icons/zoom-in.svg +4 -0
- package/themes/gramlot-index/icons/zoom-out.svg +4 -0
- package/themes/gramlot-index/icons.json +93 -0
- package/themes/gramlot-index/theme.css +24 -0
- package/themes/joanna/theme.css +2 -0
- package/themes/mela/theme.css +2 -0
- package/themes/mimi/theme.css +2 -0
- package/tools/gramlot-tools-inspector.js +11 -3
package/src/index.js
CHANGED
|
@@ -36,6 +36,7 @@ export {IndexLogic, mountIndexPage} from './index-page-runtime.js';
|
|
|
36
36
|
export {NavigationResolver} from './navigation-resolver.js';
|
|
37
37
|
|
|
38
38
|
export {parseDateExpression} from '@genrojs/toolbox';
|
|
39
|
+
export {objectExtract} from './object-extract.js';
|
|
39
40
|
|
|
40
41
|
export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "./filesystem.js";
|
|
41
42
|
export {IdeLogic} from "./ide-runtime.js";
|
|
@@ -50,11 +51,11 @@ export {BrowserCameraProvider} from './browser-camera-provider.js';
|
|
|
50
51
|
|
|
51
52
|
export {MobileEnvironment} from './mobile-environment.js';
|
|
52
53
|
|
|
53
|
-
export {chatRender, chatSend, chatCompleted, chatReset, chatResize, chatPickAttachments, chatRenderAttachments, chatRemoveAttachment} from './chat.js';
|
|
54
|
+
export {chatRender, chatSend, chatDraftKey, chatCompleted, chatReset, chatResize, chatPickAttachments, chatRenderAttachments, chatRemoveAttachment} from './chat.js';
|
|
54
55
|
|
|
55
56
|
// Preserve fluent Source identity across separately prepared collection bundles.
|
|
56
57
|
export {sourceTarget} from '@genrojs/builders';
|
|
57
|
-
export {TreeStore, TreeStoreResolver, TreeStoreLogic} from './tree-store.js';
|
|
58
|
+
export {treeStore, TreeStore, TreeStoreResolver, TreeStoreLogic} from './tree-store.js';
|
|
58
59
|
export {UploadTarget} from './server/upload.js';
|
|
59
60
|
|
|
60
61
|
export {VisualEditorLogic, visualEditorScope} from './visual-editor.js';
|
|
@@ -70,3 +71,7 @@ export {bundledCollections} from './bundled-collections.js';
|
|
|
70
71
|
export {libraries, loadLibrary} from './libraries.js';
|
|
71
72
|
// The random id of the runtime, for collections prepared outside this package.
|
|
72
73
|
export {randomId} from './random-id.js';
|
|
74
|
+
|
|
75
|
+
export {PageStore} from './server/page-store.js';
|
|
76
|
+
|
|
77
|
+
export {ChatPane, ChatPageStore} from './server/chat.js';
|
package/src/libraries.js
CHANGED
|
@@ -52,11 +52,11 @@ export const libraries = Object.freeze({
|
|
|
52
52
|
jodit: Object.freeze({packages: {jodit: '4.18.10'}, local: './components/jodit-engine.js',
|
|
53
53
|
url: null, integrity: null, format: 'module'}),
|
|
54
54
|
codemirror: Object.freeze({packages: {
|
|
55
|
-
'@codemirror/autocomplete': '6.20.4', '@codemirror/commands': '6.11.
|
|
55
|
+
'@codemirror/autocomplete': '6.20.4', '@codemirror/commands': '6.11.2', '@codemirror/lang-css': '6.3.1',
|
|
56
56
|
'@codemirror/lang-html': '6.4.12', '@codemirror/lang-javascript': '6.2.5', '@codemirror/lang-json': '6.0.2',
|
|
57
|
-
'@codemirror/lang-markdown': '6.5.
|
|
58
|
-
'@codemirror/lang-xml': '6.1.0', '@codemirror/language': '6.13.1', '@codemirror/lint': '6.9.8',
|
|
59
|
-
'@codemirror/search': '6.7.
|
|
57
|
+
'@codemirror/lang-markdown': '6.5.3', '@codemirror/lang-python': '6.2.1', '@codemirror/lang-rust': '6.0.2', '@codemirror/lang-sql': '6.10.0',
|
|
58
|
+
'@codemirror/lang-xml': '6.1.0', '@codemirror/lang-yaml': '6.1.3', '@codemirror/language': '6.13.1', '@codemirror/lint': '6.9.8',
|
|
59
|
+
'@codemirror/search': '6.7.3', '@codemirror/state': '6.7.6', '@codemirror/view': '6.44.2',
|
|
60
60
|
'@lezer/highlight': '1.2.5', 'codemirror-lang-typst': '0.6.0'},
|
|
61
61
|
local: './components/code-mirror-engine.js', url: null, integrity: null, format: 'module'}),
|
|
62
62
|
prosemirror: Object.freeze({packages: {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entries of `obj` named by `keys`: a prefix followed by `*`, or a comma-separated list of names.
|
|
3
|
+
* The extracted entries leave `obj` unless `pop` is false; a prefix is removed from the names unless
|
|
4
|
+
* `slice` is false.
|
|
5
|
+
*/
|
|
6
|
+
export function objectExtract(obj: Record<string, unknown>, keys: string,
|
|
7
|
+
options?: {pop?: boolean, slice?: boolean}): Record<string, unknown>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* @ts-self-types="./object-extract.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* The attribute families of legacy `objectExtract` (`gnrlang.js:709`).
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The entries of `obj` named by `keys`, as a new object. `keys` is a prefix followed by `*`
|
|
10
|
+
* (`'tooltip_*'`: every entry whose name starts with `tooltip_`, named without the prefix unless
|
|
11
|
+
* `slice` is false) or a comma-separated list of names (`'width, height'`: the names present in
|
|
12
|
+
* `obj`). The extracted entries leave `obj` unless `pop` is false.
|
|
13
|
+
*/
|
|
14
|
+
export function objectExtract(obj, keys, {pop = true, slice = true} = {}) {
|
|
15
|
+
const result = {};
|
|
16
|
+
if (keys.endsWith('*')) {
|
|
17
|
+
const prefix = keys.slice(0, -1);
|
|
18
|
+
for (const name of Object.keys(obj)) {
|
|
19
|
+
if (!name.startsWith(prefix)) continue;
|
|
20
|
+
result[slice ? name.slice(prefix.length) : name] = obj[name];
|
|
21
|
+
if (pop) delete obj[name];
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
for (const name of keys.split(',').map(key => key.trim())) {
|
|
26
|
+
if (!Object.hasOwn(obj, name)) continue;
|
|
27
|
+
result[name] = obj[name];
|
|
28
|
+
if (pop) delete obj[name];
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
@@ -11,7 +11,7 @@ export const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
|
11
11
|
const BINDING_ATTRIBUTES = new Set([
|
|
12
12
|
'default', 'default_value', 'live', 'group', 'visible', 'action', 'fire', 'ask', '_ask', '_ask_if', '_ask_title', '_ask_onCancel',
|
|
13
13
|
'if', 'else', '_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
|
|
14
|
-
'remote', '_cachedRemote', '_onRemote', '_onCalling', 'onComplete', 'onClose', 'onEnd', 'onChecked', 'onDrag', 'onDrop', 'dropTargetCb', 'onDragEnd',
|
|
14
|
+
'remote', '_cachedRemote', '_onRemote', '_onCalling', '_waitingMessage', 'onComplete', 'onClose', 'onEnd', 'onChecked', 'onDrag', 'onDrop', 'dropTargetCb', 'onDragEnd',
|
|
15
15
|
'dropTarget', 'dropTypes', 'dragTags', 'dropTags', 'effectAllowed', 'dropEffect', 'dragClass',
|
|
16
16
|
]);
|
|
17
17
|
const BINDING_ATTRIBUTE_PREFIXES = ['default_', 'attr_', 'fire_', 'connect_on', 'subscribe_', 'selfsubscribe_', 'formsubscribe_', 'remote_'];
|
|
@@ -41,14 +41,14 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
41
41
|
dropValues: Record<string, unknown>; item: unknown; path: string | undefined;
|
|
42
42
|
source: Record<string, unknown>; selfdrop: boolean;
|
|
43
43
|
dropEffect: 'copy' | 'move' | 'link'; dataAvailable: boolean;
|
|
44
|
-
placement?: 'before' | 'inside' | 'after';
|
|
44
|
+
placement?: 'before' | 'inside' | 'after' | {left:number; top:number};
|
|
45
45
|
}): boolean;
|
|
46
46
|
/** Invoke the general onDrop action on already accepted data not consumed by typed actions. */
|
|
47
47
|
invokeDrop(node: SourceBagNode, context: {
|
|
48
48
|
dropValues: Record<string, unknown>; item: unknown; path: string | undefined;
|
|
49
49
|
source: Record<string, unknown>; selfdrop: boolean;
|
|
50
50
|
dropEffect: 'copy' | 'move' | 'link'; dataAvailable: boolean;
|
|
51
|
-
placement?: 'before' | 'inside' | 'after';
|
|
51
|
+
placement?: 'before' | 'inside' | 'after' | {left:number; top:number};
|
|
52
52
|
}): Promise<void>;
|
|
53
53
|
/** Origin callback, followed by local endDrag publication even if the callback fails. */
|
|
54
54
|
endDrag(node: SourceBagNode, context: {source: Record<string, unknown>; dropEffect: string}, topic: string): Promise<void>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {trackChoiceFocus} from '../view/choice-focus.js';
|
|
1
2
|
import {callLogic, invokeHook} from '../binding/hooks.js';
|
|
2
3
|
/* @ts-self-types="./gramlot-renderer.d.ts" */
|
|
3
4
|
/**
|
|
@@ -51,7 +52,7 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
51
52
|
this._binding = binding;
|
|
52
53
|
this.source = source;
|
|
53
54
|
this.destination = destination;
|
|
54
|
-
this.html = new HtmlElement({uploadContext: () => ({gramlot: binding.gramlot, window: destination.ownerDocument.defaultView}), metadataAttributes: ['__ref', ...SYMBOLIC_ATTRIBUTES],
|
|
55
|
+
this.html = new HtmlElement({icons: binding.gramlot._icons, uploadContext: () => ({gramlot: binding.gramlot, window: destination.ownerDocument.defaultView}), metadataAttributes: ['__ref', ...SYMBOLIC_ATTRIBUTES],
|
|
55
56
|
format: (value, options) => binding.gramlot.format(value, options),
|
|
56
57
|
reportError: (node, error, title) => { if (this.records.has(node)) binding.reportError(error, title); },
|
|
57
58
|
callLogic: (node, name, keywords) => {
|
|
@@ -232,6 +233,8 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
232
233
|
this.records.set(node, record);
|
|
233
234
|
this.elements.set(record.element, record);
|
|
234
235
|
this.elements.set(record.presentation, record);
|
|
236
|
+
if (record.element.namespaceURI === XHTML_NS && record.element.localName === 'select')
|
|
237
|
+
record.cleanup.push(trackChoiceFocus(record.element));
|
|
235
238
|
const control = ControlAdapter.for(record.binding, record);
|
|
236
239
|
// An input can acquire/change its adapter through type while its DOM record survives.
|
|
237
240
|
if (control || (record.element.namespaceURI === XHTML_NS && record.element.localName === 'input'))
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {Bag} from '@genrojs/bag';
|
|
2
|
+
import {fileTypes} from '../chat-file-types.js';
|
|
3
|
+
import {Exposed} from './page-base.js';
|
|
4
|
+
import {UploadTarget} from './upload.js';
|
|
5
|
+
|
|
6
|
+
// Files are owned by the page. Browser imports of the authoring API never open a filesystem.
|
|
7
|
+
const FS = 'node:fs';
|
|
8
|
+
const OS = 'node:os';
|
|
9
|
+
const PATH = 'node:path';
|
|
10
|
+
const BUFFER = 'node:buffer';
|
|
11
|
+
|
|
12
|
+
export class ChatAttachments extends Exposed {
|
|
13
|
+
#files = new Map();
|
|
14
|
+
#directory = null;
|
|
15
|
+
#fs = null;
|
|
16
|
+
#closed = false;
|
|
17
|
+
url = '';
|
|
18
|
+
|
|
19
|
+
async receive({name, mediaType, size}) {
|
|
20
|
+
if (typeof name !== 'string' || !name.includes('.') || /[/\\]/.test(name) || name.length > 255 ||
|
|
21
|
+
fileTypes[name.split('.').at(-1).toLowerCase()] !== mediaType || !Number.isSafeInteger(size) || size <= 0) {
|
|
22
|
+
throw new TypeError('Invalid chat attachment');
|
|
23
|
+
}
|
|
24
|
+
const [fs, os, path] = await Promise.all([import(FS), import(OS), import(PATH)]);
|
|
25
|
+
if (this.#closed) throw new Error('Closed chat');
|
|
26
|
+
this.#fs = fs;
|
|
27
|
+
this.#directory ??= fs.mkdtempSync(path.join(os.tmpdir(), 'gramlot-chat-'));
|
|
28
|
+
const ref = crypto.randomUUID().replaceAll('-', '');
|
|
29
|
+
const filename = path.join(this.#directory, ref);
|
|
30
|
+
const record = {name, mediaType, size, ref};
|
|
31
|
+
if (mediaType.startsWith('image/')) record.url = this.url + ref;
|
|
32
|
+
return new ChatFile(fs, filename, record, () => {
|
|
33
|
+
if (this.#closed) throw new Error('Closed chat');
|
|
34
|
+
this.#files.set(ref, {record, filename});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
references(refs) {
|
|
39
|
+
const records = new Bag();
|
|
40
|
+
for (const node of refs.getNodes()) {
|
|
41
|
+
const {record} = this.#files.get(node.value.getItem('ref'));
|
|
42
|
+
records.setItem(node.label, new Bag(record));
|
|
43
|
+
}
|
|
44
|
+
return records;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async contents(refs) {
|
|
48
|
+
const result = new Bag();
|
|
49
|
+
for (const node of refs.getNodes()) {
|
|
50
|
+
const {record, filename} = this.#files.get(node.value.getItem('ref'));
|
|
51
|
+
const data = await this.#fs.promises.readFile(filename, 'base64');
|
|
52
|
+
result.setItem(node.label, new Bag({...record, data}));
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async imageUrl(png) {
|
|
58
|
+
const {Buffer} = await import(BUFFER);
|
|
59
|
+
const bytes = Buffer.from(png, 'base64');
|
|
60
|
+
const file = await this.receive({name:'image.png', mediaType:'image/png', size:bytes.length});
|
|
61
|
+
try {
|
|
62
|
+
file.write(bytes);
|
|
63
|
+
return file.finish().result.url;
|
|
64
|
+
} catch (error) { file.abort(); throw error; }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async openImage(ref) {
|
|
68
|
+
const file = this.#files.get(ref);
|
|
69
|
+
if (!file || !file.record.mediaType.startsWith('image/')) return null;
|
|
70
|
+
return {type:file.record.mediaType, bytes:await this.#fs.promises.readFile(file.filename)};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
close() {
|
|
74
|
+
this.#closed = true;
|
|
75
|
+
this.#files.clear();
|
|
76
|
+
if (this.#directory) this.#fs.rmSync(this.#directory, {recursive:true, force:true});
|
|
77
|
+
this.#directory = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
ChatAttachments.registerUpload('receive');
|
|
81
|
+
|
|
82
|
+
class ChatFile extends UploadTarget {
|
|
83
|
+
#fs;
|
|
84
|
+
#filename;
|
|
85
|
+
#record;
|
|
86
|
+
#commit;
|
|
87
|
+
#fd;
|
|
88
|
+
#size = 0;
|
|
89
|
+
#decoder;
|
|
90
|
+
#header = [];
|
|
91
|
+
#outcome = {state:'pending'};
|
|
92
|
+
|
|
93
|
+
constructor(fs, filename, record, commit) {
|
|
94
|
+
super({maxBytes:record.size});
|
|
95
|
+
this.#fs = fs;
|
|
96
|
+
this.#filename = filename;
|
|
97
|
+
this.#record = record;
|
|
98
|
+
this.#commit = commit;
|
|
99
|
+
this.#fd = fs.openSync(filename, 'wx', 0o600);
|
|
100
|
+
if (record.mediaType === 'text/plain') this.#decoder = new TextDecoder('utf-8', {fatal:true});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
write(bytes) {
|
|
104
|
+
if (this.#size + bytes.length > this.maxBytes) throw new RangeError('Upload exceeds declared size');
|
|
105
|
+
this.#decoder?.decode(bytes, {stream:true});
|
|
106
|
+
if (this.#decoder && bytes.includes(0)) throw new TypeError('Invalid text attachment');
|
|
107
|
+
this.#header.push(...bytes.subarray(0, 12 - this.#header.length));
|
|
108
|
+
let written = 0;
|
|
109
|
+
while (written < bytes.length) written += this.#fs.writeSync(this.#fd, bytes, written, bytes.length - written);
|
|
110
|
+
this.#size += written;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
finish() {
|
|
114
|
+
if (this.#size !== this.maxBytes) throw new RangeError('Incomplete attachment');
|
|
115
|
+
const header = String.fromCharCode(...this.#header);
|
|
116
|
+
const signatures = {'image/png':'\x89PNG\r\n\x1a\n', 'image/jpeg':'\xff\xd8\xff', 'image/webp':'RIFF', 'application/pdf':'%PDF-'};
|
|
117
|
+
if (this.#decoder) this.#decoder.decode();
|
|
118
|
+
else if (!header.startsWith(signatures[this.#record.mediaType]) ||
|
|
119
|
+
(this.#record.mediaType === 'image/webp' && header.slice(8, 12) !== 'WEBP')) {
|
|
120
|
+
throw new TypeError('Invalid attachment content');
|
|
121
|
+
}
|
|
122
|
+
this.#fs.closeSync(this.#fd);
|
|
123
|
+
this.#fd = null;
|
|
124
|
+
this.#commit();
|
|
125
|
+
this.#outcome = {state:'completed', result:this.#record};
|
|
126
|
+
return this.#outcome;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
abort() {
|
|
130
|
+
if (this.#outcome.state === 'pending') {
|
|
131
|
+
if (this.#fd !== null) this.#fs.closeSync(this.#fd);
|
|
132
|
+
this.#fs.rmSync(this.#filename, {force:true});
|
|
133
|
+
this.#outcome = {state:'unsaved'};
|
|
134
|
+
}
|
|
135
|
+
return this.#outcome;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
status() { return this.#outcome; }
|
|
139
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type {Bag} from '@genrojs/bag';
|
|
2
|
+
import {PageStore} from './page-store.js';
|
|
3
|
+
import type {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
4
|
+
|
|
5
|
+
type Pane = GramlotBuilder['root'];
|
|
6
|
+
export class ChatPane {
|
|
7
|
+
constructor(parent: Pane, options: {datapath: string; chat_id?: string; models?: Record<string, string> | null; [attribute: string]: unknown});
|
|
8
|
+
frame: Pane;
|
|
9
|
+
composer: Pane;
|
|
10
|
+
messages: Pane;
|
|
11
|
+
editor: Pane;
|
|
12
|
+
toolbar: Pane;
|
|
13
|
+
sendButton: Pane;
|
|
14
|
+
attachmentButton: Pane;
|
|
15
|
+
modelSelector: Pane | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ChatRequestContext {
|
|
19
|
+
chatId: string;
|
|
20
|
+
attachments: Bag;
|
|
21
|
+
feedback: (text: string) => void;
|
|
22
|
+
model: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class ChatPageStore extends PageStore {
|
|
26
|
+
static chatIds: string[];
|
|
27
|
+
processMessage(text: string, context: ChatRequestContext): string | GramlotBuilder['source'] | Promise<string | GramlotBuilder['source']>;
|
|
28
|
+
resetConversation(chatId: string): void;
|
|
29
|
+
conversationMessages(chatId: string): Bag;
|
|
30
|
+
conversationAuthors(): [string, string];
|
|
31
|
+
attachmentContents(refs: Bag): Promise<Bag>;
|
|
32
|
+
imageUrl(base64Png: string): Promise<string>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {Bag} from '@genrojs/bag';
|
|
2
|
+
import {GramlotBuilderBag} from '../builder/source.js';
|
|
3
|
+
import {PageStore} from './page-store.js';
|
|
4
|
+
import {ChatAttachments} from './chat-attachments.js';
|
|
5
|
+
|
|
6
|
+
const identifier = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
7
|
+
const conversation = ready => new Bag({messages: new Bag(), ready, request: null, response: null, feedback: ''});
|
|
8
|
+
|
|
9
|
+
export class ChatPane {
|
|
10
|
+
constructor(parent, {datapath, chat_id = 'main', models = null, ...attributes}) {
|
|
11
|
+
if (typeof chat_id !== 'string' || !identifier.test(chat_id)) throw new TypeError('Invalid chat_id');
|
|
12
|
+
if (models !== null && (typeof models !== 'object' || Array.isArray(models) || !Object.keys(models).length
|
|
13
|
+
|| !Object.values(models).every(caption => typeof caption === 'string'))) {
|
|
14
|
+
throw new TypeError('models must map model values to captions');
|
|
15
|
+
}
|
|
16
|
+
const frame = this.frame = parent.framePane({datapath, ...attributes});
|
|
17
|
+
const defaults = {
|
|
18
|
+
conversation: conversation(false), messageData: new Bag(), draft: '', pending: false,
|
|
19
|
+
requestId: '', error: '', attachments: new Bag(), picking: false, composerHeight: 96,
|
|
20
|
+
model: models ? Object.keys(models)[0] : null,
|
|
21
|
+
};
|
|
22
|
+
for (const [name, value] of Object.entries(defaults)) {
|
|
23
|
+
frame.dataSetter({destination_path: '.' + name, value});
|
|
24
|
+
}
|
|
25
|
+
frame.dataBinder({destination_path: '.conversation', server_path: 'chats.' + chat_id});
|
|
26
|
+
frame.dataController({func:'chatCompleted', response:'^.conversation.response'});
|
|
27
|
+
this.messages = frame.center.chatMessages({height:'100%', min_height:0});
|
|
28
|
+
this.messages.dataController({func:'chatRender', messages:'^.conversation.messages', _onBuilt:true});
|
|
29
|
+
frame.bottom.setAttr({height:'^.composerHeight', splitter:false});
|
|
30
|
+
this.composer = frame.bottom.framePane({height:'100%', 'data-chat-composer':true});
|
|
31
|
+
this.composer.top.setAttr({height:'2px', 'data-chat-top':true});
|
|
32
|
+
this.composer.bottom.setAttr({height:'44px'});
|
|
33
|
+
const pane = this.composer.center.contentPane({overflow:'hidden'});
|
|
34
|
+
const center = pane.div({display:'flex', flex_direction:'column', padding:'4px 10px', gap:'4px'});
|
|
35
|
+
center.dataFormula({result_path:'.status', formula:'feedback || (pending ? "Sending to the server…" : "")',
|
|
36
|
+
pending:'^.pending', feedback:'^.conversation.feedback', _init:true});
|
|
37
|
+
center.div('^.status', {node_label:'chatFeedback', class:'gramlot-chat-status', role:'status',
|
|
38
|
+
aria_live:'polite', display:'^.status:={} ? "flex" : "none"', 'data-pending':'^.pending',
|
|
39
|
+
height:'22px', min_height:'22px', overflow:'hidden', white_space:'nowrap',
|
|
40
|
+
text_overflow:'ellipsis', font_size:'13px'});
|
|
41
|
+
this.editor = center.textarea({value:'^.draft', live:true, aria_label:'Message', placeholder:'Write a message…',
|
|
42
|
+
class:'gramlot-chat-editor', connect_onkeydown:'chatDraftKey', rows:1, min_height:'44px', height:'44px', line_height:'20px',
|
|
43
|
+
resize:'none', width:'100%', box_sizing:'border-box', border_radius:'18px', padding:'10px 14px'});
|
|
44
|
+
const previews = center.div({display:'flex', gap:'6px', overflow_x:'auto', max_height:'76px'});
|
|
45
|
+
previews.dataController({func:'chatRenderAttachments', attachments:'^.attachments', _onBuilt:true});
|
|
46
|
+
center.dataController({func:'chatResize', draft:'^.draft', status:'^.status', error:'^.error',
|
|
47
|
+
attachments:'^.attachments', _onBuilt:true});
|
|
48
|
+
center.span('^.error', {role:'alert', hidden:'^.error:=!{}', font_size:'12px'});
|
|
49
|
+
const actions = this.composer.bottom.div({class:'gramlot-chat-toolbar', role:'toolbar',
|
|
50
|
+
'data-chat-toolbar':true, aria_label:'Chat controls', display:'flex', align_items:'center',
|
|
51
|
+
gap:'8px', height:'44px', padding:'0 14px', box_sizing:'border-box'});
|
|
52
|
+
this.attachmentButton = actions.button({icon:'new', aria_label:'Add files or images', title:'Add files or images',
|
|
53
|
+
class:'button--quiet', font_size:'24px', width:'36px', height:'36px',
|
|
54
|
+
style:'--gramlot-touch-target:36px;--gramlot-button-padding:0;--gramlot-field-height:36px;',
|
|
55
|
+
picking:'^.picking', disabled:'^.pending:={} || {picking}'});
|
|
56
|
+
this.attachmentButton.dataController({func:'chatPickAttachments'});
|
|
57
|
+
this.modelSelector = null;
|
|
58
|
+
if (models) {
|
|
59
|
+
this.modelSelector = actions.select({value:'^.model', aria_label:'Model', title:'Model', width:'auto',
|
|
60
|
+
flex_shrink:0, disabled:'^.pending'});
|
|
61
|
+
for (const [model, caption] of Object.entries(models)) this.modelSelector.option(caption, {value:model});
|
|
62
|
+
}
|
|
63
|
+
this.toolbar = actions.div({display:'flex', align_items:'center', gap:'10px', flex:1, min_width:0, overflow_x:'auto'});
|
|
64
|
+
this.sendButton = actions.button({aria_label:'Send', title:'Send message (Enter)', class:'gramlot-chat-send',
|
|
65
|
+
font_size:'16px', flex_shrink:0, width:'36px', height:'36px', chat_id,
|
|
66
|
+
style:'--gramlot-radius-sm:50%;--gramlot-button-padding:9px;--gramlot-button-gap:0px;--gramlot-field-height:36px;--gramlot-touch-target:36px;',
|
|
67
|
+
ready:'^.conversation.ready', draft:'^.draft', attachments:'^.attachments', picking:'^.picking',
|
|
68
|
+
disabled:'^.pending:={} || {picking} || !{ready} || (!{draft}.trim() && !{attachments}.length)'});
|
|
69
|
+
this.sendButton.span({class:'gramlot-chat-send-icon', aria_hidden:'true'});
|
|
70
|
+
this.sendButton.dataController({func:'chatSend', chat_id});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class ChatPageStore extends PageStore {
|
|
75
|
+
static chatIds = ['main'];
|
|
76
|
+
#queues = new Map();
|
|
77
|
+
attachments = new ChatAttachments();
|
|
78
|
+
|
|
79
|
+
main(root) {
|
|
80
|
+
for (const chatId of this.constructor.chatIds) {
|
|
81
|
+
if (typeof chatId !== 'string' || !identifier.test(chatId)) throw new TypeError('Invalid chatId');
|
|
82
|
+
this.data.setItem('chats.' + chatId, conversation(true));
|
|
83
|
+
root.dataController({func:'chatRequest', chatId, request:'^chats.' + chatId + '.request'});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
close() {
|
|
88
|
+
super.close();
|
|
89
|
+
this.attachments.close();
|
|
90
|
+
this.#queues.clear();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
attachmentContents(refs) { return this.attachments.contents(refs); }
|
|
94
|
+
imageUrl(png) { return this.attachments.imageUrl(png); }
|
|
95
|
+
conversationMessages(chatId) { return this.data.getItem('chats.' + chatId + '.messages'); }
|
|
96
|
+
resetConversation(chatId) { this.data.setItem('chats.' + chatId + '.messages', new Bag()); }
|
|
97
|
+
conversationAuthors() { return ['You', 'Assistant']; }
|
|
98
|
+
processMessage(text, context) { return text; }
|
|
99
|
+
|
|
100
|
+
chatRequest(node, {chatId, request}) {
|
|
101
|
+
if (!(request instanceof Bag)) return;
|
|
102
|
+
const previous = this.#queues.get(chatId) ?? Promise.resolve();
|
|
103
|
+
const next = previous.then(() => this.#handle(chatId, request));
|
|
104
|
+
this.#queues.set(chatId, next);
|
|
105
|
+
next.finally(() => { if (this.#queues.get(chatId) === next) this.#queues.delete(chatId); });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async #handle(chatId, request) {
|
|
109
|
+
if (this.closed) return;
|
|
110
|
+
const base = 'chats.' + chatId;
|
|
111
|
+
const id = request.getItem('id');
|
|
112
|
+
const text = request.getItem('text');
|
|
113
|
+
if (typeof id !== 'string' || !id || this.data.getItem(base + '.response.id') === id) return;
|
|
114
|
+
let accepted = false;
|
|
115
|
+
let error = '';
|
|
116
|
+
try {
|
|
117
|
+
const action = request.getItem('action');
|
|
118
|
+
if (action === 'reset') {
|
|
119
|
+
await this.resetConversation(chatId);
|
|
120
|
+
accepted = true;
|
|
121
|
+
} else if (action !== 'send') {
|
|
122
|
+
error = 'Unknown chat action.';
|
|
123
|
+
} else {
|
|
124
|
+
const refs = request.getItem('attachments');
|
|
125
|
+
if (!(refs instanceof Bag) || refs.length > 5) throw new TypeError('Invalid attachments');
|
|
126
|
+
if (typeof text !== 'string' || (!text.trim() && !refs.length)) {
|
|
127
|
+
error = 'Enter a message.';
|
|
128
|
+
} else {
|
|
129
|
+
const attachments = this.attachments.references(refs);
|
|
130
|
+
const feedback = status => {
|
|
131
|
+
if (typeof status !== 'string') throw new TypeError('Feedback must be text');
|
|
132
|
+
if (!this.closed) this.data.setItem(base + '.feedback', status);
|
|
133
|
+
};
|
|
134
|
+
const model = request.getItem('model') ?? null;
|
|
135
|
+
const result = await this.processMessage(text, {chatId, attachments, feedback, model});
|
|
136
|
+
if (this.closed) return;
|
|
137
|
+
if (typeof result !== 'string' && !(result instanceof GramlotBuilderBag)) throw new TypeError('Invalid reply');
|
|
138
|
+
const messages = this.conversationMessages(chatId);
|
|
139
|
+
const [user, assistant] = this.conversationAuthors();
|
|
140
|
+
const label = () => 'm_' + crypto.randomUUID().replaceAll('-', '');
|
|
141
|
+
messages.setItem(label(), this.#attachmentMessage(text, attachments), {role:'user', author:user});
|
|
142
|
+
messages.setItem(label(), result, {role:'assistant', author:assistant});
|
|
143
|
+
accepted = true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} catch {
|
|
147
|
+
error = 'The server could not process the message. Your draft has been kept.';
|
|
148
|
+
}
|
|
149
|
+
if (this.closed) return;
|
|
150
|
+
this.data.setItem(base + '.feedback', '');
|
|
151
|
+
this.data.setItem(base + '.response', new Bag({id, submitted:text, accepted, error}));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#attachmentMessage(text, attachments) {
|
|
155
|
+
if (!attachments.length) return text;
|
|
156
|
+
const builder = this.sourceBuilder();
|
|
157
|
+
builder.root.div('==' + JSON.stringify(text), {white_space:'pre-wrap'});
|
|
158
|
+
for (const node of attachments.getNodes()) {
|
|
159
|
+
const file = node.value;
|
|
160
|
+
const name = '==' + JSON.stringify(file.getItem('name'));
|
|
161
|
+
builder.root.div(name, {font_size:'13px'});
|
|
162
|
+
if (file.getItem('mediaType').startsWith('image/')) {
|
|
163
|
+
builder.root.img({src:file.getItem('url'), alt:name, max_width:'240px', max_height:'160px'});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return builder.source;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {Page} from './page-base.js';
|
|
2
|
+
|
|
3
|
+
/** Serve native dbSelect and dbComboBox from the selector service configured by the host. */
|
|
4
|
+
export class DatabaseSelectors extends Page {
|
|
5
|
+
dbSelect(parameters) {
|
|
6
|
+
if (Object.hasOwn(parameters, 'where')) {
|
|
7
|
+
throw new Error('SQL predicates belong in a dedicated server endpoint');
|
|
8
|
+
}
|
|
9
|
+
return this.database.select(parameters);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
DatabaseSelectors.registerEndpoint('dbSelect');
|
|
@@ -12,7 +12,7 @@ import {basename, dirname, extname, join, relative, resolve, sep} from 'node:pat
|
|
|
12
12
|
import {pathToFileURL} from 'node:url';
|
|
13
13
|
import {gramlotDev} from './assets.js';
|
|
14
14
|
import {fileBelow, inside, realFile} from './files.js';
|
|
15
|
-
import {Page} from './page.js';
|
|
15
|
+
import {Page, PageStore} from './page.js';
|
|
16
16
|
import {GramlotServer, PageNotFound} from './gramlot-server.js';
|
|
17
17
|
import {InvalidResourceName, SEGMENT, parseRequires} from './resources.js';
|
|
18
18
|
|
|
@@ -84,6 +84,11 @@ export class GramlotFileServer extends GramlotServer {
|
|
|
84
84
|
return (await import(await this._moduleUrl(await this.locatePage(path)))).Page;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
async resolvePageStore(PageClass, path) {
|
|
88
|
+
const module = await import(await this._moduleUrl(await this.locatePage(path)));
|
|
89
|
+
return Object.hasOwn(module, 'PageStore') ? module.PageStore : PageStore;
|
|
90
|
+
}
|
|
91
|
+
|
|
87
92
|
/** Import route-eligible page modules at startup, without calling their methods. */
|
|
88
93
|
async *iterPageClasses() {
|
|
89
94
|
const seenFiles = new Set(), seenClasses = new Set();
|
|
@@ -60,12 +60,16 @@ export interface PreparedCollection {
|
|
|
60
60
|
libraries?: Record<string, string>;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
import type {PageStore} from './page-store.js';
|
|
64
|
+
|
|
63
65
|
/** The options of a `GramlotServer`. */
|
|
64
66
|
export interface GramlotServerOptions {
|
|
65
67
|
/** URL of the runtime bundle. */
|
|
66
68
|
runtimeUrl?: string;
|
|
67
69
|
/** URL of the request envelopes. */
|
|
68
70
|
rpcUrl?: string;
|
|
71
|
+
websocketUrl?: string | null;
|
|
72
|
+
uploadUrl?: string | null;
|
|
69
73
|
/** URL of the page close endpoint. */
|
|
70
74
|
closeUrl?: string;
|
|
71
75
|
/** Id of the root element of the page. */
|
|
@@ -120,6 +124,8 @@ export class GramlotServer {
|
|
|
120
124
|
readonly markdownUrl: string;
|
|
121
125
|
/** URL of the request envelopes. */
|
|
122
126
|
rpcUrl: string;
|
|
127
|
+
websocketUrl: string | null;
|
|
128
|
+
uploadUrl: string | null;
|
|
123
129
|
/** URL of the page close endpoint. */
|
|
124
130
|
closeUrl: string;
|
|
125
131
|
/** Id of the root element of the page. */
|
|
@@ -170,14 +176,15 @@ export class GramlotServer {
|
|
|
170
176
|
/** The file of a `/themes/<path>` URL path whose real path is a file of the core themes, or null. Node.js and Bun only. */
|
|
171
177
|
themeFile(path: string): Promise<string | null>;
|
|
172
178
|
/** Register a page independently of its HTTP or Worker transport; `<path>/index.html` registers `<path>/`. */
|
|
173
|
-
|
|
179
|
+
openChatImage(pageId: string, ref: string, options: {owner: unknown}): Promise<{bytes: Uint8Array; type: string} | null>;
|
|
180
|
+
registerPage(path: string, options?: {owner?: unknown; prefix?: string; websocketUrl?: string | null}): Promise<RegisteredPage>;
|
|
174
181
|
/** Register the page and return its bootstrap; `prefix` is the mount prefix added to root-relative URLs. */
|
|
175
|
-
openPage(path: string, options?: {owner?: unknown; prefix?: string}): Promise<OpenedPage>;
|
|
182
|
+
openPage(path: string, options?: {owner?: unknown; prefix?: string; websocketUrl?: string | null}): Promise<OpenedPage>;
|
|
176
183
|
/** The text of a request body: UTF-8, a leading byte order mark removed; other bytes throw `InvalidRequest`. */
|
|
177
184
|
decodeBody(data: ArrayBuffer | ArrayBufferView): string;
|
|
178
185
|
/** Answer the request envelope `text` (TYTX) with the response envelope (TYTX); throws only `InvalidRequest`. `identity` is the trusted identity of this call, supplied to the fresh Page and `evaluateAuth`. */
|
|
179
186
|
call(text: string, options?: {owner?: unknown; identity?: unknown}): Promise<string>;
|
|
180
|
-
/** Admit
|
|
187
|
+
/** Admit the next block after owner and operation authorization checks. */
|
|
181
188
|
admitUpload(options: {pageId: string; transferId: string; authorization: string; owner?: unknown; identity?: unknown}): UploadTransfer;
|
|
182
189
|
/** Close the page named by the close message `text` (`{"pageId": string}` as JSON) when `owner` owns it; throws `InvalidRequest`. */
|
|
183
190
|
close(text: string, options?: {owner?: unknown}): void;
|
|
@@ -185,4 +192,13 @@ export class GramlotServer {
|
|
|
185
192
|
closePage(pageId: string, options?: {owner?: unknown}): void;
|
|
186
193
|
/** Forget every registered page; adapters call it on shutdown. */
|
|
187
194
|
closeAll(): void;
|
|
195
|
+
resolvePageStore(PageClass: typeof Page, path: string): typeof PageStore | Promise<typeof PageStore>;
|
|
196
|
+
createPageStore(StoreClass: typeof PageStore, context: {pageId: string; owner: unknown}): PageStore;
|
|
197
|
+
pageStore(pageId: string, context: {owner: unknown}): PageStore;
|
|
198
|
+
connectPageChannel(pageId: string, send: (wire: string) => void, context: {owner: unknown}): void;
|
|
199
|
+
disconnectPageChannel(pageId: string, context: {owner: unknown}): void;
|
|
200
|
+
sharedStore<T extends PageStore>(key: string[], factory: () => T): T;
|
|
201
|
+
closeSharedStore(key: string[]): void;
|
|
202
|
+
preparePublish(text: string, context: {owner: unknown}): [string, (() => void) | null];
|
|
203
|
+
publishToPage(topic: string, payload: Record<string, unknown>, context: {sourcePageId: string; pageId: string; owner: unknown}): void;
|
|
188
204
|
}
|