lhcb-ntuple-wizard-test 1.1.14 → 1.2.1

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.
Files changed (35) hide show
  1. package/dist/components/ConfigDict.js +39 -38
  2. package/dist/components/ConfigList.js +33 -29
  3. package/dist/components/ConfigNode.js +135 -132
  4. package/dist/components/ConfigValue.js +22 -18
  5. package/dist/components/Dataset.js +7 -3
  6. package/dist/components/Decay.js +1 -2
  7. package/dist/components/DecayItem.js +49 -45
  8. package/dist/components/DecayTag.js +10 -7
  9. package/dist/components/DecayTree.js +153 -146
  10. package/dist/components/DecaysList.js +38 -35
  11. package/dist/components/DescriptorsSearch.js +192 -187
  12. package/dist/components/EventTypeBadge.js +9 -6
  13. package/dist/components/ItemSearch.js +15 -10
  14. package/dist/components/LinesTable.js +464 -462
  15. package/dist/components/NtupleWizard.js +32 -27
  16. package/dist/components/ParticleTag.js +10 -7
  17. package/dist/components/SearchItem.js +34 -31
  18. package/dist/components/SelectParticle.js +14 -10
  19. package/dist/components/SelectTag.js +10 -9
  20. package/dist/components/SelectTool.js +10 -9
  21. package/dist/components/SelectVariables.js +24 -22
  22. package/dist/components/StrippingBadge.js +10 -7
  23. package/dist/components/StrippingLine.js +8 -6
  24. package/dist/components/TupleTool.js +4 -3
  25. package/dist/components/VariablesSearch.js +15 -10
  26. package/dist/components/loki/LokiDict.js +184 -0
  27. package/dist/components/loki/LokiEditor.js +199 -0
  28. package/dist/components/loki/LokiForm.js +116 -0
  29. package/dist/components/semantic.js +97 -0
  30. package/dist/components/semform.js +184 -0
  31. package/dist/components/worker.js +196 -0
  32. package/dist/config.json +1 -1
  33. package/dist/contexts/MetadataContext.js +8 -7
  34. package/dist/lib/DTTConfig.js +8 -8
  35. package/package.json +25 -2
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireDefault(require("react"));
8
+ var _LokiEditor = _interopRequireDefault(require("./LokiEditor"));
9
+ var _Grid = _interopRequireDefault(require("@mui/material/Grid"));
10
+ var _propTypes = _interopRequireDefault(require("prop-types"));
11
+ var _reactBootstrap = require("react-bootstrap");
12
+ var _reactBootstrapIcons = require("react-bootstrap-icons");
13
+ var _LokiForm = _interopRequireDefault(require("./LokiForm"));
14
+ var _jsxRuntime = require("react/jsx-runtime");
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
17
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
18
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*****************************************************************************\
19
+ * (c) Copyright 2024 CERN for the benefit of the LHCb Collaboration *
20
+ * *
21
+ * This software is distributed under the terms of the GNU General Public *
22
+ * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
23
+ * *
24
+ * In applying this licence, CERN does not waive the privileges and immunities *
25
+ * granted to it by virtue of its status as an Intergovernmental Organization *
26
+ * or submit itself to any jurisdiction. *
27
+ \*****************************************************************************/
28
+ class LokiDict extends _react.default.Component {
29
+ constructor() {
30
+ super(...arguments);
31
+ _defineProperty(this, "state", {
32
+ lines: this.props.value,
33
+ error: "",
34
+ add: false,
35
+ currentKey: "",
36
+ currentValue: ""
37
+ });
38
+ _defineProperty(this, "handleClose", () => {
39
+ this.setState({
40
+ add: false
41
+ });
42
+ });
43
+ _defineProperty(this, "handleAdd", () => {
44
+ var lines = this.state.lines;
45
+ const key = this.state.currentKey;
46
+ const value = this.state.currentValue;
47
+ lines[key] = value;
48
+ this.setState({
49
+ lines: lines
50
+ });
51
+ this.setState({
52
+ currentKey: ""
53
+ });
54
+ this.setState({
55
+ currentValue: ""
56
+ });
57
+ this.props.callback(this.state.lines);
58
+ });
59
+ _defineProperty(this, "handleRemove", key => {
60
+ var lines = this.state.lines;
61
+ delete lines[key];
62
+ this.setState({
63
+ lines: lines
64
+ });
65
+ this.props.callback(this.state.lines);
66
+ });
67
+ _defineProperty(this, "setValueByKey", (key, value) => {
68
+ var lines = this.state.lines;
69
+ lines[key] = value;
70
+ this.setState({
71
+ lines: lines
72
+ });
73
+ this.props.callback(this.state.lines);
74
+ });
75
+ _defineProperty(this, "setAddLine", () => {
76
+ this.setState({
77
+ add: true
78
+ });
79
+ });
80
+ _defineProperty(this, "changeKey", value => {
81
+ this.setState({
82
+ currentKey: value
83
+ });
84
+ });
85
+ _defineProperty(this, "validKey", value => {
86
+ if (value in this.state.lines) {
87
+ return false;
88
+ }
89
+ return true;
90
+ });
91
+ _defineProperty(this, "changeValue", value => {
92
+ this.setState({
93
+ currentValue: value
94
+ });
95
+ });
96
+ }
97
+ render() {
98
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactBootstrap.Card, {
99
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactBootstrap.Card.Header, {
100
+ className: "d-flex justify-content-between align-items-start",
101
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Card.Title, {
102
+ children: "Loki Lines"
103
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Button, {
104
+ variant: "success",
105
+ size: "sm",
106
+ onClick: this.setAddLine,
107
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrapIcons.PlusLg, {})
108
+ })]
109
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.ListGroup, {
110
+ variant: "flush",
111
+ children: Object.keys(this.state.lines).map(key => {
112
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.ListGroup.Item, {
113
+ className: "d-flex justify-content-between align-items-start",
114
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Grid.default, {
115
+ container: true,
116
+ spacing: 1,
117
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Grid.default, {
118
+ className: "d-flex",
119
+ item: true,
120
+ form: "maincomponent",
121
+ xs: true,
122
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
123
+ children: [key, ": "]
124
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_LokiEditor.default, {
125
+ callback: value => {
126
+ this.setValueByKey(key, value);
127
+ },
128
+ value: this.state.lines[key],
129
+ lokiVars: this.props.lokiVars,
130
+ oneLine: true,
131
+ height: "24px",
132
+ width: "40dvw"
133
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Button, {
134
+ variant: "danger",
135
+ size: "sm",
136
+ onClick: () => this.handleRemove(key),
137
+ children: "Remove"
138
+ })]
139
+ })
140
+ })
141
+ }, key);
142
+ })
143
+ }), this.state.add ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactBootstrap.Modal, {
144
+ show: true,
145
+ onHide: this.handleClose,
146
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Modal.Header, {
147
+ closeButton: true,
148
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Modal.Title, {
149
+ children: "Add Dict Item"
150
+ })
151
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Modal.Body, {
152
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_LokiForm.default, {
153
+ value: "",
154
+ keyCallback: this.changeKey,
155
+ valueCallback: this.changeValue,
156
+ lokiVars: this.props.lokiVars,
157
+ validateKey: this.validKey
158
+ }, "")
159
+ }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactBootstrap.Modal.Footer, {
160
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Button, {
161
+ variant: "secondary",
162
+ onClick: this.handleClose,
163
+ children: "Close"
164
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Button, {
165
+ variant: "primary",
166
+ onClick: () => {
167
+ this.handleAdd();
168
+ this.handleClose();
169
+ },
170
+ disabled: this.state.currentKey === "" || this.state.currentValue === "" || !this.validKey(this.state.currentKey),
171
+ children: "Save Changes"
172
+ })]
173
+ })]
174
+ }) : ""]
175
+ });
176
+ }
177
+ }
178
+ LokiDict.propTypes = {
179
+ callback: _propTypes.default.func,
180
+ defaulValue: _propTypes.default.string,
181
+ value: _propTypes.default.object,
182
+ lokiVars: _propTypes.default.object
183
+ };
184
+ var _default = exports.default = LokiDict;
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireDefault(require("react"));
8
+ var _reactMonacoEditor = _interopRequireDefault(require("react-monaco-editor"));
9
+ var monaco = _interopRequireWildcard(require("monaco-editor"));
10
+ var _react2 = require("@monaco-editor/react");
11
+ var _Spinner = _interopRequireDefault(require("react-bootstrap/Spinner"));
12
+ var _propTypes = _interopRequireDefault(require("prop-types"));
13
+ var _jsxRuntime = require("react/jsx-runtime");
14
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
+ /*****************************************************************************\
17
+ * (c) Copyright 2024 CERN for the benefit of the LHCb Collaboration *
18
+ * *
19
+ * This software is distributed under the terms of the GNU General Public *
20
+ * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
21
+ * *
22
+ * In applying this licence, CERN does not waive the privileges and immunities *
23
+ * granted to it by virtue of its status as an Intergovernmental Organization *
24
+ * or submit itself to any jurisdiction. *
25
+ \*****************************************************************************/
26
+
27
+ // import { monaco } from 'react-monaco-editor';
28
+
29
+ _react2.loader.config({
30
+ monaco
31
+ });
32
+ let LokiData = {};
33
+ function createDependencyProposals(range) {
34
+ // returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
35
+ // here you could do a server side lookup
36
+ let listOfFunctors = [];
37
+ Object.keys(LokiData).forEach((key, _index) => {
38
+ listOfFunctors.push({
39
+ label: key,
40
+ kind: monaco.languages.CompletionItemKind.Function,
41
+ documentation: LokiData[key].description,
42
+ insertText: key,
43
+ range: range
44
+ });
45
+ });
46
+ return listOfFunctors;
47
+ }
48
+ _react2.loader.init().then(monaco => {
49
+ monaco.languages.registerCompletionItemProvider("python", {
50
+ provideCompletionItems: function (model, position) {
51
+ var word = model.getWordUntilPosition(position);
52
+ var range = {
53
+ startLineNumber: position.lineNumber,
54
+ endLineNumber: position.lineNumber,
55
+ startColumn: word.startColumn,
56
+ endColumn: word.endColumn
57
+ };
58
+ return {
59
+ suggestions: createDependencyProposals(range)
60
+ };
61
+ }
62
+ });
63
+ monaco.languages.registerHoverProvider("python", {
64
+ provideHover: function (model, position) {
65
+ let hoverObjects = [];
66
+ let matches = Object.keys(LokiData).filter(name => {
67
+ return model.findMatches(name, false, false, true, " (:,)*").length !== 0;
68
+ });
69
+ if (matches.length > 0) {
70
+ matches.forEach(match => model.findMatches(match, false, false, true).forEach(range => hoverObjects.push({
71
+ match: match,
72
+ position: position,
73
+ range: range,
74
+ contents: [{
75
+ supportHtml: true,
76
+ value: LokiData[match].documentation
77
+ }]
78
+ })));
79
+ for (const element of hoverObjects) {
80
+ if (contains(element.range, position)) {
81
+ // alert(element.contents[0].value)
82
+ return element;
83
+ }
84
+ }
85
+ }
86
+ return {
87
+ range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
88
+ contents: [{
89
+ supportHtml: true,
90
+ value: '<span style="color red;">No matches</span>',
91
+ isTrusted: true
92
+ }]
93
+ };
94
+ }
95
+ });
96
+ });
97
+ function contains(range, position) {
98
+ if (range.range.startLineNumber <= position.lineNumber && range.range.endLineNumber >= position.lineNumber) {
99
+ if (range.range.startColumn <= position.column && range.range.endColumn >= position.column) {
100
+ return true;
101
+ }
102
+ }
103
+ return false;
104
+ }
105
+ class LokiEditor extends _react.default.Component {
106
+ constructor(props) {
107
+ super(props);
108
+ this.state = {
109
+ code: this.props.returnList ? this.props.value.join("\n") : this.props.value,
110
+ loaded: true
111
+ };
112
+ }
113
+ editorDidMount(editor, _monaco) {
114
+ editor.focus();
115
+ }
116
+ onChange(newValue, _e) {
117
+ if (this.props.oneLine) {
118
+ newValue = newValue.replace(/[\n\r]/g, "");
119
+ }
120
+ this.setState({
121
+ code: newValue
122
+ });
123
+ if (this.props.returnList) {
124
+ newValue = newValue.split(/\r?\n/);
125
+ }
126
+ this.props.callback(newValue);
127
+ }
128
+ render() {
129
+ const code = this.state.code;
130
+ const lokiVars = this.props.lokiVars.lokiVariables;
131
+ Object.keys(lokiVars).forEach(key => {
132
+ LokiData[key] = lokiVars[key];
133
+ });
134
+ const options = {
135
+ autoIndent: "full",
136
+ contextmenu: false,
137
+ fontFamily: "monospace",
138
+ fontSize: 18,
139
+ lineHeight: 24,
140
+ hideCursorInOverviewRuler: true,
141
+ matchBrackets: "always",
142
+ lineNumbers: "off",
143
+ lineDecorationsWidth: 0,
144
+ lineNumbersMinChars: 0,
145
+ glyphMargin: false,
146
+ wordWrap: "on",
147
+ minimap: {
148
+ enabled: false
149
+ },
150
+ scrollbar: {
151
+ horizontal: "hidden",
152
+ vertical: "hidden",
153
+ horizontalSliderSize: 0,
154
+ verticalSliderSize: 0,
155
+ horizontalScrollbarSize: 0,
156
+ verticalScrollbarSize: 0
157
+ },
158
+ scrollBeyondLastLine: false,
159
+ selectOnLineNumbers: true,
160
+ roundedSelection: false,
161
+ readOnly: false,
162
+ cursorStyle: "line",
163
+ automaticLayout: true,
164
+ fixedOverflowWidgets: true,
165
+ links: true
166
+ };
167
+ return this.state.loaded ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactMonacoEditor.default, {
168
+ width: this.props.width,
169
+ height: this.props.height,
170
+ language: "python",
171
+ theme: "vs-light",
172
+ value: code,
173
+ options: options,
174
+ onChange: this.onChange.bind(this),
175
+ editorDidMount: this.editorDidMount.bind(this)
176
+ }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Spinner.default, {
177
+ animation: "border",
178
+ role: "status"
179
+ });
180
+ }
181
+ }
182
+ LokiEditor.defaultProps = {
183
+ value: "",
184
+ oneLine: false,
185
+ width: "30vh",
186
+ height: "10vh",
187
+ returnList: false
188
+ };
189
+ LokiEditor.propTypes = {
190
+ callback: _propTypes.default.func,
191
+ value: _propTypes.default.string,
192
+ defaultValue: _propTypes.default.string,
193
+ lokiVars: _propTypes.default.object,
194
+ oneLine: _propTypes.default.bool,
195
+ height: _propTypes.default.string,
196
+ width: _propTypes.default.string,
197
+ returnList: _propTypes.default.bool
198
+ };
199
+ var _default = exports.default = LokiEditor;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireDefault(require("react"));
8
+ var _LokiEditor = _interopRequireDefault(require("./LokiEditor"));
9
+ var _propTypes = _interopRequireDefault(require("prop-types"));
10
+ var _reactBootstrap = require("react-bootstrap");
11
+ var _Grid = _interopRequireDefault(require("@mui/material/Grid"));
12
+ var _jsxRuntime = require("react/jsx-runtime");
13
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
16
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*****************************************************************************\
17
+ * (c) Copyright 2024 CERN for the benefit of the LHCb Collaboration *
18
+ * *
19
+ * This software is distributed under the terms of the GNU General Public *
20
+ * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
21
+ * *
22
+ * In applying this licence, CERN does not waive the privileges and immunities *
23
+ * granted to it by virtue of its status as an Intergovernmental Organization *
24
+ * or submit itself to any jurisdiction. *
25
+ \*****************************************************************************/
26
+ class LokiForm extends _react.default.Component {
27
+ constructor() {
28
+ super(...arguments);
29
+ _defineProperty(this, "state", {
30
+ key: this.props.key,
31
+ value: this.props.value,
32
+ err: false,
33
+ errMsg: ""
34
+ });
35
+ _defineProperty(this, "keyCallback", event => {
36
+ const key = event.target.value.replaceAll(/[^\w]/g, "");
37
+ if (!this.props.validateKey(key)) {
38
+ this.setState({
39
+ err: true,
40
+ errMsg: "Key must be unique and not empty!"
41
+ });
42
+ } else {
43
+ this.setState({
44
+ err: false,
45
+ errMsg: ""
46
+ });
47
+ }
48
+ this.setState({
49
+ key: key
50
+ });
51
+ this.props.keyCallback(key);
52
+ });
53
+ _defineProperty(this, "valueCallback", value => {
54
+ this.setState({
55
+ value: value
56
+ });
57
+ this.props.valueCallback(value);
58
+ });
59
+ }
60
+ render() {
61
+ const errorMessage = this.state.err ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.Alert, {
62
+ variant: "danger",
63
+ children: this.state.errMsg
64
+ }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {});
65
+ const margin = {
66
+ "margin-left": "7px"
67
+ };
68
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.InputGroup, {
69
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Grid.default, {
70
+ container: true,
71
+ spacing: 1,
72
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Grid.default, {
73
+ className: "d-flex",
74
+ item: true,
75
+ form: "maincomponent",
76
+ xs: true,
77
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
78
+ children: [errorMessage, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactBootstrap.FormControl, {
79
+ placeholder: "Varibale Name",
80
+ onChange: this.keyCallback,
81
+ value: this.state.key
82
+ })]
83
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
84
+ style: margin,
85
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_LokiEditor.default, {
86
+ callback: this.valueCallback,
87
+ value: this.state.value,
88
+ lokiVars: this.props.lokiVars,
89
+ oneLine: true,
90
+ height: "24px",
91
+ width: "10dvw"
92
+ })
93
+ })]
94
+ })
95
+ })
96
+ });
97
+ }
98
+ }
99
+ LokiForm.defaultProps = {
100
+ key: "",
101
+ value: "",
102
+ validateKey: () => {
103
+ true;
104
+ },
105
+ lokiActive: true
106
+ };
107
+ LokiForm.propTypes = {
108
+ key: _propTypes.default.string,
109
+ value: _propTypes.default.string,
110
+ keyCallback: _propTypes.default.func,
111
+ valueCallback: _propTypes.default.func,
112
+ validateKey: _propTypes.default.func,
113
+ lokiVars: _propTypes.default.object,
114
+ lokiActive: _propTypes.default.bool
115
+ };
116
+ var _default = exports.default = LokiForm;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.embedCorpus = embedCorpus;
7
+ exports.getSimilarity = getSimilarity;
8
+ exports.loadModel = loadModel;
9
+ var _transformers = require("@xenova/transformers");
10
+ /*****************************************************************************\
11
+ * (c) Copyright 2024 CERN for the benefit of the LHCb Collaboration *
12
+ * *
13
+ * This software is distributed under the terms of the GNU General Public *
14
+ * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
15
+ * *
16
+ * In applying this licence, CERN does not waive the privileges and immunities *
17
+ * granted to it by virtue of its status as an Intergovernmental Organization *
18
+ * or submit itself to any jurisdiction. *
19
+ \*****************************************************************************/
20
+
21
+ // To always download the model from huggingface.com
22
+ _transformers.env.allowLocalModels = false;
23
+ _transformers.env.useBrowserCache = false;
24
+ // Create a new worker
25
+ const worker = new Worker(new URL("./worker", import.meta.url), {
26
+ type: "module"
27
+ });
28
+ /**
29
+ * @type {function}
30
+ * Promise resolve function for loading the model
31
+ */
32
+ let loadResolve;
33
+ /**
34
+ * @type {function}
35
+ */
36
+ let queryResolve;
37
+ worker.onmessage = function (event) {
38
+ const message = event.data;
39
+ switch (message.type) {
40
+ case "progress":
41
+ if (message.progress.status === "ready") {
42
+ loadResolve();
43
+ }
44
+ break;
45
+ case "corpus":
46
+ // the corpus is embedded
47
+ queryResolve();
48
+ break;
49
+ case "result":
50
+ queryResolve(message.result);
51
+ break;
52
+ }
53
+ };
54
+ /**
55
+ *
56
+ * @param {string} modelname
57
+ * Load the model with the provided model name
58
+ * @returns
59
+ */
60
+ async function loadModel(modelname) {
61
+ worker.postMessage({
62
+ type: "init",
63
+ model: modelname
64
+ });
65
+ return new Promise(resolve => {
66
+ loadResolve = resolve;
67
+ });
68
+ }
69
+ /**
70
+ * Passes corpus embedding from backend to the worker
71
+ * @returns
72
+ */
73
+ async function embedCorpus(metadata) {
74
+ worker.postMessage({
75
+ type: "corpus",
76
+ kgdoc: metadata.metadata.kgdoc,
77
+ emb: metadata.metadata.embedding
78
+ });
79
+ return new Promise(resolve => {
80
+ queryResolve = resolve;
81
+ });
82
+ }
83
+ /**
84
+ *
85
+ * @param {string} query#
86
+ * Calculate the similarity between the query and the corpus
87
+ * @returns
88
+ */
89
+ async function getSimilarity(query) {
90
+ worker.postMessage({
91
+ type: "similarity",
92
+ query: query
93
+ });
94
+ return new Promise(resolve => {
95
+ queryResolve = resolve;
96
+ });
97
+ }