@motifx-community/ui 0.1.0-beta.13 → 0.1.0-beta.14
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/CHANGELOG.md +379 -0
- package/README.md +2 -0
- package/dist/components/chart/Chart.d.ts +6 -4
- package/dist/components/chart/ChartPlot.d.ts +2 -1
- package/dist/components/chart/plot-view.d.ts +10 -2
- package/dist/components/chat/Chat.d.ts +276 -0
- package/dist/components/chat/ChatComposer.d.ts +172 -0
- package/dist/components/chat/ChatReply.d.ts +53 -0
- package/dist/components/chat/ChatWriter.d.ts +119 -0
- package/dist/components/chat/chat-ask-form.d.ts +64 -0
- package/dist/components/chat/chat-asked-entry.d.ts +21 -0
- package/dist/components/chat/chat-attachment-entry.d.ts +18 -0
- package/dist/components/chat/chat-choices-view.d.ts +40 -0
- package/dist/components/chat/chat-command.d.ts +7 -0
- package/dist/components/chat/chat-compaction-entry.d.ts +19 -0
- package/dist/components/chat/chat-composer-props.d.ts +63 -0
- package/dist/components/chat/chat-context-changed-event-args.d.ts +8 -0
- package/dist/components/chat/chat-context-item.d.ts +14 -0
- package/dist/components/chat/chat-copy-button.d.ts +11 -0
- package/dist/components/chat/chat-entry.d.ts +20 -0
- package/dist/components/chat/chat-flow-view.d.ts +32 -0
- package/dist/components/chat/chat-form-entry.d.ts +24 -0
- package/dist/components/chat/chat-form-view.d.ts +48 -0
- package/dist/components/chat/chat-history-event-args.d.ts +31 -0
- package/dist/components/chat/chat-load-older-event-args.d.ts +17 -0
- package/dist/components/chat/chat-look.d.ts +33 -0
- package/dist/components/chat/chat-mention.d.ts +21 -0
- package/dist/components/chat/chat-message-status.d.ts +2 -0
- package/dist/components/chat/chat-message.d.ts +23 -0
- package/dist/components/chat/chat-notice-entry.d.ts +20 -0
- package/dist/components/chat/chat-option.d.ts +7 -0
- package/dist/components/chat/chat-part.d.ts +110 -0
- package/dist/components/chat/chat-plan-entry.d.ts +25 -0
- package/dist/components/chat/chat-props.d.ts +70 -0
- package/dist/components/chat/chat-question-view.d.ts +41 -0
- package/dist/components/chat/chat-request.d.ts +173 -0
- package/dist/components/chat/chat-role.d.ts +2 -0
- package/dist/components/chat/chat-search-bar.d.ts +27 -0
- package/dist/components/chat/chat-search.d.ts +33 -0
- package/dist/components/chat/chat-send-event-args.d.ts +12 -0
- package/dist/components/chat/chat-sources-entry.d.ts +22 -0
- package/dist/components/chat/chat-text-entry.d.ts +20 -0
- package/dist/components/chat/chat-text-part.d.ts +5 -0
- package/dist/components/chat/chat-thinking-entry.d.ts +32 -0
- package/dist/components/chat/chat-todo-entry.d.ts +15 -0
- package/dist/components/chat/chat-tool-entry.d.ts +66 -0
- package/dist/components/chat/chat-tool-init.d.ts +18 -0
- package/dist/components/chat/chat-turn-view.d.ts +53 -0
- package/dist/components/chat/chat-turn.d.ts +8 -0
- package/dist/components/chat/chat-value-changed-event-args.d.ts +6 -0
- package/dist/components/chat/index.d.ts +22 -0
- package/dist/components/contextmenu/ContextMenu.d.ts +4 -0
- package/dist/components/contextmenu/context-menu-props.d.ts +6 -0
- package/dist/components/datagrid/DataGrid.d.ts +0 -12
- package/dist/components/datagrid/column-type.d.ts +2 -1
- package/dist/components/datagrid/grid-column.d.ts +4 -1
- package/dist/components/datagrid/grid-date-interval.d.ts +2 -1
- package/dist/components/datagrid/grid-filter-value.d.ts +2 -7
- package/dist/components/datagrid/grid-header-filter-interval.d.ts +2 -2
- package/dist/components/datagrid/grid-settings.d.ts +6 -3
- package/dist/components/datagrid/grid-sheet-cell.d.ts +3 -6
- package/dist/components/datagrid/grid-sheet-column.d.ts +2 -9
- package/dist/components/datagrid/grid-sheet-merge.d.ts +2 -7
- package/dist/components/datagrid/grid-sheet-row.d.ts +2 -5
- package/dist/components/datagrid/grid-sheet.d.ts +3 -2
- package/dist/components/datagrid/grid-summary-item.d.ts +5 -2
- package/dist/components/datagrid/grid-summary-texts.d.ts +2 -0
- package/dist/components/datagrid/grid-summary-type.d.ts +5 -2
- package/dist/components/datagrid/grid-summary.d.ts +3 -2
- package/dist/components/datagrid/list-option.d.ts +2 -4
- package/dist/components/datagrid/model/ColumnModel.d.ts +21 -3
- package/dist/components/datagrid/model/FilterValues.d.ts +15 -7
- package/dist/components/datagrid/model/GridExport.d.ts +1 -1
- package/dist/components/datagrid/model/GridModel.d.ts +2 -2
- package/dist/components/datagrid/model/LookupTexts.d.ts +1 -1
- package/dist/components/datagrid/view/CellEditor.d.ts +1 -1
- package/dist/components/datagrid/view/ColumnStyles.d.ts +1 -1
- package/dist/components/datagrid/view/GridBody.d.ts +1 -1
- package/dist/components/datagrid/view/HeaderRow.d.ts +3 -1
- package/dist/components/draggable/Draggable.d.ts +14 -3
- package/dist/components/form/MxForm.d.ts +1 -1
- package/dist/components/markdown/Markdown.d.ts +87 -0
- package/dist/components/markdown/code-block.d.ts +57 -0
- package/dist/components/markdown/code-colorer.d.ts +11 -0
- package/dist/components/markdown/code-highlighter.d.ts +9 -0
- package/dist/components/markdown/code-run.d.ts +6 -0
- package/dist/components/markdown/diagram-renderer.d.ts +21 -0
- package/dist/components/markdown/entities.d.ts +2 -0
- package/dist/components/markdown/index.d.ts +8 -0
- package/dist/components/markdown/inline-writer.d.ts +85 -0
- package/dist/components/markdown/markdown-context.d.ts +53 -0
- package/dist/components/markdown/markdown-link-click-event-args.d.ts +9 -0
- package/dist/components/markdown/markdown-props.d.ts +40 -0
- package/dist/components/markdown/markdown-writer.d.ts +95 -0
- package/dist/components/markdown/math-block.d.ts +22 -0
- package/dist/components/markdown/math-renderer.d.ts +18 -0
- package/dist/components/markdown/safe-href.d.ts +5 -0
- package/dist/components/markdown/text-run.d.ts +7 -0
- package/dist/components/menubar/MenuBar.d.ts +4 -0
- package/dist/components/menubar/menu-bar-props.d.ts +6 -0
- package/dist/components/pivotgrid/PivotGrid.d.ts +415 -0
- package/dist/components/pivotgrid/index.d.ts +44 -0
- package/dist/components/pivotgrid/model/PivotArrangement.d.ts +29 -0
- package/dist/components/pivotgrid/model/PivotAxisWindow.d.ts +169 -0
- package/dist/components/pivotgrid/model/PivotCellRanges.d.ts +63 -0
- package/dist/components/pivotgrid/model/PivotCellWindow.d.ts +47 -0
- package/dist/components/pivotgrid/model/PivotChart.d.ts +49 -0
- package/dist/components/pivotgrid/model/PivotDisplay.d.ts +61 -0
- package/dist/components/pivotgrid/model/PivotDrill.d.ts +19 -0
- package/dist/components/pivotgrid/model/PivotExpansion.d.ts +44 -0
- package/dist/components/pivotgrid/model/PivotFields.d.ts +55 -0
- package/dist/components/pivotgrid/model/PivotFilterValues.d.ts +36 -0
- package/dist/components/pivotgrid/model/PivotLayout.d.ts +75 -0
- package/dist/components/pivotgrid/model/PivotLoader.d.ts +29 -0
- package/dist/components/pivotgrid/model/PivotSheet.d.ts +44 -0
- package/dist/components/pivotgrid/model/PivotStateStore.d.ts +19 -0
- package/dist/components/pivotgrid/model/PivotTable.d.ts +78 -0
- package/dist/components/pivotgrid/model/PivotTexts.d.ts +47 -0
- package/dist/components/pivotgrid/model/PivotWindows.d.ts +120 -0
- package/dist/components/pivotgrid/model/pivot-copy.d.ts +26 -0
- package/dist/components/pivotgrid/model/pivot-csv.d.ts +9 -0
- package/dist/components/pivotgrid/model/pivot-header-cell.d.ts +19 -0
- package/dist/components/pivotgrid/model/pivot-line-key.d.ts +17 -0
- package/dist/components/pivotgrid/model/pivot-line.d.ts +14 -0
- package/dist/components/pivotgrid/pivot-grid-area.d.ts +2 -0
- package/dist/components/pivotgrid/pivot-grid-cell-click-event-args.d.ts +5 -0
- package/dist/components/pivotgrid/pivot-grid-cell-context.d.ts +19 -0
- package/dist/components/pivotgrid/pivot-grid-cell-dbl-click-event-args.d.ts +5 -0
- package/dist/components/pivotgrid/pivot-grid-cell-range.d.ts +16 -0
- package/dist/components/pivotgrid/pivot-grid-cell-selection-changed-event-args.d.ts +6 -0
- package/dist/components/pivotgrid/pivot-grid-chart-data.d.ts +15 -0
- package/dist/components/pivotgrid/pivot-grid-chart-options.d.ts +19 -0
- package/dist/components/pivotgrid/pivot-grid-column-resized-event-args.d.ts +8 -0
- package/dist/components/pivotgrid/pivot-grid-column-resizing.d.ts +15 -0
- package/dist/components/pivotgrid/pivot-grid-column.d.ts +19 -0
- package/dist/components/pivotgrid/pivot-grid-content-ready-event-args.d.ts +6 -0
- package/dist/components/pivotgrid/pivot-grid-context-menu-preparing-event-args.d.ts +22 -0
- package/dist/components/pivotgrid/pivot-grid-copying-event-args.d.ts +10 -0
- package/dist/components/pivotgrid/pivot-grid-custom-summary-info.d.ts +15 -0
- package/dist/components/pivotgrid/pivot-grid-data-error-event-args.d.ts +5 -0
- package/dist/components/pivotgrid/pivot-grid-drill-down.d.ts +14 -0
- package/dist/components/pivotgrid/pivot-grid-expanded-event-args.d.ts +6 -0
- package/dist/components/pivotgrid/pivot-grid-expanding-event-args.d.ts +9 -0
- package/dist/components/pivotgrid/pivot-grid-export-cell.d.ts +21 -0
- package/dist/components/pivotgrid/pivot-grid-export-options.d.ts +16 -0
- package/dist/components/pivotgrid/pivot-grid-export.d.ts +17 -0
- package/dist/components/pivotgrid/pivot-grid-exported-event-args.d.ts +9 -0
- package/dist/components/pivotgrid/pivot-grid-exporting-event-args.d.ts +17 -0
- package/dist/components/pivotgrid/pivot-grid-field-chooser.d.ts +20 -0
- package/dist/components/pivotgrid/pivot-grid-field-panel.d.ts +16 -0
- package/dist/components/pivotgrid/pivot-grid-field.d.ts +110 -0
- package/dist/components/pivotgrid/pivot-grid-fields-changed-event-args.d.ts +8 -0
- package/dist/components/pivotgrid/pivot-grid-filter-value.d.ts +4 -0
- package/dist/components/pivotgrid/pivot-grid-group-interval.d.ts +7 -0
- package/dist/components/pivotgrid/pivot-grid-header-filter-items-request.d.ts +9 -0
- package/dist/components/pivotgrid/pivot-grid-header-filter-items.d.ts +7 -0
- package/dist/components/pivotgrid/pivot-grid-load-panel.d.ts +11 -0
- package/dist/components/pivotgrid/pivot-grid-props.d.ts +121 -0
- package/dist/components/pivotgrid/pivot-grid-sheet-options.d.ts +14 -0
- package/dist/components/pivotgrid/pivot-grid-sheet.d.ts +19 -0
- package/dist/components/pivotgrid/pivot-grid-state.d.ts +54 -0
- package/dist/components/pivotgrid/pivot-grid-summary-cell.d.ts +29 -0
- package/dist/components/pivotgrid/pivot-grid-summary-display-mode.d.ts +5 -0
- package/dist/components/pivotgrid/pivot-grid-summary-type.d.ts +5 -0
- package/dist/components/pivotgrid/view/PivotFieldChooser.d.ts +71 -0
- package/dist/components/pivotgrid/view/PivotFieldPanel.d.ts +92 -0
- package/dist/components/pivotgrid/view/PivotView.d.ts +263 -0
- package/dist/components/resizable/Resizable.d.ts +2 -0
- package/dist/components/ribbonmenu/RibbonKeyHints.d.ts +4 -0
- package/dist/components/ribbonmenu/RibbonMenu.d.ts +4 -1
- package/dist/components/ribbonmenu/ribbon-menu-custom-menu-opening-event-args.d.ts +8 -4
- package/dist/components/ribbonmenu/ribbon-menu-props.d.ts +6 -0
- package/dist/components/scheduler/view/TimeView.d.ts +2 -0
- package/dist/components/scrollview/ScrollView.d.ts +48 -1
- package/dist/components/scrollview/index.d.ts +1 -0
- package/dist/components/scrollview/scroll-end-changed-event-args.d.ts +7 -0
- package/dist/components/scrollview/scroll-view-props.d.ts +9 -0
- package/dist/components/splitter/Splitter.d.ts +1 -3
- package/dist/components/tabdocs/TabDocumentManager.d.ts +3 -2
- package/dist/components/textarea/TextArea.d.ts +11 -0
- package/dist/components/textarea/text-area-props.d.ts +12 -1
- package/dist/components/textbox/TextBox.d.ts +3 -0
- package/dist/components/textbox/text-box-props.d.ts +3 -0
- package/dist/components/toolbar/Toolbar.d.ts +70 -1
- package/dist/components/toolbar/index.d.ts +5 -0
- package/dist/components/toolbar/toolbar-added-item.d.ts +9 -0
- package/dist/components/toolbar/toolbar-custom-menu-opening-event-args.d.ts +23 -0
- package/dist/components/toolbar/toolbar-customization.d.ts +18 -0
- package/dist/components/toolbar/toolbar-customized-event-args.d.ts +7 -0
- package/dist/components/toolbar/toolbar-item-display.d.ts +2 -0
- package/dist/components/toolbar/toolbar-item.d.ts +11 -1
- package/dist/components/toolbar/toolbar-props.d.ts +26 -0
- package/dist/components/toolbar/toolbar-targets.d.ts +22 -0
- package/dist/core/clipboard.d.ts +11 -0
- package/dist/core/enter-key-hint.d.ts +2 -0
- package/dist/core/look-watch.d.ts +2 -2
- package/dist/core/measure/look.d.ts +1 -1
- package/dist/core/measure/on-layout.d.ts +1 -1
- package/dist/core/measure/scroll.d.ts +3 -1
- package/dist/core/save-text.d.ts +2 -0
- package/dist/core/ui-strings.d.ts +190 -0
- package/dist/excel/excel-pivot-customize-cell-args.d.ts +8 -0
- package/dist/excel/excel-pivot-export-options.d.ts +44 -0
- package/dist/excel/excel-pivot-export-result.d.ts +6 -0
- package/dist/excel/export-pivot-grid.d.ts +35 -0
- package/dist/excel/index.d.ts +4 -0
- package/dist/excel/index.js +190 -56
- package/dist/excel/index.js.map +1 -1
- package/dist/excel/write-sheet.d.ts +16 -0
- package/dist/{formula-guard-NYlpoA-t.js → formula-guard-Bx48S0u3.js} +2 -2
- package/dist/formula-guard-Bx48S0u3.js.map +1 -0
- package/dist/generated/tokens.d.ts +7527 -4097
- package/dist/index.d.ts +6 -0
- package/dist/index.js +65982 -49896
- package/dist/index.js.map +1 -1
- package/dist/locales/en.json +179 -1
- package/dist/locales/tr.json +179 -1
- package/dist/parts/customizer/Customizer.d.ts +12 -6
- package/dist/parts/customizer/customizer-display.d.ts +2 -0
- package/dist/parts/customizer/customizer-node.d.ts +5 -0
- package/dist/parts/customizer/customizer-source.d.ts +15 -3
- package/dist/parts/item-layout/layout-added-item.d.ts +2 -2
- package/dist/parts/menu/MenuList.d.ts +5 -0
- package/dist/parts/popup/Popup.d.ts +2 -0
- package/dist/{components/datagrid/model → parts/table}/CellFormatter.d.ts +8 -8
- package/dist/{components/datagrid/view → parts/table}/HeaderFilter.d.ts +16 -11
- package/dist/parts/table/OffsetList.d.ts +21 -0
- package/dist/parts/table/ScaledOffsets.d.ts +32 -0
- package/dist/{components/datagrid/model → parts/table}/ValueIntervals.d.ts +12 -5
- package/dist/{components/datagrid/model → parts/table}/ValueListAdapter.d.ts +4 -4
- package/dist/parts/table/date-interval.d.ts +2 -0
- package/dist/parts/table/date-type.d.ts +3 -0
- package/dist/{components/datagrid/model → parts/table}/filter-tree-node.d.ts +3 -1
- package/dist/parts/table/filter-value.d.ts +9 -0
- package/dist/parts/table/format-column.d.ts +14 -0
- package/dist/parts/table/header-filter-host.d.ts +27 -0
- package/dist/parts/table/option-of.d.ts +6 -0
- package/dist/parts/table/slice-sheet.d.ts +8 -0
- package/dist/parts/table/table-sheet-cell.d.ts +13 -0
- package/dist/parts/table/table-sheet-column.d.ts +11 -0
- package/dist/parts/table/table-sheet-merge.d.ts +9 -0
- package/dist/parts/table/table-sheet-row.d.ts +8 -0
- package/dist/parts/table/table-sheet.d.ts +24 -0
- package/dist/parts/table/value-interval.d.ts +6 -0
- package/dist/{components/datagrid/model → parts/table}/value-list.d.ts +7 -2
- package/dist/parts/table/value-loader.d.ts +8 -0
- package/dist/parts/table/value-option.d.ts +5 -0
- package/dist/parts/table/value-type.d.ts +5 -0
- package/dist/parts/table/values-result.d.ts +6 -0
- package/dist/parts/window/follow-pointer.d.ts +9 -3
- package/dist/parts/window-list/WindowList.d.ts +105 -0
- package/dist/parts/window-list/window-list-source.d.ts +10 -0
- package/dist/pdf/export-pivot-grid.d.ts +21 -0
- package/dist/pdf/index.d.ts +3 -0
- package/dist/pdf/index.js +143 -16
- package/dist/pdf/index.js.map +1 -1
- package/dist/pdf/pdf-customize-cell-args.d.ts +1 -1
- package/dist/pdf/pdf-pivot-cell-args.d.ts +25 -0
- package/dist/pdf/pdf-pivot-export-options.d.ts +61 -0
- package/dist/pdf/pdf-table.d.ts +38 -6
- package/dist/slice-sheet-C116M42H.js +43 -0
- package/dist/slice-sheet-C116M42H.js.map +1 -0
- package/dist/styles.css +4484 -177
- package/dist/tokens/index.d.ts +7527 -4097
- package/dist/tokens/index.js +7527 -4097
- package/dist/tokens/index.js.map +1 -1
- package/dist/{tr-BvKE8jSF.js → tr-BGbF9uCs.js} +359 -3
- package/dist/tr-BGbF9uCs.js.map +1 -0
- package/docs/accordion.md +1 -1
- package/docs/actionsheet.md +1 -1
- package/docs/appbar.md +1 -1
- package/docs/appmenu-api.md +1 -1
- package/docs/appmenu-programmatic.md +1 -1
- package/docs/appmenu-search.md +1 -1
- package/docs/appmenu.md +1 -1
- package/docs/avatar.md +1 -1
- package/docs/badge.md +1 -1
- package/docs/bar-gauge-angles.md +1 -1
- package/docs/bar-gauge-data.md +1 -1
- package/docs/bar-gauge-labels.md +1 -1
- package/docs/bar-gauge.md +1 -1
- package/docs/barcode.md +1 -1
- package/docs/bottomnavigation.md +1 -1
- package/docs/bottomsheet.md +1 -1
- package/docs/breadcrumb.md +1 -1
- package/docs/button.md +1 -1
- package/docs/buttongroup.md +1 -1
- package/docs/buttons.md +1 -1
- package/docs/calendar.md +1 -1
- package/docs/card.md +1 -1
- package/docs/chart-bars.md +1 -1
- package/docs/chart-candles.md +1 -1
- package/docs/chart-funnel.md +1 -1
- package/docs/chart-heatmap.md +1 -1
- package/docs/chart-lines.md +1 -1
- package/docs/chart-live.md +1 -1
- package/docs/chart-pie.md +1 -1
- package/docs/chart-radar.md +1 -1
- package/docs/chart-ranges.md +1 -1
- package/docs/chart-references.md +1 -1
- package/docs/chart-scatter.md +1 -1
- package/docs/chart-waterfall.md +1 -1
- package/docs/chart-zoom.md +1 -1
- package/docs/chart.md +1 -1
- package/docs/chat.md +1512 -0
- package/docs/checkbox.md +1 -1
- package/docs/chip.md +2 -2
- package/docs/choice.md +1 -1
- package/docs/colorbox.md +1 -1
- package/docs/combobox.md +1 -1
- package/docs/commands.md +1 -1
- package/docs/control.md +1 -1
- package/docs/data.md +1 -1
- package/docs/datagrid-api.md +3 -3
- package/docs/datagrid-bands.md +1 -1
- package/docs/datagrid-drag-select.md +1 -1
- package/docs/datagrid-drag.md +1 -1
- package/docs/datagrid-editing.md +1 -1
- package/docs/datagrid-filtering.md +2 -2
- package/docs/datagrid-forms.md +1 -1
- package/docs/datagrid-grouping.md +7 -7
- package/docs/datagrid-layout.md +2 -2
- package/docs/datagrid-lookups.md +1 -1
- package/docs/datagrid-master-detail.md +1 -1
- package/docs/datagrid-scrolling.md +1 -1
- package/docs/datagrid-selection.md +1 -1
- package/docs/datagrid.md +1 -1
- package/docs/datebox.md +1 -1
- package/docs/daterangebox.md +1 -1
- package/docs/display.md +1 -1
- package/docs/draggable.md +2 -2
- package/docs/drawer.md +1 -1
- package/docs/dropdown.md +1 -1
- package/docs/editors.md +1 -1
- package/docs/feedback.md +1 -1
- package/docs/fileuploader.md +1 -1
- package/docs/filterbuilder.md +1 -1
- package/docs/formlayout.md +1 -1
- package/docs/foundations.md +1 -1
- package/docs/gallery.md +1 -1
- package/docs/gauge-base.md +1 -1
- package/docs/gauge-circular.md +1 -1
- package/docs/gauge-dashboards.md +1 -1
- package/docs/gauge-linear.md +1 -1
- package/docs/gauge-markers.md +1 -1
- package/docs/gauge-marks.md +1 -1
- package/docs/gauge-ranges.md +1 -1
- package/docs/gauge-scales.md +1 -1
- package/docs/gauge.md +1 -1
- package/docs/home.md +1 -1
- package/docs/keyboard.md +1 -1
- package/docs/language.md +1 -1
- package/docs/layout.md +1 -1
- package/docs/listbox.md +1 -1
- package/docs/loadpanel.md +1 -1
- package/docs/lookup.md +1 -1
- package/docs/markdown.md +343 -0
- package/docs/menus-api.md +3 -1
- package/docs/menus-context.md +2 -1
- package/docs/menus-customization.md +5 -1
- package/docs/menus-merging.md +1 -1
- package/docs/menus-programmatic.md +1 -1
- package/docs/menus.md +1 -1
- package/docs/multiview.md +1 -1
- package/docs/navigation.md +1 -1
- package/docs/numberbox.md +1 -1
- package/docs/otpinput.md +1 -1
- package/docs/pagination.md +1 -1
- package/docs/pivotgrid-api.md +180 -0
- package/docs/pivotgrid-chart.md +132 -0
- package/docs/pivotgrid-drilldown.md +142 -0
- package/docs/pivotgrid-export.md +207 -0
- package/docs/pivotgrid-fields.md +245 -0
- package/docs/pivotgrid-keyboard.md +223 -0
- package/docs/pivotgrid-layout.md +663 -0
- package/docs/pivotgrid-panel.md +223 -0
- package/docs/pivotgrid-remote.md +397 -0
- package/docs/pivotgrid-sorting.md +379 -0
- package/docs/pivotgrid-state.md +245 -0
- package/docs/pivotgrid-summaries.md +330 -0
- package/docs/pivotgrid.md +295 -0
- package/docs/popover.md +1 -1
- package/docs/progress.md +1 -1
- package/docs/qrcode.md +1 -1
- package/docs/radiogroup.md +1 -1
- package/docs/rating.md +1 -1
- package/docs/resizable.md +1 -1
- package/docs/ribbonmenu-api.md +2 -1
- package/docs/ribbonmenu-commands.md +1 -1
- package/docs/ribbonmenu-customization.md +3 -2
- package/docs/ribbonmenu-display.md +1 -1
- package/docs/ribbonmenu-layouts.md +1 -1
- package/docs/ribbonmenu-programmatic.md +1 -1
- package/docs/ribbonmenu.md +3 -1
- package/docs/scheduler-agenda.md +1 -1
- package/docs/scheduler-big-data.md +1 -1
- package/docs/scheduler-editing.md +1 -1
- package/docs/scheduler-form.md +1 -1
- package/docs/scheduler-icalendar.md +1 -1
- package/docs/scheduler-mobile.md +1 -1
- package/docs/scheduler-recurrence.md +1 -1
- package/docs/scheduler-resources.md +1 -1
- package/docs/scheduler-timeline.md +1 -1
- package/docs/scheduler.md +1 -1
- package/docs/scrollview.md +1 -1
- package/docs/shortcuteditor.md +1 -1
- package/docs/showcase-home.md +1 -1
- package/docs/showcase-mail.md +39 -0
- package/docs/showcase.md +1 -1
- package/docs/signature.md +1 -1
- package/docs/skeleton.md +1 -1
- package/docs/slider.md +1 -1
- package/docs/sortable.md +3 -6
- package/docs/sparkline.md +1 -1
- package/docs/speeddial.md +1 -1
- package/docs/splitter.md +1 -1
- package/docs/stepper.md +1 -1
- package/docs/switch.md +1 -1
- package/docs/tabdocs-dock.md +1 -1
- package/docs/tabdocs-drag.md +1 -1
- package/docs/tabdocs-menus.md +1 -1
- package/docs/tabdocs-mobile.md +1 -1
- package/docs/tabdocs-tabs.md +1 -1
- package/docs/tabdocs-windows.md +1 -1
- package/docs/tabdocs-workspace.md +1 -1
- package/docs/tabdocs.md +1 -1
- package/docs/tabs.md +1 -1
- package/docs/tagbox.md +1 -1
- package/docs/textarea.md +4 -1
- package/docs/textbox.md +2 -1
- package/docs/tileview.md +1 -1
- package/docs/timeline.md +1 -1
- package/docs/toast.md +1 -1
- package/docs/tokens.md +687 -1
- package/docs/toolbar-api.md +80 -0
- package/docs/toolbar-customization.md +261 -0
- package/docs/toolbar.md +4 -52
- package/docs/tooltip.md +1 -1
- package/docs/treeview.md +1 -1
- package/docs/validationsummary.md +1 -1
- package/docs/window.md +1 -1
- package/docs/wizard.md +1 -1
- package/llms-full.txt +18989 -12344
- package/llms.txt +29 -2
- package/package.json +2 -2
- package/dist/components/datagrid/model/date-type.d.ts +0 -3
- package/dist/components/datagrid/model/option-of.d.ts +0 -6
- package/dist/components/datagrid/model/value-loader.d.ts +0 -8
- package/dist/formula-guard-NYlpoA-t.js.map +0 -1
- package/dist/tr-BvKE8jSF.js.map +0 -1
- /package/dist/{components/datagrid/model → core}/text-ranges.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/ExtraHeights.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/ScrollGeometry.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/cell-list.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/date-like.d.ts +0 -0
- /package/dist/{components/datagrid/view → parts/table}/editor-value.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/formula-guard.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/parse-date-text.d.ts +0 -0
- /package/dist/{components/datagrid/model → parts/table}/same-value.d.ts +0 -0
package/docs/chat.md
ADDED
|
@@ -0,0 +1,1512 @@
|
|
|
1
|
+
# Chat
|
|
2
|
+
|
|
3
|
+
> AI chat · @motifx-community/ui 0.1.0-beta.14
|
|
4
|
+
|
|
5
|
+
A conversation with a model, drawn by turn: the user’s question, then what the model did, in order — its thinking, text, tool calls, to-do list — each with a dot at its start. The chat draws everything, and your app talks to the model and writes the reply. There is no built-in provider: any streaming API connects through `reply()`.
|
|
6
|
+
|
|
7
|
+
## Import
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { Chat } from '@motifx-community/ui';
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import type { ChatMessage, ChatPart, ChatPlanAnswer, ChatQuestion, ChatQuestionsAnswer, ChatTodoItem } from '@motifx-community/ui';
|
|
17
|
+
import { ModelError, type Model, type ModelEvent, type ModelMessage } from './model';
|
|
18
|
+
|
|
19
|
+
/** Waits, or rejects as soon as `signal` is aborted. */
|
|
20
|
+
export function pause(ms: number, signal: AbortSignal): Promise<void> {
|
|
21
|
+
return new Promise((done, fail) => {
|
|
22
|
+
if (signal.aborted) {
|
|
23
|
+
fail(signal.reason);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const timer = setTimeout(done, ms);
|
|
27
|
+
signal.addEventListener('abort', () => {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
fail(signal.reason);
|
|
30
|
+
}, { once: true });
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Writes `text` a few characters at a time, as a model streams. */
|
|
35
|
+
export async function typed(write: (delta: string) => void, text: string, signal: AbortSignal): Promise<void> {
|
|
36
|
+
for (let at = 0; at < text.length; ) {
|
|
37
|
+
const piece = text.slice(at, at + 2 + Math.floor(Math.random() * 6));
|
|
38
|
+
at += piece.length;
|
|
39
|
+
write(piece);
|
|
40
|
+
await pause(22, signal);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const QUESTIONS: ChatQuestion[] = [
|
|
45
|
+
{
|
|
46
|
+
text: 'When should the call run?',
|
|
47
|
+
header: 'Timing',
|
|
48
|
+
options: [
|
|
49
|
+
{ label: 'After the last call', description: 'Waits until the calls stop', preview: '```\ncalls x xx x\nruns ● ●\n```' },
|
|
50
|
+
{ label: 'On the first call', description: 'Runs at once, then waits', preview: '```\ncalls x xx x\nruns ● ●\n```' }
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
{ text: 'Anything else to add?', header: 'Extras', multiple: true, options: [{ label: 'A cancel() method' }, { label: 'A test for many calls' }] }
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const TASKS = ['Read the debounce helper', 'Run its tests', 'Fix the failing case', 'Commit the fix'];
|
|
57
|
+
|
|
58
|
+
/** What a model that searches names as its sources. */
|
|
59
|
+
const SOURCES = [
|
|
60
|
+
{ title: 'clearTimeout()', url: 'https://developer.mozilla.org/docs/Web/API/Window/clearTimeout', snippet: 'Cancels a timeout previously established by calling setTimeout().' },
|
|
61
|
+
{ title: 'src/debounce.ts', snippet: 'The helper, as it was read.' }
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
const ANSWER = `Fixed. The returned function started a new timer on every call **without clearing the old one**, so each call ran:
|
|
65
|
+
|
|
66
|
+
\`\`\`ts
|
|
67
|
+
return () => {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
timer = setTimeout(run, wait);
|
|
70
|
+
};
|
|
71
|
+
\`\`\`
|
|
72
|
+
|
|
73
|
+
| Test | Before | After |
|
|
74
|
+
|:-----|:------:|:-----:|
|
|
75
|
+
| runs once after the wait | ✓ | ✓ |
|
|
76
|
+
| runs once for many calls | × | ✓ |
|
|
77
|
+
|
|
78
|
+
The two other callers (\`search.ts\`, \`resize.ts\`) need no change.[^1]`;
|
|
79
|
+
|
|
80
|
+
const NOTE = '[^1]: Both already wait for the last call: `search.ts` through its own timer, `resize.ts` through `requestAnimationFrame`.';
|
|
81
|
+
|
|
82
|
+
/** The to-do list with `done` tasks done and the next one under way. */
|
|
83
|
+
function tasks(done: number): ChatTodoItem[] {
|
|
84
|
+
return TASKS.map((text, i) => ({ text, status: i < done ? 'done' : i === done ? 'active' : 'pending' }));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Stands in for your model: a scripted agent that answers anything the same way, one step a request, reading what its
|
|
89
|
+
* calls returned. It asks two questions, shows a plan, keeps a to-do list, reads a file, runs the tests (one fails),
|
|
90
|
+
* edits the file (its arguments arrive in pieces), sends a sub-agent to look around, commits through a git server
|
|
91
|
+
* that asks for the message, and answers, naming its sources. What the user says to the questions, the plan, the
|
|
92
|
+
* permission and the commit changes what it does. Its very first request is refused as busy once, as a provider does
|
|
93
|
+
* at times: the agent tries it again.
|
|
94
|
+
*/
|
|
95
|
+
export class FakeModel implements Model {
|
|
96
|
+
private calls = 0;
|
|
97
|
+
private refused = false;
|
|
98
|
+
|
|
99
|
+
async *stream(history: readonly ModelMessage[], signal: AbortSignal): AsyncIterable<ModelEvent> {
|
|
100
|
+
if (!this.refused) {
|
|
101
|
+
this.refused = true;
|
|
102
|
+
await pause(300, signal);
|
|
103
|
+
throw new ModelError('Overloaded', true);
|
|
104
|
+
}
|
|
105
|
+
// This turn: from the user's last message on.
|
|
106
|
+
let start = history.length - 1;
|
|
107
|
+
while (start > 0 && history[start].role !== 'user') start--;
|
|
108
|
+
const turn = history.slice(start);
|
|
109
|
+
const names = new Map<string, string>();
|
|
110
|
+
const results = new Map<string, string>();
|
|
111
|
+
for (const m of turn) {
|
|
112
|
+
if (m.role === 'assistant') for (const c of m.calls) names.set(c.id, c.name);
|
|
113
|
+
else if (m.role === 'tool') results.set(names.get(m.id) ?? '', m.result);
|
|
114
|
+
}
|
|
115
|
+
const asked = results.get('ask_user');
|
|
116
|
+
const answers = asked ? (JSON.parse(asked) as ChatQuestionsAnswer) : null;
|
|
117
|
+
const timing = ((!answers?.declined && answers?.answers.find((a) => a.id === '1')?.values[0]) || 'After the last call').toLowerCase();
|
|
118
|
+
switch (turn.filter((m) => m.role === 'assistant').length) {
|
|
119
|
+
case 0: {
|
|
120
|
+
const question = turn[0].role === 'user' ? turn[0].text : '';
|
|
121
|
+
yield* this.say('thinking', `The question: "${question}". Before I start I need to know how the helper should behave.`, signal);
|
|
122
|
+
await pause(400, signal);
|
|
123
|
+
yield* this.call('ask_user', { questions: QUESTIONS }, signal);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
case 1:
|
|
127
|
+
yield* this.call('plan', { plan: `1. Read \`src/debounce.ts\`\n2. Run its tests to see what fails\n3. Make it run **${timing}**\n4. Check the other callers` }, signal);
|
|
128
|
+
return;
|
|
129
|
+
case 2: {
|
|
130
|
+
const plan = JSON.parse(results.get('plan') ?? '{}') as ChatPlanAnswer;
|
|
131
|
+
if (plan.choice === 'no') {
|
|
132
|
+
yield* this.say('text', plan.text ? `All right, I will plan again with that in mind: *${plan.text}*.` : 'All right: tell me what to change in the plan.', signal);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
yield* this.call('todo', { items: tasks(0) }, signal);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
case 3:
|
|
139
|
+
yield* this.call('Read', { path: 'src/debounce.ts' }, signal);
|
|
140
|
+
return;
|
|
141
|
+
case 4:
|
|
142
|
+
yield* this.call('todo', { items: tasks(1) }, signal);
|
|
143
|
+
return;
|
|
144
|
+
case 5:
|
|
145
|
+
yield* this.call('Bash', { command: 'npm test -- debounce', description: 'Run the tests' }, signal);
|
|
146
|
+
return;
|
|
147
|
+
case 6: {
|
|
148
|
+
const ran = results.get('Bash') ?? '';
|
|
149
|
+
if (ran.startsWith('The user did not allow it')) {
|
|
150
|
+
yield* this.say('text', 'I did not run the tests. What should I do instead?', signal);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
yield* this.say('thinking', 'The returned function starts a new timer on every call and never clears the old one.', signal);
|
|
154
|
+
yield* this.call('Edit', { path: 'src/debounce.ts', old: ' timer = setTimeout(run, wait);', new: ' clearTimeout(timer);\n timer = setTimeout(run, wait);' }, signal, 4);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
case 7:
|
|
158
|
+
yield* this.call('Task', { description: 'Look for other callers' }, signal);
|
|
159
|
+
return;
|
|
160
|
+
case 8:
|
|
161
|
+
yield* this.call('todo', { items: tasks(3) }, signal);
|
|
162
|
+
return;
|
|
163
|
+
case 9:
|
|
164
|
+
yield* this.call('commit', {}, signal);
|
|
165
|
+
return;
|
|
166
|
+
case 10:
|
|
167
|
+
yield* this.call('todo', { items: tasks(4) }, signal);
|
|
168
|
+
return;
|
|
169
|
+
default: {
|
|
170
|
+
const commit = results.get('commit') ?? '';
|
|
171
|
+
yield* this.say('text', `${ANSWER}\n\nIt now runs ${timing}. ${commit}\n\n${NOTE}`, signal);
|
|
172
|
+
for (const source of SOURCES) yield { type: 'source', ...source };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private async *say(type: 'thinking' | 'text', text: string, signal: AbortSignal): AsyncIterable<ModelEvent> {
|
|
178
|
+
for (let at = 0; at < text.length; ) {
|
|
179
|
+
const delta = text.slice(at, at + 2 + Math.floor(Math.random() * 6));
|
|
180
|
+
at += delta.length;
|
|
181
|
+
yield { type, delta };
|
|
182
|
+
await pause(22, signal);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** A call, its arguments in `pieces` that take a while each, as a long argument arrives. */
|
|
187
|
+
private async *call(name: string, args: unknown, signal: AbortSignal, pieces = 1): AsyncIterable<ModelEvent> {
|
|
188
|
+
const id = `call-${++this.calls}`;
|
|
189
|
+
yield { type: 'tool-start', id, name };
|
|
190
|
+
const json = JSON.stringify(args);
|
|
191
|
+
const size = Math.ceil(json.length / pieces);
|
|
192
|
+
for (let at = 0; at < json.length; at += size) {
|
|
193
|
+
yield { type: 'tool-args', id, delta: json.slice(at, at + size) };
|
|
194
|
+
if (pieces > 1) await pause(350, signal);
|
|
195
|
+
}
|
|
196
|
+
yield { type: 'tool-end', id };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const TOPICS = ['the router', 'the date box', 'a flaky test', 'the build', 'the grid filter', 'dark mode', 'the release notes', 'a slow page'];
|
|
201
|
+
|
|
202
|
+
/** One turn of an earlier conversation, as a server would return it. */
|
|
203
|
+
function turn(n: number, at: Date): ChatMessage[] {
|
|
204
|
+
const topic = TOPICS[n % TOPICS.length];
|
|
205
|
+
const parts: ChatPart[] = [{ kind: 'thinking', seconds: 1 + (n % 5), notes: [`Looking at ${topic}`] }];
|
|
206
|
+
if (n % 2 === 0) parts.push({ kind: 'tool', id: `t${n}`, name: 'Read', title: `src/${topic.replace(/\s/g, '-')}.ts`, status: 'done', density: 'line', input: 'src/…', output: '…' });
|
|
207
|
+
if (n % 5 === 0) {
|
|
208
|
+
parts.push({
|
|
209
|
+
kind: 'questions',
|
|
210
|
+
id: `q${n}`,
|
|
211
|
+
questions: [{ id: '1', text: 'Which file should I start with?', options: [{ label: 'The view' }, { label: 'The model' }] }],
|
|
212
|
+
answer: { declined: false, answers: [{ id: '1', values: ['The model'] }] }
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (n % 3 === 0) {
|
|
216
|
+
parts.push({
|
|
217
|
+
kind: 'tool',
|
|
218
|
+
id: `b${n}`,
|
|
219
|
+
name: 'Bash',
|
|
220
|
+
title: 'Run the tests',
|
|
221
|
+
status: 'done',
|
|
222
|
+
look: 'terminal',
|
|
223
|
+
input: 'npm test',
|
|
224
|
+
output: ' ✓ 42 passed\n Tests 42 passed (42)',
|
|
225
|
+
permission: { answer: { choice: 'yes' } }
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
let said = `Turn ${n}: looked at **${topic}**.${n % 4 === 0 ? `\n\n- checked the cause\n- wrote the fix\n- ran the tests\n\n\`\`\`ts\nexport const answer${n} = ${n};\n\`\`\`` : ' It is fine as it is.'}`;
|
|
229
|
+
// A long block now and then: the chat shows its first lines (`codeLines`) and a button for the rest.
|
|
230
|
+
if (n % 7 === 0) said += `\n\nThe whole file:\n\n\`\`\`ts\n${Array.from({ length: 24 }, (_, i) => `export const step${i + 1} = () => check('${topic}', ${i + 1});`).join('\n')}\n\`\`\``;
|
|
231
|
+
parts.push({ kind: 'text', text: said });
|
|
232
|
+
return [
|
|
233
|
+
{ id: `q${n}`, role: 'user', time: at, parts: [{ kind: 'text', text: `Can you check ${topic}? (${n})` }] },
|
|
234
|
+
{ id: `a${n}`, role: 'assistant', time: new Date(at.getTime() + 20_000), parts }
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let first = 0;
|
|
239
|
+
|
|
240
|
+
/** The last `count` turns of a long conversation: the chat opens at its end. */
|
|
241
|
+
export function history(count: number): ChatMessage[] {
|
|
242
|
+
const start = Date.now() - count * 120_000;
|
|
243
|
+
first = 1000;
|
|
244
|
+
const messages: ChatMessage[] = [];
|
|
245
|
+
for (let i = 0; i < count; i++) messages.push(...turn(first + i, new Date(start + i * 120_000)));
|
|
246
|
+
return messages;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** The page of turns before the first one shown. */
|
|
250
|
+
export function olderPage(): ChatMessage[] {
|
|
251
|
+
const messages: ChatMessage[] = [];
|
|
252
|
+
const start = Date.now() - (first + 20) * 120_000;
|
|
253
|
+
for (let i = 20; i > 0; i--) messages.push(...turn(first - i, new Date(start + (20 - i) * 120_000)));
|
|
254
|
+
first -= 20;
|
|
255
|
+
return messages;
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Streaming replies
|
|
260
|
+
|
|
261
|
+
Start a reply in `onSend` and write each piece with `text()` as it arrives. Each piece shows the moment it comes, and nothing already drawn is drawn again (see Markdown). Pass `signal` to the request so that stopping the reply, or removing the chat, cancels it.
|
|
262
|
+
|
|
263
|
+
```tsx
|
|
264
|
+
<Chat ref={(c) => (chat = c)} onSend={async (e) => {
|
|
265
|
+
const reply = chat.reply();
|
|
266
|
+
try {
|
|
267
|
+
const res = await fetch('/api/chat', { method: 'POST', body: JSON.stringify({ text: e.text }), signal: reply.signal });
|
|
268
|
+
const reader = res.body!.pipeThrough(new TextDecoderStream()).getReader();
|
|
269
|
+
for (let r = await reader.read(); !r.done; r = await reader.read()) reply.text(r.value);
|
|
270
|
+
reply.done();
|
|
271
|
+
} catch (error) {
|
|
272
|
+
if (!reply.signal.aborted) reply.fail(error as Error);
|
|
273
|
+
}
|
|
274
|
+
}} />
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
| ChatReply | Description |
|
|
278
|
+
| --- | --- |
|
|
279
|
+
| `text(delta)` | Adds markdown text. What it completes takes its form, and the rest shows as text at once (see Markdown). |
|
|
280
|
+
| `thinking(delta?)` | Adds to the thinking being written, or starts one. It shows “Thinking 3s” while it runs and “Thought for 3s” after; a click opens what it wrote. Anything else written ends it. A thinking with nothing written that ended within a second is not drawn. |
|
|
281
|
+
| `note(text)` | A short note on what the thinking is about. The newest stands beside the thinking line while it runs; all of them are inside it. |
|
|
282
|
+
| `tool(init)` | Starts a tool call and returns a `ChatToolCall`: `progress(file?, bytes?)` while its arguments arrive (`status: 'assembling'`), `start(input?, title?)`, `output(delta)`, `done(output?)`, `fail(error?)`. A sub-agent writes its own `text()` and `tool()` into the call, and they are drawn inside it. |
|
|
283
|
+
| `todo(items)` | The to-do list as it stands (`{ text, status: 'pending' \| 'active' \| 'done' }`). Written again right after, it changes in place. |
|
|
284
|
+
| `notice(tone, text?)` | A line in the flow: `retry`, `info`, `stopped` or `error`. |
|
|
285
|
+
| `retrying(seconds, { attempt?, attempts?, reason? })` | The request failed and is tried again in `seconds`: the line counts down to it, and says which try it is of how many and why (“Overloaded · Trying again in 4s · 3/4”). |
|
|
286
|
+
| `compaction(summary?)` | The conversation so far was summed up for the model: a line across the flow, “Conversation summarized”, which opens the summary. |
|
|
287
|
+
| `call.permission({ always? })` | Asks the user whether a tool call may run, and returns a `Promise<ChatPermissionAnswer>` (`{ choice: 'yes' \| 'always' \| 'no', text? }`). The call waits open, and its choices take the composer’s place: “Yes”, `always` when given (“Yes, and allow npm commands”), and “No” with a box for what to do instead. Then it runs or is denied. |
|
|
288
|
+
| `plan(text, choices?)` | Shows a plan (markdown) and asks to go on, its choices in the composer’s place. Returns a `Promise<ChatPlanAnswer>`: the `value` of the choice taken (by default one, “Yes, go ahead”, value `yes`), or `no` with what to change. |
|
|
289
|
+
| `questions(list)` | Asks the user questions and returns a `Promise<ChatQuestionsAnswer>`: `{ declined, answers: [{ id, values }] }`. The form takes the composer’s place; the flow keeps the questions and their answers. |
|
|
290
|
+
| `form(message, schema, { source? })` | Asks the user to fill in a form, as an MCP server’s elicitation asks: `schema` is its `requestedSchema`, `source` who asks. Returns a `Promise<ChatFormAnswer>`, the elicitation’s result: `{ action: 'accept', content }`, `decline` or `cancel`. On a tool call (`call.form()`) the flow keeps it inside the call. |
|
|
291
|
+
| `sources(items)` | The sources the reply names (`{ title, url?, snippet? }`), as numbered chips. Right after other sources they join them, each once. |
|
|
292
|
+
| `status(text)` | What the model does now, in a word or two. The line under the conversation says it, with the seconds since the reply started. |
|
|
293
|
+
| `done()` | The reply is complete. |
|
|
294
|
+
| `stop()` | Stops the reply where it is: `signal` is aborted, and the flow says it stopped. |
|
|
295
|
+
| `fail(error?)` | The reply failed: the flow says `error`, or the kit text, as an alert. |
|
|
296
|
+
| `signal` | An `AbortSignal`, aborted when the user stops the reply or the chat is disposed. Pass it to the request. |
|
|
297
|
+
| `message` | The message being written. Its `parts`, `status` and `error` are updated as the reply arrives. |
|
|
298
|
+
| `isOpen` | `true` until the reply is done, stopped or failed. |
|
|
299
|
+
|
|
300
|
+
### ChatMessage
|
|
301
|
+
|
|
302
|
+
| Field | Type | Description |
|
|
303
|
+
| --- | --- | --- |
|
|
304
|
+
| `id` | `string \| number` | Identifies the message. |
|
|
305
|
+
| `role` | `'user' \| 'assistant' \| 'system'` | Who wrote it. Each user message starts a turn; the messages after it, up to the next one, are its flow. |
|
|
306
|
+
| `parts` | `ChatPart[]` | The pieces of the message, in order: `text` (markdown), `thinking`, `tool` (with `permission` when it asked), `todo`, `notice`, `plan`, `questions`, `form`, `sources`, `attachment`, `compaction`. A request’s `answer` is written when the user answers, so a turn drawn again shows it. A user message shows its text as it was typed. |
|
|
307
|
+
| `time` | `Date` | Shown above the question, and above the first text of a reply. |
|
|
308
|
+
| `status` | `'streaming' \| 'done' \| 'stopped' \| 'error'` | The default is `done`. |
|
|
309
|
+
| `error` | `string` | Shown under a message whose status is `error`. |
|
|
310
|
+
| `feedback` | `'good' \| 'bad'` | What the user said of the reply with its buttons (`onFeedback`); a turn drawn again shows it. |
|
|
311
|
+
|
|
312
|
+
## The composer
|
|
313
|
+
|
|
314
|
+
The box and its tools sit in one field. Each tool shows when it is given, through the chat’s `composer` prop or `chat.composer`. Files (`attachments`, `accept`) are picked with the attach button, pasted or dropped, each a chip above the box, and go with the message (`e.attachments`). What the app adds by itself, such as the open file or a selection (`contextItems`), shows as chips the user turns off or takes off; those on go with the message (`e.contextItems`). Both show as chips under the question. `/` at the start of the box lists the `commands`, and `@` asks the app what fits the word after it (`onMention`, with `e.items` or `e.promise`). The mode (`modes`, Shift+Tab goes to the next), the model and its thinking (`models`, `thinking`) are menus; `usage` shows how full the context is. Arrow Up at the start of the box brings back what was sent.
|
|
315
|
+
|
|
316
|
+
```tsx
|
|
317
|
+
<Chat composer={{
|
|
318
|
+
attachments: true,
|
|
319
|
+
accept: 'image/*,.pdf,.md',
|
|
320
|
+
contextItems: () => this.editor.selections,
|
|
321
|
+
commands: [{ name: 'review', description: 'Review the changes' }],
|
|
322
|
+
onMention: (e) => (e.promise = this.files.search(e.query)),
|
|
323
|
+
modes: [{ value: 'ask', label: 'Ask before edits' }, { value: 'auto', label: 'Edit automatically' }],
|
|
324
|
+
onModeChanged: (e) => (this.agent.mode = e.value),
|
|
325
|
+
models: this.models,
|
|
326
|
+
thinking: true,
|
|
327
|
+
usage: () => this.agent.usage,
|
|
328
|
+
}} onSend={(e) => this.agent.answer(chat.reply(), e.text, e.attachments, e.contextItems)} />
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## Actions and history
|
|
332
|
+
|
|
333
|
+
A question’s actions copy it, edit it in place and send it again, go back to before it (its text back in the box) and, with `onFork`, start another conversation from it. Going back and editing remove the question and what came after it: `onRewind` says what goes, so your model forgets it too. At the end of a turn the reply can be copied and, with `onFeedback`, called good or bad (pressed again, taken back; it is kept in `message.feedback`). The latest reply, when it failed or stopped, is tried again with `onRetry`, and with `onRegenerate` any latest reply that ended can be asked for again: it goes, and you write the new one. While a request is tried again, `reply.retrying()` counts down to it; `reply.compaction()` marks where the conversation was summed up.
|
|
334
|
+
|
|
335
|
+
```tsx
|
|
336
|
+
<Chat
|
|
337
|
+
onRewind={(e) => this.agent.rewind(e.removed.filter((m) => m.role === 'user').length)}
|
|
338
|
+
onFork={(e) => this.sessions.open(e.messages)}
|
|
339
|
+
onRetry={() => void this.agent.again(chat.reply())}
|
|
340
|
+
onRegenerate={() => void this.agent.again(chat.reply())}
|
|
341
|
+
onFeedback={(e) => this.api.feedback(e.message.id, e.feedback)}
|
|
342
|
+
/>
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## Search
|
|
346
|
+
|
|
347
|
+
Ctrl+F in the chat opens a search over every message, the turns that are not on the page too: what the user wrote, the model’s texts and its plans, without case. The result nearest the end shows first, as in a terminal; Enter goes up to older ones and Shift+Enter down. Going to a result draws its turn when it is off the page and marks the block that holds it: a paragraph, a heading, a code block, a table row or the question’s box. From code, `find()`, `findNext()` and `findPrevious()` do the same.
|
|
348
|
+
|
|
349
|
+
```tsx
|
|
350
|
+
const count = chat.find('timeout');
|
|
351
|
+
chat.findPrevious();
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Phones and tablets
|
|
355
|
+
|
|
356
|
+
On a touch screen Enter starts a new line and the send button sends, and the keyboard’s Enter key says so. While the user writes, an on-screen keyboard that covers the chat’s end gives the chat that much room at its end, so the box stands on the keyboard and the conversation, at its end, above it. The chat also keeps clear of the home indicator and the notch where the page lays out to the edges (`viewport-fit=cover`).
|
|
357
|
+
|
|
358
|
+
## Connecting a model
|
|
359
|
+
|
|
360
|
+
The chat has no provider. Your server, or the provider’s SDK, reads the model’s stream and gives five things, which most providers send in their own words: thinking, text, the start of a tool call, its arguments as they arrive, and its end; a model that searches names its sources too (`model.ts`). The loop below is the code to copy (`connect.ts`): it writes the stream into the reply as it comes, runs the calls the model made, asking first where a tool says so and remembering “always”, gives the model what they returned, and asks again until the model answers without calling anything. Asking the user, the plan and the to-do list are tools of your model too: their `run` calls `reply.questions()`, `reply.plan()` and `reply.todo()`, and what the user said goes back to the model as the tool’s result. The demo above runs this code against a model that stands in for yours (`fake-model.ts`, `demo-tools.ts`).
|
|
361
|
+
|
|
362
|
+
```tsx
|
|
363
|
+
import type { ChatReply, ChatToolCall, ChatToolDensity, ChatToolLook } from '@motifx-community/ui';
|
|
364
|
+
import { ModelError, type Model, type ModelCall, type ModelMessage } from './model';
|
|
365
|
+
|
|
366
|
+
/** How many times a request that failed before anything came is sent, in all; the waits double from a second. */
|
|
367
|
+
const TRIES = 4;
|
|
368
|
+
|
|
369
|
+
/** A tool of the app: how its call reads in the chat, whether it asks first, and what it does. */
|
|
370
|
+
export interface AppTool {
|
|
371
|
+
/** The name a person reads; by default the tool's name. */
|
|
372
|
+
label?: string;
|
|
373
|
+
look?: ChatToolLook;
|
|
374
|
+
density?: ChatToolDensity;
|
|
375
|
+
/** What a call does, in a line, from its arguments: a file, a command. */
|
|
376
|
+
title?: (args: Record<string, unknown>) => string;
|
|
377
|
+
/** The call has no row of its own: the tool draws into the reply itself (questions, a plan, the to-do list). */
|
|
378
|
+
quiet?: boolean;
|
|
379
|
+
/** Asks the user before it runs. A text is the choice that stops asking ("Yes, and allow npm test"). */
|
|
380
|
+
ask?: boolean | string;
|
|
381
|
+
/** Runs the call; what it returns goes back to the model. `call` is its row: write output there as it comes. */
|
|
382
|
+
run(args: Record<string, unknown>, reply: ChatReply, call: ChatToolCall | null): Promise<string>;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** A call while it is read and run. */
|
|
386
|
+
interface Running extends ModelCall {
|
|
387
|
+
call: ChatToolCall | null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Connects a chat to a model: the conversation the model reads, the tools it may call, and one turn. The model's
|
|
392
|
+
* stream goes into the reply as it comes; then the calls it made run (asking first where a tool says so), what they
|
|
393
|
+
* returned goes back, and the model is asked again, until it answers without calling anything. A request the provider
|
|
394
|
+
* refused for a while (`ModelError.retryable`) is tried again after a wait the chat counts down, when nothing of it had
|
|
395
|
+
* come yet.
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* const agent = new Agent(model, tools);
|
|
399
|
+
* <Chat ref={c => (chat = c)} onSend={e => void agent.answer(chat.reply(), e.text)} />
|
|
400
|
+
*/
|
|
401
|
+
export class Agent {
|
|
402
|
+
/** What the model reads. Load it with the conversation's messages when a saved one opens. */
|
|
403
|
+
readonly history: ModelMessage[] = [];
|
|
404
|
+
/** `auto`: the tools that ask first run without asking (the composer's mode, see `onModeChanged`). */
|
|
405
|
+
mode = 'ask';
|
|
406
|
+
/** Tools the user allowed for the rest of the conversation. */
|
|
407
|
+
private readonly allowed = new Set<string>();
|
|
408
|
+
|
|
409
|
+
constructor(private readonly model: Model, private readonly tools: Record<string, AppTool>) {}
|
|
410
|
+
|
|
411
|
+
/** Answers what the user sent, into `reply`. */
|
|
412
|
+
async answer(reply: ChatReply, text: string): Promise<void> {
|
|
413
|
+
this.history.push({ role: 'user', text });
|
|
414
|
+
await this.run(reply);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Answers the user's last message again, into `reply` (the chat's `onRetry`, `onRegenerate`): what the model said to it goes. */
|
|
418
|
+
async again(reply: ChatReply): Promise<void> {
|
|
419
|
+
this.history.splice(this.lastQuestion(0) + 1);
|
|
420
|
+
await this.run(reply);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Forgets the user's last `questions` messages and what came after them, as the chat went back (`onRewind`). */
|
|
424
|
+
rewind(questions: number): void {
|
|
425
|
+
if (questions > 0) this.history.splice(Math.max(0, this.lastQuestion(questions - 1)));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** Where the user's message is, `skip` of them back from the last; `-1` when there is none. */
|
|
429
|
+
private lastQuestion(skip: number): number {
|
|
430
|
+
for (let i = this.history.length - 1; i >= 0; i--) if (this.history[i].role === 'user' && skip-- === 0) return i;
|
|
431
|
+
return -1;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private async run(reply: ChatReply): Promise<void> {
|
|
435
|
+
try {
|
|
436
|
+
for (let calls = await this.read(reply); calls.length; calls = await this.read(reply)) {
|
|
437
|
+
for (const c of calls) this.history.push({ role: 'tool', id: c.id, result: await this.use(reply, c) });
|
|
438
|
+
}
|
|
439
|
+
reply.done();
|
|
440
|
+
} catch (error) {
|
|
441
|
+
// Stopped by the user: the chat already says so.
|
|
442
|
+
if (!reply.signal.aborted) reply.fail(error as Error);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** One request, tried again while it fails before anything came and the provider says it may work later. */
|
|
447
|
+
private async read(reply: ChatReply): Promise<Running[]> {
|
|
448
|
+
for (let attempt = 1; ; attempt++) {
|
|
449
|
+
const heard = { any: false };
|
|
450
|
+
try {
|
|
451
|
+
return await this.readOnce(reply, heard);
|
|
452
|
+
} catch (error) {
|
|
453
|
+
if (reply.signal.aborted || heard.any || attempt >= TRIES || !(error instanceof ModelError) || !error.retryable) throw error;
|
|
454
|
+
const seconds = 2 ** (attempt - 1);
|
|
455
|
+
reply.retrying(seconds, { attempt: attempt + 1, attempts: TRIES, reason: error.message });
|
|
456
|
+
await this.wait(seconds * 1000, reply.signal);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** One request: its events into the reply, and the calls it made. */
|
|
462
|
+
private async readOnce(reply: ChatReply, heard: { any: boolean }): Promise<Running[]> {
|
|
463
|
+
const calls = new Map<string, Running>();
|
|
464
|
+
let said = '';
|
|
465
|
+
reply.status('Thinking');
|
|
466
|
+
for await (const e of this.model.stream(this.history, reply.signal)) {
|
|
467
|
+
heard.any = true;
|
|
468
|
+
switch (e.type) {
|
|
469
|
+
case 'thinking':
|
|
470
|
+
reply.thinking(e.delta);
|
|
471
|
+
break;
|
|
472
|
+
case 'text':
|
|
473
|
+
said += e.delta;
|
|
474
|
+
reply.text(e.delta);
|
|
475
|
+
break;
|
|
476
|
+
case 'tool-start': {
|
|
477
|
+
const tool = this.tools[e.name];
|
|
478
|
+
const call = tool?.quiet ? null : reply.tool({ id: e.id, name: e.name, label: tool?.label, look: tool?.look, density: tool?.density, status: 'assembling' });
|
|
479
|
+
calls.set(e.id, { id: e.id, name: e.name, args: '', call });
|
|
480
|
+
reply.status(tool?.label ?? e.name);
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
case 'tool-args': {
|
|
484
|
+
const c = calls.get(e.id)!;
|
|
485
|
+
c.args += e.delta;
|
|
486
|
+
c.call?.progress(undefined, c.args.length);
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
case 'tool-end': {
|
|
490
|
+
const c = calls.get(e.id)!;
|
|
491
|
+
c.call?.start(c.args, this.tools[c.name]?.title?.(this.parse(c.args)));
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
case 'source':
|
|
495
|
+
reply.sources([{ title: e.title, url: e.url, snippet: e.snippet }]);
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
const made = [...calls.values()];
|
|
500
|
+
this.history.push({ role: 'assistant', text: said, calls: made.map(({ id, name, args }) => ({ id, name, args })) });
|
|
501
|
+
return made;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** Runs a call, asking first when its tool says so and the user has not allowed it for good. */
|
|
505
|
+
private async use(reply: ChatReply, c: Running): Promise<string> {
|
|
506
|
+
const tool = this.tools[c.name];
|
|
507
|
+
if (!tool) {
|
|
508
|
+
c.call?.fail(`No tool named ${c.name}`);
|
|
509
|
+
return `There is no tool named ${c.name}.`;
|
|
510
|
+
}
|
|
511
|
+
if (tool.ask && c.call && this.mode !== 'auto' && !this.allowed.has(c.name)) {
|
|
512
|
+
const said = await c.call.permission({ always: typeof tool.ask === 'string' ? tool.ask : undefined });
|
|
513
|
+
if (said.choice === 'always') this.allowed.add(c.name);
|
|
514
|
+
if (said.choice === 'no') return said.text ? `The user did not allow it, and said: ${said.text}` : 'The user did not allow it.';
|
|
515
|
+
}
|
|
516
|
+
reply.status(tool.label ?? c.name);
|
|
517
|
+
try {
|
|
518
|
+
const out = await tool.run(this.parse(c.args), reply, c.call);
|
|
519
|
+
c.call?.done(out);
|
|
520
|
+
return out;
|
|
521
|
+
} catch (error) {
|
|
522
|
+
if (reply.signal.aborted) throw error;
|
|
523
|
+
const said = error instanceof Error ? error.message : String(error);
|
|
524
|
+
// What the call wrote as it ran stays; the model reads it with the error.
|
|
525
|
+
const out = c.call?.part.output;
|
|
526
|
+
c.call?.fail(out ? undefined : said);
|
|
527
|
+
return out ? `${out}\n${said}` : `Failed: ${said}`;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
private wait(ms: number, signal: AbortSignal): Promise<void> {
|
|
532
|
+
return new Promise((done, fail) => {
|
|
533
|
+
const timer = setTimeout(done, ms);
|
|
534
|
+
signal.addEventListener('abort', () => {
|
|
535
|
+
clearTimeout(timer);
|
|
536
|
+
fail(signal.reason);
|
|
537
|
+
}, { once: true });
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
private parse(args: string): Record<string, unknown> {
|
|
542
|
+
try {
|
|
543
|
+
return JSON.parse(args || '{}') as Record<string, unknown>;
|
|
544
|
+
} catch {
|
|
545
|
+
return {};
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
```tsx
|
|
552
|
+
/**
|
|
553
|
+
* What a model's stream gives, once read. Most providers send these five things, each in its own words, and a model
|
|
554
|
+
* that searches names its sources too: your server, or the provider's SDK, turns its events into them, and the chat
|
|
555
|
+
* never sees the provider.
|
|
556
|
+
*/
|
|
557
|
+
export type ModelEvent =
|
|
558
|
+
| { type: 'thinking'; delta: string }
|
|
559
|
+
| { type: 'text'; delta: string }
|
|
560
|
+
| { type: 'tool-start'; id: string; name: string }
|
|
561
|
+
| { type: 'tool-args'; id: string; delta: string }
|
|
562
|
+
| { type: 'tool-end'; id: string }
|
|
563
|
+
| { type: 'source'; title: string; url?: string; snippet?: string };
|
|
564
|
+
|
|
565
|
+
/** A call the model made: its arguments as JSON text. */
|
|
566
|
+
export interface ModelCall {
|
|
567
|
+
id: string;
|
|
568
|
+
name: string;
|
|
569
|
+
args: string;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** The conversation as the model reads it: what the user said, what the model said and called, what each call returned. */
|
|
573
|
+
export type ModelMessage =
|
|
574
|
+
| { role: 'user'; text: string }
|
|
575
|
+
| { role: 'assistant'; text: string; calls: ModelCall[] }
|
|
576
|
+
| { role: 'tool'; id: string; result: string };
|
|
577
|
+
|
|
578
|
+
/** A request that failed; `retryable`: it may work if sent again (the provider was busy, the network dropped). */
|
|
579
|
+
export class ModelError extends Error {
|
|
580
|
+
constructor(message: string, readonly retryable = false) {
|
|
581
|
+
super(message);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** A model: one request, and its events as they arrive. `signal` cancels it; a failure throws a `ModelError`. */
|
|
586
|
+
export interface Model {
|
|
587
|
+
stream(history: readonly ModelMessage[], signal: AbortSignal): AsyncIterable<ModelEvent>;
|
|
588
|
+
}
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
## Waiting for the user
|
|
592
|
+
|
|
593
|
+
A reply can stop and ask. Each request returns a promise that resolves with what the user said, and the reply goes on from there. Every request takes the composer’s place until it is answered, so what waits for the user stands where they write; requests come one at a time, and the flow says which one waits, with “Answer” to go there. A tool call asks before it runs (`call.permission()`): it waits open, what goes in shown, and its choices take the composer’s place. A plan (`reply.plan()`) is drawn in its box, and the ways to go on take the composer’s place. Questions (`reply.questions()`) come one at a time, a tab for each when there are several, options with a line under them and a preview beside them, “Other” for the user’s own words, and “Decide for me” to leave it to the model. A form (`form()`, below) is the fields an MCP server asks for. The flow keeps every request and its answer, written into the message, so a turn drawn again shows them. When the reply stops first, a request resolves as `no` (questions as declined, a form as `cancel`) and the flow says nobody answered.
|
|
594
|
+
|
|
595
|
+
```tsx
|
|
596
|
+
const run = reply.tool({ name: 'Bash', title: 'Run the tests', input: 'npm test', look: 'terminal' });
|
|
597
|
+
const allowed = await run.permission({ always: 'Yes, and allow npm test from now on' });
|
|
598
|
+
if (allowed.choice === 'no') return model.toolResult(run.part.id, allowed.text ?? 'The user said no.');
|
|
599
|
+
|
|
600
|
+
// A tool of your model that asks the user: its arguments go in, the answer is the tool's result.
|
|
601
|
+
const answer = await reply.questions([
|
|
602
|
+
{ text: 'When should the call run?', header: 'Timing', options: [{ label: 'After the last call' }, { label: 'On the first call' }] },
|
|
603
|
+
{ text: 'Anything else?', multiple: true, options: [{ label: 'A cancel() method' }] },
|
|
604
|
+
]);
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
An MCP server asks the user through its client (elicitation): a message and the fields it wants, as a flat JSON schema. `form()` draws them as a form in the composer’s place, each field as a kit editor: a box for a text (email, address, date or time by its `format`), a number, round buttons or a drop-down for a choice of one (`enum`, `oneOf`), tags for several (`array`), a check box for a yes or no. The fields are checked as the schema says (`required`, lengths, ranges); “Submit” gives `accept` and what was written, a field left empty not with it, “Decline” `decline`, and “Cancel” or Escape `cancel`. The answer is the elicitation’s result, so it goes straight back to the server.
|
|
608
|
+
|
|
609
|
+
```tsx
|
|
610
|
+
// The MCP client's handler, while a tool call of that server runs.
|
|
611
|
+
client.setRequestHandler(ElicitRequestSchema, (request) =>
|
|
612
|
+
call.form(request.params.message, request.params.requestedSchema, { source: 'git' }),
|
|
613
|
+
);
|
|
614
|
+
```
|
|
615
|
+
|
|
616
|
+
## Sources
|
|
617
|
+
|
|
618
|
+
A model that searches, or quotes documents, names its sources: Gemini’s grounding, Claude’s citations, OpenAI’s annotations. Give them to `reply.sources()` and they show as numbered chips after what was written, each with its title and its site, a link to its address (opened where `linkTarget` says), and what the reply took from it as its hint. Sources given right after others join them, each once.
|
|
619
|
+
|
|
620
|
+
```tsx
|
|
621
|
+
for (const chunk of response.candidates[0].groundingMetadata?.groundingChunks ?? []) {
|
|
622
|
+
if (chunk.web) reply.sources([{ title: chunk.web.title, url: chunk.web.uri }]);
|
|
623
|
+
}
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
## Scrolling as a reply arrives
|
|
627
|
+
|
|
628
|
+
While the view is at the end, a growing reply keeps it there. If you scroll up, the view stays where you are and a button shows how many messages arrived. Click it to go back to the end. Sending a message always goes to the end.
|
|
629
|
+
|
|
630
|
+
While a reply is written, a line under the conversation says what the model does (`reply.status()`), for how long, and that Escape stops it. What the user sends meanwhile waits above the box, and goes when the replies end.
|
|
631
|
+
|
|
632
|
+
## Large conversations
|
|
633
|
+
|
|
634
|
+
Only the turns in and near the view are on the page. A turn is drawn from its messages when it comes near the view and disposed when it moves far away: only its messages and its measured height are kept, and it is drawn again when it comes back. A reply written into a turn far from the view writes its message, and the turn shows all of it when it comes back. Heights are estimated until measured, and the turn on screen stays in place while estimates become measurements. A turn that holds focus stays on the page. The chat opens at its end in one frame: until the turns at the end are placed it is laid out but not shown. A very long message is drawn a piece a frame (see Markdown), so the page never stops for it; when one is at the end, the chat shows once it is all there. With `codeLines`, a long code block shows its first lines and a button for the rest. The browser’s own find sees only the turns on the page; Ctrl+F in the chat searches them all (see Search).
|
|
635
|
+
|
|
636
|
+
## Loading older messages
|
|
637
|
+
|
|
638
|
+
With `hasOlder`, older messages are requested when the user goes up near the top — never when the chat opens — and also from the button above the first message for keyboard users. Add them with `prepend()`: the messages on screen stay in place, so the view does not jump. If loading fails, the button offers to try again, and the chat does not ask again on its own.
|
|
639
|
+
|
|
640
|
+
```tsx
|
|
641
|
+
<Chat hasOlder={() => this.history.more} onLoadOlder={(e) => {
|
|
642
|
+
e.promise = this.history.before(e.before).then((page) => chat.prepend(page.messages));
|
|
643
|
+
}} />
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
## Keyboard
|
|
647
|
+
|
|
648
|
+
| Key | Action |
|
|
649
|
+
| --- | --- |
|
|
650
|
+
| `Enter` | Sends the message; while a reply is written, queues it. Pressing Enter to finish an input method’s composition never sends. On a touch screen with no pointer that hovers (a phone, a tablet without a mouse), Enter starts a new line and the send button sends, as in a phone’s messages; the keyboard’s Enter key says so. |
|
|
651
|
+
| `Shift+Enter` | Starts a new line. |
|
|
652
|
+
| `Escape` | Closes the menu of commands or mentions; otherwise stops the reply while one is being written, from the box, the form of questions or the choices of a request. In the box under the choices, goes back to its choice. |
|
|
653
|
+
| `/ at the start of the box, @` | Lists the commands, or what the app suggests for the word after `@`. Arrow Up and Down move in the list, Enter or Tab puts the active one in the text. |
|
|
654
|
+
| `Enter, Escape while a question is edited` | Sends it in place of the old one; goes back to it as it was. |
|
|
655
|
+
| `Enter, Escape in a form` | Enter in a box of the form sends it once its fields pass their checks; Escape dismisses it (`cancel`). |
|
|
656
|
+
| `Arrow Up, Arrow Down in the box` | At the start of the box, brings back what was sent before; on a text brought back, goes back or forward, down to what was being written. |
|
|
657
|
+
| `Shift+Tab in the box` | Goes to the next mode, when there are modes. |
|
|
658
|
+
| `Ctrl+F (Cmd+F)` | Opens the search, while the focus is in the chat. |
|
|
659
|
+
| `Enter, Shift+Enter, Escape in the search` | Goes up to the older result, down to the newer one; closes the search, and the focus goes back to the box. |
|
|
660
|
+
| `Page Down, Page Up in a turn` | Moves the focus to the next turn, or the one before; above the first, older messages are asked for. |
|
|
661
|
+
| `Ctrl+End, Ctrl+Home in a turn` | Moves the focus to the box (or the questions waiting), or to the conversation itself. |
|
|
662
|
+
| `1–9` | Takes the choice or the option with that number, while the focus is among them. |
|
|
663
|
+
| `Arrow Up / Arrow Down` | Moves between the choices of a request, or the options of a question. |
|
|
664
|
+
| `Tab` | Moves through the button for older messages, the conversation (it scrolls with the arrow keys), the line of each thinking and tool call (Enter or Space opens it), the button back to the latest message, the box and the send button. |
|
|
665
|
+
|
|
666
|
+
## Accessibility
|
|
667
|
+
|
|
668
|
+
| Topic | Details |
|
|
669
|
+
| --- | --- |
|
|
670
|
+
| `Conversation` | A focusable region named “Conversation” in the kit language. Its turns are a feed (`role="feed"`, `aria-busy` while older messages load): each turn is an `article` named by its question, with `aria-posinset` and `aria-setsize` (`-1` while older messages may still come) whether it is on the page or not, and `aria-busy` while its reply is written. |
|
|
671
|
+
| `Announcements` | Screen readers hear when a reply ends, stops or fails, and when it waits for the user, from a polite live region that is not shown. |
|
|
672
|
+
| `Search` | The search is a `search` landmark; its box is named “Search in conversation”, and the count of results is a polite live region. The result shown is marked (`data-found`) on the block that holds it. |
|
|
673
|
+
| `Thinking and tool calls` | Their lines are buttons with `aria-expanded` when they open something. |
|
|
674
|
+
| `Errors` | The notice of a failed reply has the role `alert`, so it is announced. |
|
|
675
|
+
| `Requests` | What takes the composer’s place is a group named by its question. The choices of a tool call or a plan are buttons; the options of a question are `radio` or `checkbox` items with `aria-checked`; with several questions the tabs have the role `tab` and `aria-selected`. When a request appears while the focus is in the chat, the focus goes to it; after the answer, to the next request or back to the box. In the flow, “Answer” by a waiting request takes the focus there. |
|
|
676
|
+
| `Buttons` | The send button says “Send” or “Stop”, and the button back to the latest message says how many messages arrived. |
|
|
677
|
+
| `Actions` | A question’s actions (copy, edit, go back, fork) and a reply’s (copy, good, bad, try again, regenerate) are buttons named by what they do; good and bad are toggle buttons with `aria-pressed`. Where a pointer can hover they show on hover or when the focus is in the question or the turn, and they are always in the Tab order. The button of a summary has `aria-expanded`. |
|
|
678
|
+
| `Forms and sources` | A form is a group named “Information request”; its fields are the kit’s editors with their labels, help and messages, checked as its schema says. The sources of a reply are a group named “Sources”; a chip with an address is a link. |
|
|
679
|
+
| `Composer` | The attach, commands and send buttons show their icon alone and are named (`aria-label`, and a tooltip). The box has `aria-autocomplete="list"` when it suggests, and `aria-activedescendant` points to the active line of the open menu. The context meter has the role `meter` with its percent. Files and context items are chips: their remove buttons, Delete and Backspace take them off. |
|
|
680
|
+
|
|
681
|
+
## Localization
|
|
682
|
+
|
|
683
|
+
| String | Used for |
|
|
684
|
+
| --- | --- |
|
|
685
|
+
| `chatLabel, chatYou, chatAssistant, chatSystem` | The conversation’s name, and the default author names. |
|
|
686
|
+
| `chatLoadEarlier, chatLoadFailed` | The button for older messages, and what it says when loading failed. |
|
|
687
|
+
| `chatNewMessages, chatJumpToLatest` | The button back to the latest message. |
|
|
688
|
+
| `chatSend, chatStop, chatPlaceholder` | The message box and its button. |
|
|
689
|
+
| `chatStopped, chatFailed` | The notices of a stopped or failed reply. |
|
|
690
|
+
| `chatThinking, chatThought, chatThoughtBrief, chatSeconds, chatKilobytes` | The thinking line, and what a tool call waits on. |
|
|
691
|
+
| `chatWorking, chatStopHint` | The line under the conversation while a reply is written. |
|
|
692
|
+
| `chatShowMore, chatShowLess, chatTasks` | The button of a long question, and the title of the to-do list. |
|
|
693
|
+
| `chatQueuePlaceholder, chatRemove` | The box while a reply is written, and the button that removes a queued message. |
|
|
694
|
+
| `chatAllowTool, chatAllow, chatDeny, chatDenyHint, chatAllowed, chatAllowedAlways, chatDenied, chatUnanswered` | A permission: its question, its choices, what the user said, and a request nobody answered. |
|
|
695
|
+
| `chatPlan, chatPlanReady, chatPlanApprove, chatPlanKeep, chatPlanHint, chatPlanApproved, chatPlanKept` | A plan: its title, its question, its choices and what the user said. |
|
|
696
|
+
| `chatQuestions, chatQuestionCount, chatWaitingAnswer, chatAnswered, chatDeclined, chatAnswer` | Requests in the flow: how many questions, where they stand, and the button to the composer’s place. |
|
|
697
|
+
| `chatOther, chatOtherHint, chatDecide, chatPrevious, chatNext, chatSubmitAnswers, chatRequired` | The form of questions. |
|
|
698
|
+
| `chatWaitingYou` | The line under the conversation while a reply waits for the user. |
|
|
699
|
+
| `chatAttach, chatAttachments, chatCommands` | The attach and commands buttons, and how many files a queued message carries. |
|
|
700
|
+
| `chatModeHint, chatModel, chatThinkingToggle, chatUsage` | The mode and model buttons, the thinking check box, and the context meter’s name. |
|
|
701
|
+
| `chatCopy, chatCopied, chatEdit, chatRewind, chatFork, chatCancel, chatRetry` | The actions of questions and replies, and editing a question. |
|
|
702
|
+
| `chatRetryingIn, chatRetried, chatCompacted` | A retry’s countdown, and the line where the conversation was summed up. |
|
|
703
|
+
| `chatReplied` | What screen readers hear when a reply ends. |
|
|
704
|
+
| `chatSearch, chatSearchCount, chatSearchNone, chatSearchPrevious, chatSearchNext` | The search: its box, “3 of 12”, no results, and its buttons. Its close button says the kit’s `close`. |
|
|
705
|
+
| `chatRegenerate, chatGood, chatBad` | The actions of a reply that ended: another reply, good and bad. |
|
|
706
|
+
| `chatSources` | The name of a reply’s sources. |
|
|
707
|
+
| `chatForm, chatFormFrom, chatFormSubmit, chatFormDecline, chatFormCancel, chatFormDeclined, chatFormCancelled` | A form: its name, who asks (“git asks”), its buttons and what the user did. A yes or no in the flow says the kit’s `yes` and `no`. |
|
|
708
|
+
|
|
709
|
+
## Styling
|
|
710
|
+
|
|
711
|
+
| Hook | Description |
|
|
712
|
+
| --- | --- |
|
|
713
|
+
| `.mx-chat` | The component (`data-searching` while the search is open). An on-screen keyboard over its end gives it room there through `--_keyboard`, and it keeps clear of a phone’s edges (`env(safe-area-inset-*)`). |
|
|
714
|
+
| `.mx-chat__search, __search-box, __search-count, __search-previous, __search-next, __search-close` | The search at the top. |
|
|
715
|
+
| `[data-found], .mx-chat__live` | The block that holds the result shown; what is said to screen readers. |
|
|
716
|
+
| `.mx-chat__view, .mx-chat__scroller` | The part that scrolls (a `ScrollView`). |
|
|
717
|
+
| `.mx-chat__older, .mx-chat__older-button` | The button for older messages. |
|
|
718
|
+
| `.mx-chat__list, .mx-chat__turn` | The turns: only those in and near the view are on the page. |
|
|
719
|
+
| `.mx-chat__question-head, __question-actions, __action, __edit, __edit-box, __edit-actions, __edit-cancel, __edit-send, __turn-actions` | The actions of a question (over its box, with its time) and of a reply (at the end of its turn, `data-failed` when it can be tried again), and a question being edited (`data-editing` on the question). |
|
|
720
|
+
| `.mx-chat__compaction, __compaction-line, __compaction-head, __compaction-text` | Where the conversation was summed up (`data-expanded` on its entry). |
|
|
721
|
+
| `.mx-chat__question, __time, __bubble, __more` | The user’s question (`data-long`, `data-expanded`), its time, its box and the button for the rest. |
|
|
722
|
+
| `.mx-chat__flow, .mx-chat__entry, __dot, __entry-main` | What the model did: one entry per part, with `data-kind` (`text`, `thinking`, `tool`, `todo`, `notice`, `plan`, `questions`, `form`, `sources`, `attachment`, `compaction`), and on a tool call `data-status`, `data-density`, `data-look` and `data-full`. |
|
|
723
|
+
| `.mx-chat__tool-head, __tool-name, __tool-title, __tool-wait, __tool-box, __tool-side, __tool-label, __tool-code, __diff-line` | A tool call: its line, what went in and came out, a diff’s lines (`data-line`). |
|
|
724
|
+
| `.mx-chat__thinking-head, __thinking-label, __thinking-note, __thinking-body, __thinking-item` | A thinking (`data-state`): its line, its newest note, what it wrote and its notes. |
|
|
725
|
+
| `.mx-chat__todo-title, __todo-list, __todo-item, __todo-glyph, __notice` | The to-do list (`data-status` on each task) and a notice (`data-tone`). |
|
|
726
|
+
| `.mx-chat__request, __answer` | What a tool call or a plan asks, in the flow: while it waits, the line that says so (`__asked-head`, `__asked-state`, `__asked-button`); then the answer (`data-answer`: `yes`, `always`, `no`, `none`). |
|
|
727
|
+
| `.mx-chat__plan, __plan-title, __plan-text` | A plan’s box. |
|
|
728
|
+
| `.mx-chat__asked-head, __asked-count, __asked-state, __asked-button, __asked-list, __asked-pair, __asked-question, __asked-answer` | Questions in the flow; the head and the button are a waiting request’s in every kind. |
|
|
729
|
+
| `.mx-chat__ask, __ask-header, __ask-tabs, __ask-tab, __ask-count, __ask-body, __ask-text, __ask-choice, __ask-options, __ask-option, __ask-key, __ask-label, __ask-description, __ask-check, __ask-preview, __ask-other, __ask-free, __ask-missing, __ask-actions` | The form of questions in the composer’s place; tabs carry `data-selected` and `data-answered`, options `data-checked`, the options area `data-preview`. The choices of a tool call or a plan are the same parts in `.mx-chat__ask[data-kind="choices"]`: its question (`__ask-text`), an option for each choice, and the box under them (`__ask-other`), its choice marked `data-checked`. |
|
|
730
|
+
| `.mx-chat__ask[data-kind="form"], __form-source, __form-fields, __form-cancel, __form-message` | A form in the composer’s place: who asks, its message (`__ask-text`), its fields (a `FormLayout`) and its buttons (`__form-cancel`, `__ask-decline`, `__ask-submit`); in the flow, an entry of the kind `form` with its message and the fields filled in (`__asked-*`). |
|
|
731
|
+
| `.mx-chat__sources, __source, __source-index, __source-title, __source-site` | The sources of a reply: an entry of the kind `sources`, a chip each. |
|
|
732
|
+
| `.mx-chat__status, __status-glyph, __status-text, __status-detail` | The line under the conversation while a reply is written (`data-waiting` while it waits for the user). |
|
|
733
|
+
| `.mx-chat__queue, __queued, __queued-text, __queued-remove` | The messages waiting above the box. |
|
|
734
|
+
| `.mx-chat__jump` | The button back to the latest message. |
|
|
735
|
+
| `.mx-chat-composer, __field, __files, __file, __box, __tools` | The composer (`data-busy` while a reply is written): the field (`data-dragover` while files are dragged over it) with the files above the box and the tools under it. |
|
|
736
|
+
| `.mx-chat-composer__attach, __commands, __context, __context-item, __mode, __model, __space, __usage, __usage-ring, __usage-text, __button` | The tools: attach, commands, the context chips, the mode and the model, the context meter (`data-high` when nearly full) and the send button. |
|
|
737
|
+
| `.mx-chat-composer__popup, __menu, __menu-label, __menu-description` | The menu of commands or mentions. |
|
|
738
|
+
| `.mx-chat__files, .mx-chat__file` | The files and context of a question, as chips under its box. |
|
|
739
|
+
|
|
740
|
+
### Component tokens
|
|
741
|
+
|
|
742
|
+
| Token | Description |
|
|
743
|
+
| --- | --- |
|
|
744
|
+
| `chat.turns-gap, chat.jump-offset, chat.clamp-lines, chat.status-spin` | The space between turns, where the button back to the latest message sits, how many lines a long question and a summary show, and one turn of the status glyph. |
|
|
745
|
+
| `chat.list, turn, question, time, bubble, flow, entry, dot, main` | Units of the turns and their entries. The dot has a state per kind: `text`, `thinking`, `running`, `waiting`, `done`, `error`, `todo`, `notice`, `alert`. |
|
|
746
|
+
| `chat.tool-head, tool-name, tool-title, tool-wait, tool-box, tool-side, tool-label, tool-code, diff-line` | Units of a tool call; `diff-line` has `added` and `removed`. |
|
|
747
|
+
| `chat.thinking-head, thinking-note, thinking-body, thinking-item, todo-title, todo-list, todo-item, todo-glyph, notice` | Units of a thinking, the to-do list (`active`, `done`) and a notice (`error`). |
|
|
748
|
+
| `chat.status, status-glyph, status-detail, queue, queued, queued-text, older` | Units of the line under the conversation, the queue and the place of the button for older messages. |
|
|
749
|
+
| `chat.request, answer, plan, plan-title` | Units of a request in the flow; `answer` has `yes` and `no`. |
|
|
750
|
+
| `chat.asked-head, asked-count, asked-state, asked-list, asked-pair, asked-question, asked-answer` | Units of questions in the flow; the head and the state are a waiting request’s in every kind. |
|
|
751
|
+
| `chat.ask, ask-header, ask-tabs, ask-tab, ask-tab-check, ask-count, ask-body, ask-text, ask-choice, ask-options, ask-option, ask-key, ask-label, ask-description, ask-check, ask-preview, ask-missing, ask-actions` | Units of what takes the composer’s place: the form of questions, and the same units for the choices of a request; `ask-tab` has `selected`, `ask-option` has `checked`, both `hover` and `focus`. |
|
|
752
|
+
| `chat.question-head, question-actions, turn-actions, edit, edit-actions, compaction, compaction-line, compaction-head, compaction-text` | Units of the actions, of editing a question and of a summary; `compaction-head` has `hover` and `focus`. |
|
|
753
|
+
| `chat.question-action, chat.turn-action, chat.edit-cancel, chat.edit-send (places)` | The variant, tone and size of the action buttons and of the edit buttons. |
|
|
754
|
+
| `chat.search, search-count, found` | Units of the search, and of the block that holds the result shown. `turn` has `focus`, for Page Down and Page Up. |
|
|
755
|
+
| `chat.search-previous, chat.search-next, chat.search-close (places)` | The variant, tone and size of the search’s buttons. |
|
|
756
|
+
| `chat.sources, source, source-index, source-title, source-site` | Units of a reply’s sources; `source` has `hover` and `focus`. |
|
|
757
|
+
| `chat.form-source, form-message, chat.form-cancel (place)` | Who asks for a form, what a form in the flow asked for, and the button that dismisses it. The rest of a form is the questions’ `ask` units and places, and its fields a `FormLayout`’s. |
|
|
758
|
+
| `chat.older-button, chat.jump, chat.more, chat.queued-remove, chat.asked-button, chat.ask-previous, chat.ask-next, chat.ask-decline, chat.ask-submit (places)` | The variant, tone and size of those buttons. |
|
|
759
|
+
| `chat-composer.usage-fill, usage-high, usage-track` | The context meter’s ring: the part used, the part used once nearly full, and the rest. |
|
|
760
|
+
| `chat-composer.frame, field, files, tools, context, usage, usage-ring, menu-label, menu-description` | Units of the composer; `field` has `focus` and `dragover`. |
|
|
761
|
+
| `chat-composer.box, attach, commands, mode, model, send (places)` | The box with no frame of its own, and the variant, tone and size of the buttons. |
|
|
762
|
+
|
|
763
|
+
## API reference
|
|
764
|
+
|
|
765
|
+
### Props
|
|
766
|
+
|
|
767
|
+
| Prop | Type | Default | Description |
|
|
768
|
+
| --- | --- | --- | --- |
|
|
769
|
+
| `messages` | `Bind<ChatMessage[]>` | `[]` | The messages, oldest first. The chat keeps this array and its objects: `add`, `prepend` and `remove` change it, and replies are written into their messages. A different array is a new conversation, which opens at its end. |
|
|
770
|
+
| `hasOlder` | `Bind<boolean>` | `false` | There are older messages. They load near the top or from the button above the first message. |
|
|
771
|
+
| `placeholder` | `Bind<string>` | The kit string “Message” | The hint in the empty message box. |
|
|
772
|
+
| `highlighter` | `Bind<CodeHighlighter \| null>` | None | Colours the code blocks of messages drawn after it is set. |
|
|
773
|
+
| `mathRenderer` | `Bind<MathRenderer \| null>` | None | Draws the math of messages drawn after it is set (see Markdown). Without it, `$` is text. |
|
|
774
|
+
| `diagramRenderer` | `Bind<DiagramRenderer \| null>` | None | Draws the diagrams of messages drawn after it is set, such as a fenced `mermaid` block (see Markdown). |
|
|
775
|
+
| `linkTarget` | `Bind<string>` | `'_blank'` | Where links in messages, and the links of sources, open. |
|
|
776
|
+
| `codeLines` | `Bind<number>` | `0` | A code block in a reply longer than this many lines shows that many, and a button under it shows them all (see Markdown). For messages drawn after it is set; `0` shows every line. |
|
|
777
|
+
| `label` | `Bind<string>` | The kit string “Conversation” | The accessible name of the conversation. |
|
|
778
|
+
| `composer` | `ChatComposerProps` | | Props of the composer, given to it as they are (see “The composer”): `attachments`, `accept`, `contextItems`, `commands`, `onMention`, `modes`, `mode`, `models`, `model`, `thinking`, `usage`, `history` and their events. `onSend` and `onStop` are the chat’s. |
|
|
779
|
+
| `onSend` | `(e) => void` | | A message of the user is sent (`e.text`, `e.message`). It is added unless `e.cancel` is set. Start the reply here. What the user sends while a reply is written waits above the box and is sent when the replies end. |
|
|
780
|
+
| `onStop` | `(e) => void` | | The user stopped the reply. Its `signal` is already aborted. |
|
|
781
|
+
| `onRewind` | `(e: ChatRewindEventArgs) => void` | | The conversation goes back to before a question (its edit or go-back button, `rewind()`, `resend()`): `e.removed` are it and what came after. Set `e.cancel` to keep them; forget them in your model’s history. |
|
|
782
|
+
| `onFork` | `(e: ChatForkEventArgs) => void` | | Given, a question has a button that starts another conversation from it: open it with `e.messages`, those before `e.message`. |
|
|
783
|
+
| `onRetry` | `(e: ChatRetryEventArgs) => void` | | Given, the latest reply, when it failed or stopped, has a button to try again: it is gone; write the new one with `reply()` (`e.question` is the message it answered). |
|
|
784
|
+
| `onRegenerate` | `(e: ChatRetryEventArgs) => void` | | Given, the latest reply has a button that asks for another in its place once it ended (a failed one too, unless `onRetry` is given): it is gone; write the new one with `reply()`. |
|
|
785
|
+
| `onFeedback` | `(e: ChatFeedbackEventArgs) => void` | | Given, a reply that ended has buttons to say it was good or bad. Pressed again, a button takes it back (`e.feedback` is `null`). It is written in `e.message.feedback` first. |
|
|
786
|
+
| `onLoadOlder` | `(e: ChatLoadOlderEventArgs) => void` | | Older messages are needed. Call `chat.prepend()`, then `e.release()`, or set `e.promise`. Use `e.release(true)` when there are no more. If the promise rejects or you call `e.fail()`, a button appears to try again. |
|
|
787
|
+
|
|
788
|
+
### Properties
|
|
789
|
+
|
|
790
|
+
| Property | Access | Description |
|
|
791
|
+
| --- | --- | --- |
|
|
792
|
+
| `every prop above` | read/write | Accepts a value or a function, the same as the prop. |
|
|
793
|
+
| `isBusy` | read-only | `true` while a reply is being written. |
|
|
794
|
+
| `queuedTexts` | read-only | The messages waiting above the box, in order. |
|
|
795
|
+
| `isWaiting` | read-only | `true` while a reply waits for the user: a permission, a plan, questions or a form. |
|
|
796
|
+
| `composer` | read-only | The `ChatComposer`, the box where the user writes. |
|
|
797
|
+
|
|
798
|
+
### Methods
|
|
799
|
+
|
|
800
|
+
| Method | Description |
|
|
801
|
+
| --- | --- |
|
|
802
|
+
| `reply(init?)` | Starts a reply from the model at the end and returns a `ChatReply`. |
|
|
803
|
+
| `add(message)` | Adds a message at the end. |
|
|
804
|
+
| `prepend(messages)` | Adds older messages before the first one. The messages on screen stay where they are. |
|
|
805
|
+
| `remove(message), clear()` | Removes one message, or all of them. `clear` also stops open replies. |
|
|
806
|
+
| `rewind(message)` | Goes back to before a question: it and everything after it go (`onRewind` can keep them), open replies among them stop, the queue empties, and its text goes back in the box. |
|
|
807
|
+
| `resend(message, text)` | Sends `text` in place of a question, with its files: back to before it, then sent. |
|
|
808
|
+
| `send(text)` | Sends `text` as the user, the same as the message box. Returns whether it was added. |
|
|
809
|
+
| `stop()` | Stops every open reply, the same as the stop button. |
|
|
810
|
+
| `loadOlder()` | Requests older messages now, the same as the button. |
|
|
811
|
+
| `scrollToEnd(smooth?)` | Goes to the end. From there, new messages keep the view at the end. |
|
|
812
|
+
| `openSearch(query?), closeSearch()` | Opens the search at the top (with `query` when given) and focuses it; closes it, and its mark goes. |
|
|
813
|
+
| `find(query)` | Searches every message for `query`, the turns off the page too, and goes to the result nearest the end. Returns how many it found. |
|
|
814
|
+
| `findNext(), findPrevious()` | Goes to the next result, down towards the end, or the one before, up; around the ends. |
|
|
815
|
+
| `focus()` | Focuses where the user writes: the questions or the form waiting, or the message box. |
|
|
816
|
+
|
|
817
|
+
`ChatComposer` (the message box) can also be used on its own, with the props of “The composer” and `placeholder`, `busy`, `maxRows`, `onSend` and `onStop`. From code: `send()`, `stop()`, `value`, `files`, `attach(files)`, `detach(file)`, `pick()`, `nextMode()`, and every prop as a property. While `busy`, Enter still sends and the button stops.
|
|
818
|
+
|
|
819
|
+
## Feedback
|
|
820
|
+
|
|
821
|
+
Loading issues…
|
|
822
|
+
|
|
823
|
+
## Example files
|
|
824
|
+
|
|
825
|
+
`Conversation.tsx`
|
|
826
|
+
|
|
827
|
+
```tsx
|
|
828
|
+
import { Chat } from '@motifx-community/ui';
|
|
829
|
+
import { Agent } from './connect';
|
|
830
|
+
import { tools } from './demo-tools';
|
|
831
|
+
import { FakeModel, history, olderPage } from './fake-model';
|
|
832
|
+
|
|
833
|
+
const MODES = [
|
|
834
|
+
{ value: 'ask', label: 'Ask before edits' },
|
|
835
|
+
{ value: 'auto', label: 'Edit automatically' },
|
|
836
|
+
{ value: 'plan', label: 'Plan mode', description: 'Reads, then plans' }
|
|
837
|
+
];
|
|
838
|
+
|
|
839
|
+
const COMMANDS = [
|
|
840
|
+
{ name: 'review', description: 'Review the changes' },
|
|
841
|
+
{ name: 'compact', description: 'Sum up the conversation so far' },
|
|
842
|
+
{ name: 'clear', description: 'Start a new conversation' }
|
|
843
|
+
];
|
|
844
|
+
|
|
845
|
+
const FILES = ['src/debounce.ts', 'src/debounce.test.ts', 'src/search.ts', 'src/resize.ts', 'README.md'];
|
|
846
|
+
|
|
847
|
+
const MODELS = [
|
|
848
|
+
{ value: 'large', label: 'Large', description: 'The most capable' },
|
|
849
|
+
{ value: 'small', label: 'Small', description: 'The fastest' }
|
|
850
|
+
];
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* A long conversation that opens at its end, and an agent connected the way a real one is (`connect.ts`), to a model
|
|
854
|
+
* that stands in for yours: send anything and it asks two questions, shows a plan, keeps a to-do list, reads a file,
|
|
855
|
+
* asks before running the tests (one fails), edits the file, sends a sub-agent, commits through a git server that
|
|
856
|
+
* asks for the message in a form, and answers with its sources. Send again while it works: the message waits above
|
|
857
|
+
* the box. Escape stops it. Scroll up for older turns; a long code block shows its first lines. In the box: files to
|
|
858
|
+
* attach (the button, paste or drop), the open file as context, `/` for commands, `@` for files; under it the mode
|
|
859
|
+
* (Shift+Tab; "Edit automatically" runs the tests without asking), the model and its thinking, and how full the
|
|
860
|
+
* context is. Hover a question to copy it, edit and send it again, go back to it, or start a new conversation from
|
|
861
|
+
* it; hover a reply to copy it, say whether it was good, or ask for another. `/compact` sums the conversation up. The
|
|
862
|
+
* model's first request is refused as busy once: the agent counts down and tries again.
|
|
863
|
+
*/
|
|
864
|
+
export default function Conversation() {
|
|
865
|
+
let chat!: Chat;
|
|
866
|
+
let pages = 0;
|
|
867
|
+
let agent = new Agent(new FakeModel(), tools);
|
|
868
|
+
// A real app sends what the user said of a reply to its server, with the message's id.
|
|
869
|
+
const judged = new Map<string | number, string | null>();
|
|
870
|
+
return (
|
|
871
|
+
<Chat
|
|
872
|
+
ref={(c: Chat) => (chat = c)}
|
|
873
|
+
style="block-size: 36rem"
|
|
874
|
+
messages={history(200)}
|
|
875
|
+
hasOlder
|
|
876
|
+
codeLines={12}
|
|
877
|
+
composer={{
|
|
878
|
+
attachments: true,
|
|
879
|
+
contextItems: [{ label: 'debounce.ts', data: 'src/debounce.ts' }],
|
|
880
|
+
commands: COMMANDS,
|
|
881
|
+
onMention: (e) => (e.items = FILES.filter((f) => f.includes(e.query)).map((f) => ({ label: f.split('/').pop()!, value: f, description: f }))),
|
|
882
|
+
modes: MODES,
|
|
883
|
+
onModeChanged: (e) => (agent.mode = e.value),
|
|
884
|
+
models: MODELS,
|
|
885
|
+
thinking: true,
|
|
886
|
+
usage: 0.18
|
|
887
|
+
}}
|
|
888
|
+
onRewind={(e) => agent.rewind(e.removed.filter((m) => m.role === 'user').length)}
|
|
889
|
+
onFork={(e) => {
|
|
890
|
+
// Another conversation from that question: its messages before it, the question back in the box.
|
|
891
|
+
const said = e.message.parts.map((p) => (p.kind === 'text' ? p.text : '')).join('');
|
|
892
|
+
agent = new Agent(new FakeModel(), tools);
|
|
893
|
+
agent.mode = chat.composer.mode;
|
|
894
|
+
chat.messages = e.messages.slice();
|
|
895
|
+
chat.composer.value = said;
|
|
896
|
+
}}
|
|
897
|
+
onRetry={() => void agent.again(chat.reply())}
|
|
898
|
+
onRegenerate={() => void agent.again(chat.reply())}
|
|
899
|
+
onFeedback={(e) => judged.set(e.message.id, e.feedback)}
|
|
900
|
+
onSend={(e) => {
|
|
901
|
+
if (e.text.trim() === '/compact') {
|
|
902
|
+
const reply = chat.reply();
|
|
903
|
+
reply.compaction('So far: the debounce helper started a new timer on every call; it now clears the old one first. Its two callers need no change.');
|
|
904
|
+
reply.done();
|
|
905
|
+
// A real app asks its model for the summary, and the model's history starts again from it.
|
|
906
|
+
agent.history.length = 0;
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
// A real app sends the files and the context to its model too; this one only names them.
|
|
910
|
+
const named = [...e.contextItems.map((c) => String(c.data)), ...e.attachments.map((f) => f.name)];
|
|
911
|
+
const said = named.length ? `${e.text} (${named.join(', ')})` : e.text;
|
|
912
|
+
void agent.answer(chat.reply(), said).then(() => (chat.composer.usage = Math.min(0.97, chat.composer.usage! + 0.21)));
|
|
913
|
+
}}
|
|
914
|
+
onLoadOlder={(e) => {
|
|
915
|
+
e.promise = new Promise((done) => setTimeout(done, 600)).then(() => {
|
|
916
|
+
chat.prepend(olderPage());
|
|
917
|
+
if (++pages === 3) e.release(true);
|
|
918
|
+
});
|
|
919
|
+
}}
|
|
920
|
+
/>
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
`connect.ts`
|
|
926
|
+
|
|
927
|
+
```tsx
|
|
928
|
+
import type { ChatReply, ChatToolCall, ChatToolDensity, ChatToolLook } from '@motifx-community/ui';
|
|
929
|
+
import { ModelError, type Model, type ModelCall, type ModelMessage } from './model';
|
|
930
|
+
|
|
931
|
+
/** How many times a request that failed before anything came is sent, in all; the waits double from a second. */
|
|
932
|
+
const TRIES = 4;
|
|
933
|
+
|
|
934
|
+
/** A tool of the app: how its call reads in the chat, whether it asks first, and what it does. */
|
|
935
|
+
export interface AppTool {
|
|
936
|
+
/** The name a person reads; by default the tool's name. */
|
|
937
|
+
label?: string;
|
|
938
|
+
look?: ChatToolLook;
|
|
939
|
+
density?: ChatToolDensity;
|
|
940
|
+
/** What a call does, in a line, from its arguments: a file, a command. */
|
|
941
|
+
title?: (args: Record<string, unknown>) => string;
|
|
942
|
+
/** The call has no row of its own: the tool draws into the reply itself (questions, a plan, the to-do list). */
|
|
943
|
+
quiet?: boolean;
|
|
944
|
+
/** Asks the user before it runs. A text is the choice that stops asking ("Yes, and allow npm test"). */
|
|
945
|
+
ask?: boolean | string;
|
|
946
|
+
/** Runs the call; what it returns goes back to the model. `call` is its row: write output there as it comes. */
|
|
947
|
+
run(args: Record<string, unknown>, reply: ChatReply, call: ChatToolCall | null): Promise<string>;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/** A call while it is read and run. */
|
|
951
|
+
interface Running extends ModelCall {
|
|
952
|
+
call: ChatToolCall | null;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* Connects a chat to a model: the conversation the model reads, the tools it may call, and one turn. The model's
|
|
957
|
+
* stream goes into the reply as it comes; then the calls it made run (asking first where a tool says so), what they
|
|
958
|
+
* returned goes back, and the model is asked again, until it answers without calling anything. A request the provider
|
|
959
|
+
* refused for a while (`ModelError.retryable`) is tried again after a wait the chat counts down, when nothing of it had
|
|
960
|
+
* come yet.
|
|
961
|
+
*
|
|
962
|
+
* @example
|
|
963
|
+
* const agent = new Agent(model, tools);
|
|
964
|
+
* <Chat ref={c => (chat = c)} onSend={e => void agent.answer(chat.reply(), e.text)} />
|
|
965
|
+
*/
|
|
966
|
+
export class Agent {
|
|
967
|
+
/** What the model reads. Load it with the conversation's messages when a saved one opens. */
|
|
968
|
+
readonly history: ModelMessage[] = [];
|
|
969
|
+
/** `auto`: the tools that ask first run without asking (the composer's mode, see `onModeChanged`). */
|
|
970
|
+
mode = 'ask';
|
|
971
|
+
/** Tools the user allowed for the rest of the conversation. */
|
|
972
|
+
private readonly allowed = new Set<string>();
|
|
973
|
+
|
|
974
|
+
constructor(private readonly model: Model, private readonly tools: Record<string, AppTool>) {}
|
|
975
|
+
|
|
976
|
+
/** Answers what the user sent, into `reply`. */
|
|
977
|
+
async answer(reply: ChatReply, text: string): Promise<void> {
|
|
978
|
+
this.history.push({ role: 'user', text });
|
|
979
|
+
await this.run(reply);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/** Answers the user's last message again, into `reply` (the chat's `onRetry`, `onRegenerate`): what the model said to it goes. */
|
|
983
|
+
async again(reply: ChatReply): Promise<void> {
|
|
984
|
+
this.history.splice(this.lastQuestion(0) + 1);
|
|
985
|
+
await this.run(reply);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/** Forgets the user's last `questions` messages and what came after them, as the chat went back (`onRewind`). */
|
|
989
|
+
rewind(questions: number): void {
|
|
990
|
+
if (questions > 0) this.history.splice(Math.max(0, this.lastQuestion(questions - 1)));
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/** Where the user's message is, `skip` of them back from the last; `-1` when there is none. */
|
|
994
|
+
private lastQuestion(skip: number): number {
|
|
995
|
+
for (let i = this.history.length - 1; i >= 0; i--) if (this.history[i].role === 'user' && skip-- === 0) return i;
|
|
996
|
+
return -1;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
private async run(reply: ChatReply): Promise<void> {
|
|
1000
|
+
try {
|
|
1001
|
+
for (let calls = await this.read(reply); calls.length; calls = await this.read(reply)) {
|
|
1002
|
+
for (const c of calls) this.history.push({ role: 'tool', id: c.id, result: await this.use(reply, c) });
|
|
1003
|
+
}
|
|
1004
|
+
reply.done();
|
|
1005
|
+
} catch (error) {
|
|
1006
|
+
// Stopped by the user: the chat already says so.
|
|
1007
|
+
if (!reply.signal.aborted) reply.fail(error as Error);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/** One request, tried again while it fails before anything came and the provider says it may work later. */
|
|
1012
|
+
private async read(reply: ChatReply): Promise<Running[]> {
|
|
1013
|
+
for (let attempt = 1; ; attempt++) {
|
|
1014
|
+
const heard = { any: false };
|
|
1015
|
+
try {
|
|
1016
|
+
return await this.readOnce(reply, heard);
|
|
1017
|
+
} catch (error) {
|
|
1018
|
+
if (reply.signal.aborted || heard.any || attempt >= TRIES || !(error instanceof ModelError) || !error.retryable) throw error;
|
|
1019
|
+
const seconds = 2 ** (attempt - 1);
|
|
1020
|
+
reply.retrying(seconds, { attempt: attempt + 1, attempts: TRIES, reason: error.message });
|
|
1021
|
+
await this.wait(seconds * 1000, reply.signal);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/** One request: its events into the reply, and the calls it made. */
|
|
1027
|
+
private async readOnce(reply: ChatReply, heard: { any: boolean }): Promise<Running[]> {
|
|
1028
|
+
const calls = new Map<string, Running>();
|
|
1029
|
+
let said = '';
|
|
1030
|
+
reply.status('Thinking');
|
|
1031
|
+
for await (const e of this.model.stream(this.history, reply.signal)) {
|
|
1032
|
+
heard.any = true;
|
|
1033
|
+
switch (e.type) {
|
|
1034
|
+
case 'thinking':
|
|
1035
|
+
reply.thinking(e.delta);
|
|
1036
|
+
break;
|
|
1037
|
+
case 'text':
|
|
1038
|
+
said += e.delta;
|
|
1039
|
+
reply.text(e.delta);
|
|
1040
|
+
break;
|
|
1041
|
+
case 'tool-start': {
|
|
1042
|
+
const tool = this.tools[e.name];
|
|
1043
|
+
const call = tool?.quiet ? null : reply.tool({ id: e.id, name: e.name, label: tool?.label, look: tool?.look, density: tool?.density, status: 'assembling' });
|
|
1044
|
+
calls.set(e.id, { id: e.id, name: e.name, args: '', call });
|
|
1045
|
+
reply.status(tool?.label ?? e.name);
|
|
1046
|
+
break;
|
|
1047
|
+
}
|
|
1048
|
+
case 'tool-args': {
|
|
1049
|
+
const c = calls.get(e.id)!;
|
|
1050
|
+
c.args += e.delta;
|
|
1051
|
+
c.call?.progress(undefined, c.args.length);
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
case 'tool-end': {
|
|
1055
|
+
const c = calls.get(e.id)!;
|
|
1056
|
+
c.call?.start(c.args, this.tools[c.name]?.title?.(this.parse(c.args)));
|
|
1057
|
+
break;
|
|
1058
|
+
}
|
|
1059
|
+
case 'source':
|
|
1060
|
+
reply.sources([{ title: e.title, url: e.url, snippet: e.snippet }]);
|
|
1061
|
+
break;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
const made = [...calls.values()];
|
|
1065
|
+
this.history.push({ role: 'assistant', text: said, calls: made.map(({ id, name, args }) => ({ id, name, args })) });
|
|
1066
|
+
return made;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
/** Runs a call, asking first when its tool says so and the user has not allowed it for good. */
|
|
1070
|
+
private async use(reply: ChatReply, c: Running): Promise<string> {
|
|
1071
|
+
const tool = this.tools[c.name];
|
|
1072
|
+
if (!tool) {
|
|
1073
|
+
c.call?.fail(`No tool named ${c.name}`);
|
|
1074
|
+
return `There is no tool named ${c.name}.`;
|
|
1075
|
+
}
|
|
1076
|
+
if (tool.ask && c.call && this.mode !== 'auto' && !this.allowed.has(c.name)) {
|
|
1077
|
+
const said = await c.call.permission({ always: typeof tool.ask === 'string' ? tool.ask : undefined });
|
|
1078
|
+
if (said.choice === 'always') this.allowed.add(c.name);
|
|
1079
|
+
if (said.choice === 'no') return said.text ? `The user did not allow it, and said: ${said.text}` : 'The user did not allow it.';
|
|
1080
|
+
}
|
|
1081
|
+
reply.status(tool.label ?? c.name);
|
|
1082
|
+
try {
|
|
1083
|
+
const out = await tool.run(this.parse(c.args), reply, c.call);
|
|
1084
|
+
c.call?.done(out);
|
|
1085
|
+
return out;
|
|
1086
|
+
} catch (error) {
|
|
1087
|
+
if (reply.signal.aborted) throw error;
|
|
1088
|
+
const said = error instanceof Error ? error.message : String(error);
|
|
1089
|
+
// What the call wrote as it ran stays; the model reads it with the error.
|
|
1090
|
+
const out = c.call?.part.output;
|
|
1091
|
+
c.call?.fail(out ? undefined : said);
|
|
1092
|
+
return out ? `${out}\n${said}` : `Failed: ${said}`;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
private wait(ms: number, signal: AbortSignal): Promise<void> {
|
|
1097
|
+
return new Promise((done, fail) => {
|
|
1098
|
+
const timer = setTimeout(done, ms);
|
|
1099
|
+
signal.addEventListener('abort', () => {
|
|
1100
|
+
clearTimeout(timer);
|
|
1101
|
+
fail(signal.reason);
|
|
1102
|
+
}, { once: true });
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
private parse(args: string): Record<string, unknown> {
|
|
1107
|
+
try {
|
|
1108
|
+
return JSON.parse(args || '{}') as Record<string, unknown>;
|
|
1109
|
+
} catch {
|
|
1110
|
+
return {};
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
```
|
|
1115
|
+
|
|
1116
|
+
`model.ts`
|
|
1117
|
+
|
|
1118
|
+
```tsx
|
|
1119
|
+
/**
|
|
1120
|
+
* What a model's stream gives, once read. Most providers send these five things, each in its own words, and a model
|
|
1121
|
+
* that searches names its sources too: your server, or the provider's SDK, turns its events into them, and the chat
|
|
1122
|
+
* never sees the provider.
|
|
1123
|
+
*/
|
|
1124
|
+
export type ModelEvent =
|
|
1125
|
+
| { type: 'thinking'; delta: string }
|
|
1126
|
+
| { type: 'text'; delta: string }
|
|
1127
|
+
| { type: 'tool-start'; id: string; name: string }
|
|
1128
|
+
| { type: 'tool-args'; id: string; delta: string }
|
|
1129
|
+
| { type: 'tool-end'; id: string }
|
|
1130
|
+
| { type: 'source'; title: string; url?: string; snippet?: string };
|
|
1131
|
+
|
|
1132
|
+
/** A call the model made: its arguments as JSON text. */
|
|
1133
|
+
export interface ModelCall {
|
|
1134
|
+
id: string;
|
|
1135
|
+
name: string;
|
|
1136
|
+
args: string;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/** The conversation as the model reads it: what the user said, what the model said and called, what each call returned. */
|
|
1140
|
+
export type ModelMessage =
|
|
1141
|
+
| { role: 'user'; text: string }
|
|
1142
|
+
| { role: 'assistant'; text: string; calls: ModelCall[] }
|
|
1143
|
+
| { role: 'tool'; id: string; result: string };
|
|
1144
|
+
|
|
1145
|
+
/** A request that failed; `retryable`: it may work if sent again (the provider was busy, the network dropped). */
|
|
1146
|
+
export class ModelError extends Error {
|
|
1147
|
+
constructor(message: string, readonly retryable = false) {
|
|
1148
|
+
super(message);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/** A model: one request, and its events as they arrive. `signal` cancels it; a failure throws a `ModelError`. */
|
|
1153
|
+
export interface Model {
|
|
1154
|
+
stream(history: readonly ModelMessage[], signal: AbortSignal): AsyncIterable<ModelEvent>;
|
|
1155
|
+
}
|
|
1156
|
+
```
|
|
1157
|
+
|
|
1158
|
+
`demo-tools.ts`
|
|
1159
|
+
|
|
1160
|
+
```tsx
|
|
1161
|
+
import type { ChatFormSchema, ChatQuestion, ChatTodoItem } from '@motifx-community/ui';
|
|
1162
|
+
import type { AppTool } from './connect';
|
|
1163
|
+
import { pause, typed } from './fake-model';
|
|
1164
|
+
|
|
1165
|
+
/** What the git server asks before it commits, as an MCP server's elicitation names its fields. */
|
|
1166
|
+
const COMMIT: ChatFormSchema = {
|
|
1167
|
+
type: 'object',
|
|
1168
|
+
properties: {
|
|
1169
|
+
message: { type: 'string', title: 'Message', description: 'The first line of the commit', minLength: 5, default: 'Clear the old timer in debounce' },
|
|
1170
|
+
branch: { type: 'string', title: 'Branch', enum: ['main', 'fix/debounce'], enumNames: ['main', 'fix/debounce (new)'], default: 'fix/debounce' },
|
|
1171
|
+
push: { type: 'boolean', title: 'Push it', default: false }
|
|
1172
|
+
},
|
|
1173
|
+
required: ['message', 'branch']
|
|
1174
|
+
};
|
|
1175
|
+
|
|
1176
|
+
const SOURCE = `export function debounce(run: () => void, wait = 300) {
|
|
1177
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
1178
|
+
return () => {
|
|
1179
|
+
timer = setTimeout(run, wait);
|
|
1180
|
+
};
|
|
1181
|
+
}`;
|
|
1182
|
+
|
|
1183
|
+
const TESTS = ['> vitest run debounce', '', ' ✓ runs once after the wait', ' ✓ uses the last call', ' × runs once for many calls', ' expected 1 call, got 3', '', ' Tests 1 failed | 2 passed (3)'];
|
|
1184
|
+
|
|
1185
|
+
const DIFF = ` return () => {
|
|
1186
|
+
+ clearTimeout(timer);
|
|
1187
|
+
timer = setTimeout(run, wait);
|
|
1188
|
+
};`;
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* The demo's tools. Asking the user, the plan and the to-do list are tools of the model that draw into the reply; the
|
|
1192
|
+
* others stand in for reading a file, running a command, editing and a sub-agent, and take a while as real ones do.
|
|
1193
|
+
*/
|
|
1194
|
+
export const tools: Record<string, AppTool> = {
|
|
1195
|
+
ask_user: {
|
|
1196
|
+
quiet: true,
|
|
1197
|
+
run: async (args, reply) => JSON.stringify(await reply.questions(args.questions as ChatQuestion[]))
|
|
1198
|
+
},
|
|
1199
|
+
plan: {
|
|
1200
|
+
quiet: true,
|
|
1201
|
+
run: async (args, reply) => JSON.stringify(await reply.plan(String(args.plan)))
|
|
1202
|
+
},
|
|
1203
|
+
todo: {
|
|
1204
|
+
quiet: true,
|
|
1205
|
+
run: async (args, reply) => {
|
|
1206
|
+
reply.todo(args.items as ChatTodoItem[]);
|
|
1207
|
+
return 'Saved.';
|
|
1208
|
+
}
|
|
1209
|
+
},
|
|
1210
|
+
Read: {
|
|
1211
|
+
density: 'line',
|
|
1212
|
+
title: (args) => String(args.path),
|
|
1213
|
+
run: async (_, reply) => {
|
|
1214
|
+
await pause(500, reply.signal);
|
|
1215
|
+
return SOURCE;
|
|
1216
|
+
}
|
|
1217
|
+
},
|
|
1218
|
+
Bash: {
|
|
1219
|
+
look: 'terminal',
|
|
1220
|
+
ask: 'Yes, and allow npm test from now on',
|
|
1221
|
+
title: (args) => String(args.description ?? args.command),
|
|
1222
|
+
run: async (_, reply, call) => {
|
|
1223
|
+
for (const line of TESTS) {
|
|
1224
|
+
call?.output(`${line}\n`);
|
|
1225
|
+
await pause(140, reply.signal);
|
|
1226
|
+
}
|
|
1227
|
+
throw new Error('Exit code 1');
|
|
1228
|
+
}
|
|
1229
|
+
},
|
|
1230
|
+
Edit: {
|
|
1231
|
+
look: 'diff',
|
|
1232
|
+
title: (args) => String(args.path),
|
|
1233
|
+
run: async (_, reply) => {
|
|
1234
|
+
await pause(300, reply.signal);
|
|
1235
|
+
return DIFF;
|
|
1236
|
+
}
|
|
1237
|
+
},
|
|
1238
|
+
Task: {
|
|
1239
|
+
label: 'Agent',
|
|
1240
|
+
title: (args) => String(args.description),
|
|
1241
|
+
// A real sub-agent is a model loop of its own, writing into `call` as the agent writes into the reply.
|
|
1242
|
+
run: async (_, reply, call) => {
|
|
1243
|
+
if (!call) return '';
|
|
1244
|
+
await typed((d) => call.text(d), 'Searching for other uses of `debounce`…', reply.signal);
|
|
1245
|
+
const grep = call.tool({ name: 'Grep', title: 'debounce(', input: 'debounce\\(', density: 'line' });
|
|
1246
|
+
await pause(500, reply.signal);
|
|
1247
|
+
grep.done('src/search.ts:12\nsrc/resize.ts:4');
|
|
1248
|
+
await typed((d) => call.text(d), ' Two callers; both pass a function and a wait, so nothing else changes.', reply.signal);
|
|
1249
|
+
return 'Two callers, no change needed.';
|
|
1250
|
+
}
|
|
1251
|
+
},
|
|
1252
|
+
// A tool of an MCP server that asks the user for what it needs: its elicitation is a form in the composer's place
|
|
1253
|
+
// (`call.form()`), and the answer goes to the server as the elicitation's result.
|
|
1254
|
+
commit: {
|
|
1255
|
+
label: 'Commit',
|
|
1256
|
+
title: () => 'git',
|
|
1257
|
+
run: async (_, reply, call) => {
|
|
1258
|
+
const said = await (call ?? reply).form('What should the commit say, and where should it go?', COMMIT, { source: 'git' });
|
|
1259
|
+
if (said.action !== 'accept') return said.action === 'decline' ? 'The user declined the commit.' : 'The user dismissed the commit.';
|
|
1260
|
+
await pause(400, reply.signal);
|
|
1261
|
+
const c = said.content ?? {};
|
|
1262
|
+
return `Committed "${String(c.message)}" to ${String(c.branch)}${c.push ? ', and pushed' : ''}.`;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
```
|
|
1267
|
+
|
|
1268
|
+
`fake-model.ts`
|
|
1269
|
+
|
|
1270
|
+
```tsx
|
|
1271
|
+
import type { ChatMessage, ChatPart, ChatPlanAnswer, ChatQuestion, ChatQuestionsAnswer, ChatTodoItem } from '@motifx-community/ui';
|
|
1272
|
+
import { ModelError, type Model, type ModelEvent, type ModelMessage } from './model';
|
|
1273
|
+
|
|
1274
|
+
/** Waits, or rejects as soon as `signal` is aborted. */
|
|
1275
|
+
export function pause(ms: number, signal: AbortSignal): Promise<void> {
|
|
1276
|
+
return new Promise((done, fail) => {
|
|
1277
|
+
if (signal.aborted) {
|
|
1278
|
+
fail(signal.reason);
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
const timer = setTimeout(done, ms);
|
|
1282
|
+
signal.addEventListener('abort', () => {
|
|
1283
|
+
clearTimeout(timer);
|
|
1284
|
+
fail(signal.reason);
|
|
1285
|
+
}, { once: true });
|
|
1286
|
+
});
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
/** Writes `text` a few characters at a time, as a model streams. */
|
|
1290
|
+
export async function typed(write: (delta: string) => void, text: string, signal: AbortSignal): Promise<void> {
|
|
1291
|
+
for (let at = 0; at < text.length; ) {
|
|
1292
|
+
const piece = text.slice(at, at + 2 + Math.floor(Math.random() * 6));
|
|
1293
|
+
at += piece.length;
|
|
1294
|
+
write(piece);
|
|
1295
|
+
await pause(22, signal);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
const QUESTIONS: ChatQuestion[] = [
|
|
1300
|
+
{
|
|
1301
|
+
text: 'When should the call run?',
|
|
1302
|
+
header: 'Timing',
|
|
1303
|
+
options: [
|
|
1304
|
+
{ label: 'After the last call', description: 'Waits until the calls stop', preview: '```\ncalls x xx x\nruns ● ●\n```' },
|
|
1305
|
+
{ label: 'On the first call', description: 'Runs at once, then waits', preview: '```\ncalls x xx x\nruns ● ●\n```' }
|
|
1306
|
+
]
|
|
1307
|
+
},
|
|
1308
|
+
{ text: 'Anything else to add?', header: 'Extras', multiple: true, options: [{ label: 'A cancel() method' }, { label: 'A test for many calls' }] }
|
|
1309
|
+
];
|
|
1310
|
+
|
|
1311
|
+
const TASKS = ['Read the debounce helper', 'Run its tests', 'Fix the failing case', 'Commit the fix'];
|
|
1312
|
+
|
|
1313
|
+
/** What a model that searches names as its sources. */
|
|
1314
|
+
const SOURCES = [
|
|
1315
|
+
{ title: 'clearTimeout()', url: 'https://developer.mozilla.org/docs/Web/API/Window/clearTimeout', snippet: 'Cancels a timeout previously established by calling setTimeout().' },
|
|
1316
|
+
{ title: 'src/debounce.ts', snippet: 'The helper, as it was read.' }
|
|
1317
|
+
];
|
|
1318
|
+
|
|
1319
|
+
const ANSWER = `Fixed. The returned function started a new timer on every call **without clearing the old one**, so each call ran:
|
|
1320
|
+
|
|
1321
|
+
\`\`\`ts
|
|
1322
|
+
return () => {
|
|
1323
|
+
clearTimeout(timer);
|
|
1324
|
+
timer = setTimeout(run, wait);
|
|
1325
|
+
};
|
|
1326
|
+
\`\`\`
|
|
1327
|
+
|
|
1328
|
+
| Test | Before | After |
|
|
1329
|
+
|:-----|:------:|:-----:|
|
|
1330
|
+
| runs once after the wait | ✓ | ✓ |
|
|
1331
|
+
| runs once for many calls | × | ✓ |
|
|
1332
|
+
|
|
1333
|
+
The two other callers (\`search.ts\`, \`resize.ts\`) need no change.[^1]`;
|
|
1334
|
+
|
|
1335
|
+
const NOTE = '[^1]: Both already wait for the last call: `search.ts` through its own timer, `resize.ts` through `requestAnimationFrame`.';
|
|
1336
|
+
|
|
1337
|
+
/** The to-do list with `done` tasks done and the next one under way. */
|
|
1338
|
+
function tasks(done: number): ChatTodoItem[] {
|
|
1339
|
+
return TASKS.map((text, i) => ({ text, status: i < done ? 'done' : i === done ? 'active' : 'pending' }));
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* Stands in for your model: a scripted agent that answers anything the same way, one step a request, reading what its
|
|
1344
|
+
* calls returned. It asks two questions, shows a plan, keeps a to-do list, reads a file, runs the tests (one fails),
|
|
1345
|
+
* edits the file (its arguments arrive in pieces), sends a sub-agent to look around, commits through a git server
|
|
1346
|
+
* that asks for the message, and answers, naming its sources. What the user says to the questions, the plan, the
|
|
1347
|
+
* permission and the commit changes what it does. Its very first request is refused as busy once, as a provider does
|
|
1348
|
+
* at times: the agent tries it again.
|
|
1349
|
+
*/
|
|
1350
|
+
export class FakeModel implements Model {
|
|
1351
|
+
private calls = 0;
|
|
1352
|
+
private refused = false;
|
|
1353
|
+
|
|
1354
|
+
async *stream(history: readonly ModelMessage[], signal: AbortSignal): AsyncIterable<ModelEvent> {
|
|
1355
|
+
if (!this.refused) {
|
|
1356
|
+
this.refused = true;
|
|
1357
|
+
await pause(300, signal);
|
|
1358
|
+
throw new ModelError('Overloaded', true);
|
|
1359
|
+
}
|
|
1360
|
+
// This turn: from the user's last message on.
|
|
1361
|
+
let start = history.length - 1;
|
|
1362
|
+
while (start > 0 && history[start].role !== 'user') start--;
|
|
1363
|
+
const turn = history.slice(start);
|
|
1364
|
+
const names = new Map<string, string>();
|
|
1365
|
+
const results = new Map<string, string>();
|
|
1366
|
+
for (const m of turn) {
|
|
1367
|
+
if (m.role === 'assistant') for (const c of m.calls) names.set(c.id, c.name);
|
|
1368
|
+
else if (m.role === 'tool') results.set(names.get(m.id) ?? '', m.result);
|
|
1369
|
+
}
|
|
1370
|
+
const asked = results.get('ask_user');
|
|
1371
|
+
const answers = asked ? (JSON.parse(asked) as ChatQuestionsAnswer) : null;
|
|
1372
|
+
const timing = ((!answers?.declined && answers?.answers.find((a) => a.id === '1')?.values[0]) || 'After the last call').toLowerCase();
|
|
1373
|
+
switch (turn.filter((m) => m.role === 'assistant').length) {
|
|
1374
|
+
case 0: {
|
|
1375
|
+
const question = turn[0].role === 'user' ? turn[0].text : '';
|
|
1376
|
+
yield* this.say('thinking', `The question: "${question}". Before I start I need to know how the helper should behave.`, signal);
|
|
1377
|
+
await pause(400, signal);
|
|
1378
|
+
yield* this.call('ask_user', { questions: QUESTIONS }, signal);
|
|
1379
|
+
return;
|
|
1380
|
+
}
|
|
1381
|
+
case 1:
|
|
1382
|
+
yield* this.call('plan', { plan: `1. Read \`src/debounce.ts\`\n2. Run its tests to see what fails\n3. Make it run **${timing}**\n4. Check the other callers` }, signal);
|
|
1383
|
+
return;
|
|
1384
|
+
case 2: {
|
|
1385
|
+
const plan = JSON.parse(results.get('plan') ?? '{}') as ChatPlanAnswer;
|
|
1386
|
+
if (plan.choice === 'no') {
|
|
1387
|
+
yield* this.say('text', plan.text ? `All right, I will plan again with that in mind: *${plan.text}*.` : 'All right: tell me what to change in the plan.', signal);
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
yield* this.call('todo', { items: tasks(0) }, signal);
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
case 3:
|
|
1394
|
+
yield* this.call('Read', { path: 'src/debounce.ts' }, signal);
|
|
1395
|
+
return;
|
|
1396
|
+
case 4:
|
|
1397
|
+
yield* this.call('todo', { items: tasks(1) }, signal);
|
|
1398
|
+
return;
|
|
1399
|
+
case 5:
|
|
1400
|
+
yield* this.call('Bash', { command: 'npm test -- debounce', description: 'Run the tests' }, signal);
|
|
1401
|
+
return;
|
|
1402
|
+
case 6: {
|
|
1403
|
+
const ran = results.get('Bash') ?? '';
|
|
1404
|
+
if (ran.startsWith('The user did not allow it')) {
|
|
1405
|
+
yield* this.say('text', 'I did not run the tests. What should I do instead?', signal);
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
yield* this.say('thinking', 'The returned function starts a new timer on every call and never clears the old one.', signal);
|
|
1409
|
+
yield* this.call('Edit', { path: 'src/debounce.ts', old: ' timer = setTimeout(run, wait);', new: ' clearTimeout(timer);\n timer = setTimeout(run, wait);' }, signal, 4);
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
case 7:
|
|
1413
|
+
yield* this.call('Task', { description: 'Look for other callers' }, signal);
|
|
1414
|
+
return;
|
|
1415
|
+
case 8:
|
|
1416
|
+
yield* this.call('todo', { items: tasks(3) }, signal);
|
|
1417
|
+
return;
|
|
1418
|
+
case 9:
|
|
1419
|
+
yield* this.call('commit', {}, signal);
|
|
1420
|
+
return;
|
|
1421
|
+
case 10:
|
|
1422
|
+
yield* this.call('todo', { items: tasks(4) }, signal);
|
|
1423
|
+
return;
|
|
1424
|
+
default: {
|
|
1425
|
+
const commit = results.get('commit') ?? '';
|
|
1426
|
+
yield* this.say('text', `${ANSWER}\n\nIt now runs ${timing}. ${commit}\n\n${NOTE}`, signal);
|
|
1427
|
+
for (const source of SOURCES) yield { type: 'source', ...source };
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
private async *say(type: 'thinking' | 'text', text: string, signal: AbortSignal): AsyncIterable<ModelEvent> {
|
|
1433
|
+
for (let at = 0; at < text.length; ) {
|
|
1434
|
+
const delta = text.slice(at, at + 2 + Math.floor(Math.random() * 6));
|
|
1435
|
+
at += delta.length;
|
|
1436
|
+
yield { type, delta };
|
|
1437
|
+
await pause(22, signal);
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
/** A call, its arguments in `pieces` that take a while each, as a long argument arrives. */
|
|
1442
|
+
private async *call(name: string, args: unknown, signal: AbortSignal, pieces = 1): AsyncIterable<ModelEvent> {
|
|
1443
|
+
const id = `call-${++this.calls}`;
|
|
1444
|
+
yield { type: 'tool-start', id, name };
|
|
1445
|
+
const json = JSON.stringify(args);
|
|
1446
|
+
const size = Math.ceil(json.length / pieces);
|
|
1447
|
+
for (let at = 0; at < json.length; at += size) {
|
|
1448
|
+
yield { type: 'tool-args', id, delta: json.slice(at, at + size) };
|
|
1449
|
+
if (pieces > 1) await pause(350, signal);
|
|
1450
|
+
}
|
|
1451
|
+
yield { type: 'tool-end', id };
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
const TOPICS = ['the router', 'the date box', 'a flaky test', 'the build', 'the grid filter', 'dark mode', 'the release notes', 'a slow page'];
|
|
1456
|
+
|
|
1457
|
+
/** One turn of an earlier conversation, as a server would return it. */
|
|
1458
|
+
function turn(n: number, at: Date): ChatMessage[] {
|
|
1459
|
+
const topic = TOPICS[n % TOPICS.length];
|
|
1460
|
+
const parts: ChatPart[] = [{ kind: 'thinking', seconds: 1 + (n % 5), notes: [`Looking at ${topic}`] }];
|
|
1461
|
+
if (n % 2 === 0) parts.push({ kind: 'tool', id: `t${n}`, name: 'Read', title: `src/${topic.replace(/\s/g, '-')}.ts`, status: 'done', density: 'line', input: 'src/…', output: '…' });
|
|
1462
|
+
if (n % 5 === 0) {
|
|
1463
|
+
parts.push({
|
|
1464
|
+
kind: 'questions',
|
|
1465
|
+
id: `q${n}`,
|
|
1466
|
+
questions: [{ id: '1', text: 'Which file should I start with?', options: [{ label: 'The view' }, { label: 'The model' }] }],
|
|
1467
|
+
answer: { declined: false, answers: [{ id: '1', values: ['The model'] }] }
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
if (n % 3 === 0) {
|
|
1471
|
+
parts.push({
|
|
1472
|
+
kind: 'tool',
|
|
1473
|
+
id: `b${n}`,
|
|
1474
|
+
name: 'Bash',
|
|
1475
|
+
title: 'Run the tests',
|
|
1476
|
+
status: 'done',
|
|
1477
|
+
look: 'terminal',
|
|
1478
|
+
input: 'npm test',
|
|
1479
|
+
output: ' ✓ 42 passed\n Tests 42 passed (42)',
|
|
1480
|
+
permission: { answer: { choice: 'yes' } }
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
let said = `Turn ${n}: looked at **${topic}**.${n % 4 === 0 ? `\n\n- checked the cause\n- wrote the fix\n- ran the tests\n\n\`\`\`ts\nexport const answer${n} = ${n};\n\`\`\`` : ' It is fine as it is.'}`;
|
|
1484
|
+
// A long block now and then: the chat shows its first lines (`codeLines`) and a button for the rest.
|
|
1485
|
+
if (n % 7 === 0) said += `\n\nThe whole file:\n\n\`\`\`ts\n${Array.from({ length: 24 }, (_, i) => `export const step${i + 1} = () => check('${topic}', ${i + 1});`).join('\n')}\n\`\`\``;
|
|
1486
|
+
parts.push({ kind: 'text', text: said });
|
|
1487
|
+
return [
|
|
1488
|
+
{ id: `q${n}`, role: 'user', time: at, parts: [{ kind: 'text', text: `Can you check ${topic}? (${n})` }] },
|
|
1489
|
+
{ id: `a${n}`, role: 'assistant', time: new Date(at.getTime() + 20_000), parts }
|
|
1490
|
+
];
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
let first = 0;
|
|
1494
|
+
|
|
1495
|
+
/** The last `count` turns of a long conversation: the chat opens at its end. */
|
|
1496
|
+
export function history(count: number): ChatMessage[] {
|
|
1497
|
+
const start = Date.now() - count * 120_000;
|
|
1498
|
+
first = 1000;
|
|
1499
|
+
const messages: ChatMessage[] = [];
|
|
1500
|
+
for (let i = 0; i < count; i++) messages.push(...turn(first + i, new Date(start + i * 120_000)));
|
|
1501
|
+
return messages;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
/** The page of turns before the first one shown. */
|
|
1505
|
+
export function olderPage(): ChatMessage[] {
|
|
1506
|
+
const messages: ChatMessage[] = [];
|
|
1507
|
+
const start = Date.now() - (first + 20) * 120_000;
|
|
1508
|
+
for (let i = 20; i > 0; i--) messages.push(...turn(first - i, new Date(start + (20 - i) * 120_000)));
|
|
1509
|
+
first -= 20;
|
|
1510
|
+
return messages;
|
|
1511
|
+
}
|
|
1512
|
+
```
|