react-cheminfo 0.130.0 → 0.132.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/README.md +29 -0
  2. package/lib/core.d.ts +1 -0
  3. package/lib/core.d.ts.map +1 -1
  4. package/lib/core.js +1 -0
  5. package/lib/core.js.map +1 -1
  6. package/lib/drawing/render/index.d.ts +3 -1
  7. package/lib/drawing/render/index.d.ts.map +1 -1
  8. package/lib/drawing/render/index.js +1 -0
  9. package/lib/drawing/render/index.js.map +1 -1
  10. package/lib/drawing/render/motion.d.ts +3 -2
  11. package/lib/drawing/render/motion.d.ts.map +1 -1
  12. package/lib/drawing/render/motion.js +25 -3
  13. package/lib/drawing/render/motion.js.map +1 -1
  14. package/lib/drawing/render/primitives.d.ts +10 -0
  15. package/lib/drawing/render/primitives.d.ts.map +1 -1
  16. package/lib/drawing/render/spinning.d.ts +29 -0
  17. package/lib/drawing/render/spinning.d.ts.map +1 -0
  18. package/lib/drawing/render/spinning.js +38 -0
  19. package/lib/drawing/render/spinning.js.map +1 -0
  20. package/lib/drawing/render/svg.d.ts.map +1 -1
  21. package/lib/drawing/render/svg.js +8 -2
  22. package/lib/drawing/render/svg.js.map +1 -1
  23. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  24. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  25. package/lib/locales/de.json +4 -0
  26. package/lib/locales/en.json +4 -0
  27. package/lib/locales/es.json +4 -0
  28. package/lib/locales/fr.json +4 -0
  29. package/lib/locales/it.json +4 -0
  30. package/lib/log/core/index.d.ts +4 -0
  31. package/lib/log/core/index.d.ts.map +1 -0
  32. package/lib/log/core/index.js +3 -0
  33. package/lib/log/core/index.js.map +1 -0
  34. package/lib/log/core/logGroups.d.ts +87 -0
  35. package/lib/log/core/logGroups.d.ts.map +1 -0
  36. package/lib/log/core/logGroups.js +85 -0
  37. package/lib/log/core/logGroups.js.map +1 -0
  38. package/lib/log/core/logLines.d.ts +19 -0
  39. package/lib/log/core/logLines.d.ts.map +1 -0
  40. package/lib/log/core/logLines.js +36 -0
  41. package/lib/log/core/logLines.js.map +1 -0
  42. package/lib/log/ui/LogBadge.d.ts +39 -0
  43. package/lib/log/ui/LogBadge.d.ts.map +1 -0
  44. package/lib/log/ui/LogBadge.js +28 -0
  45. package/lib/log/ui/LogBadge.js.map +1 -0
  46. package/lib/log/ui/LogCallouts.d.ts +55 -0
  47. package/lib/log/ui/LogCallouts.d.ts.map +1 -0
  48. package/lib/log/ui/LogCallouts.js +38 -0
  49. package/lib/log/ui/LogCallouts.js.map +1 -0
  50. package/lib/log/ui/index.d.ts +7 -0
  51. package/lib/log/ui/index.d.ts.map +1 -0
  52. package/lib/log/ui/index.js +5 -0
  53. package/lib/log/ui/index.js.map +1 -0
  54. package/lib/log/ui/logIcon.d.ts +9 -0
  55. package/lib/log/ui/logIcon.d.ts.map +1 -0
  56. package/lib/log/ui/logIcon.js +17 -0
  57. package/lib/log/ui/logIcon.js.map +1 -0
  58. package/lib/log/ui/useLogEntries.d.ts +15 -0
  59. package/lib/log/ui/useLogEntries.d.ts.map +1 -0
  60. package/lib/log/ui/useLogEntries.js +47 -0
  61. package/lib/log/ui/useLogEntries.js.map +1 -0
  62. package/lib/overlay/ui/OverlayBarButton.d.ts.map +1 -1
  63. package/lib/overlay/ui/OverlayBarButton.js +1 -1
  64. package/lib/overlay/ui/OverlayBarButton.js.map +1 -1
  65. package/lib/overlay/ui/OverlayInfo.d.ts.map +1 -1
  66. package/lib/overlay/ui/OverlayInfo.js +1 -1
  67. package/lib/overlay/ui/OverlayInfo.js.map +1 -1
  68. package/lib/structure/core/conformerMinimise.d.ts +13 -4
  69. package/lib/structure/core/conformerMinimise.d.ts.map +1 -1
  70. package/lib/structure/core/conformerMinimise.js +23 -6
  71. package/lib/structure/core/conformerMinimise.js.map +1 -1
  72. package/lib/structure/core/conformerRefine.d.ts +2 -2
  73. package/lib/structure/core/conformerRefine.d.ts.map +1 -1
  74. package/lib/structure/core/conformerRefine.js +23 -7
  75. package/lib/structure/core/conformerRefine.js.map +1 -1
  76. package/lib/structure/core/conformers.d.ts +8 -3
  77. package/lib/structure/core/conformers.d.ts.map +1 -1
  78. package/lib/structure/core/conformers.js +10 -7
  79. package/lib/structure/core/conformers.js.map +1 -1
  80. package/lib/structure/core/geometryRelaxer.d.ts +10 -3
  81. package/lib/structure/core/geometryRelaxer.d.ts.map +1 -1
  82. package/lib/structure/core/relaxerLogLines.d.ts +16 -0
  83. package/lib/structure/core/relaxerLogLines.d.ts.map +1 -0
  84. package/lib/structure/core/relaxerLogLines.js +33 -0
  85. package/lib/structure/core/relaxerLogLines.js.map +1 -0
  86. package/lib/ui.d.ts +1 -0
  87. package/lib/ui.d.ts.map +1 -1
  88. package/lib/ui.js +1 -0
  89. package/lib/ui.js.map +1 -1
  90. package/lib/xtb/core/taggedLogger.d.ts +11 -0
  91. package/lib/xtb/core/taggedLogger.d.ts.map +1 -0
  92. package/lib/xtb/core/taggedLogger.js +31 -0
  93. package/lib/xtb/core/taggedLogger.js.map +1 -0
  94. package/lib/xtb/core/xtbRelaxer.d.ts.map +1 -1
  95. package/lib/xtb/core/xtbRelaxer.js +13 -5
  96. package/lib/xtb/core/xtbRelaxer.js.map +1 -1
  97. package/package.json +4 -3
  98. package/src/core.ts +1 -0
  99. package/src/drawing/render/index.ts +3 -0
  100. package/src/drawing/render/motion.ts +25 -3
  101. package/src/drawing/render/primitives.ts +11 -0
  102. package/src/drawing/render/spinning.ts +48 -0
  103. package/src/drawing/render/svg.ts +8 -2
  104. package/src/locales/de.json +4 -0
  105. package/src/locales/en.json +4 -0
  106. package/src/locales/es.json +4 -0
  107. package/src/locales/fr.json +4 -0
  108. package/src/locales/it.json +4 -0
  109. package/src/log/core/index.ts +9 -0
  110. package/src/log/core/logGroups.ts +159 -0
  111. package/src/log/core/logLines.ts +44 -0
  112. package/src/log/ui/LogBadge.tsx +89 -0
  113. package/src/log/ui/LogCallouts.tsx +103 -0
  114. package/src/log/ui/index.ts +6 -0
  115. package/src/log/ui/logIcon.ts +21 -0
  116. package/src/log/ui/useLogEntries.ts +57 -0
  117. package/src/overlay/ui/OverlayBarButton.tsx +1 -0
  118. package/src/overlay/ui/OverlayInfo.tsx +1 -0
  119. package/src/structure/core/conformerMinimise.ts +34 -8
  120. package/src/structure/core/conformerRefine.ts +29 -13
  121. package/src/structure/core/conformers.ts +23 -12
  122. package/src/structure/core/geometryRelaxer.ts +11 -3
  123. package/src/structure/core/relaxerLogLines.ts +39 -0
  124. package/src/ui.ts +1 -0
  125. package/src/xtb/core/taggedLogger.ts +35 -0
  126. package/src/xtb/core/xtbRelaxer.ts +15 -5
  127. package/styles/chrome.css +34 -0
@@ -0,0 +1,159 @@
1
+ /** A level a log entry carries, as fifo-logger and pino label it. */
2
+ export type LogLevelLabel =
3
+ 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
4
+
5
+ /** How a message reads on the page, in Blueprint's intents. */
6
+ export type LogIntent = 'danger' | 'warning' | 'primary' | 'none';
7
+
8
+ /**
9
+ * One logged message, as the page reads it. A fifo-logger `LogEntry` is one;
10
+ * so is a plain object a site writes for a caveat that is always true, or one
11
+ * read back from a saved file.
12
+ *
13
+ * Two keys of `meta` are shown: a string `title` heads the message, and a
14
+ * string `source` — a file, a line, the third conformer — labels where it
15
+ * comes from, so the message itself never has to name it.
16
+ */
17
+ export interface LogLine {
18
+ /** The level it was logged at; `silent` entries are never shown. */
19
+ levelLabel: LogLevelLabel | 'silent';
20
+ /** The message. */
21
+ message: string;
22
+ /**
23
+ * What was logged with it.
24
+ * @default undefined
25
+ */
26
+ meta?: Record<string, unknown>;
27
+ /**
28
+ * The error that was logged, whose own message is shown.
29
+ * @default undefined
30
+ */
31
+ error?: Error;
32
+ }
33
+
34
+ /** One message as the page shows it: every entry that said it, once. */
35
+ export interface LogGroup {
36
+ /** The same for the same level, source, title and text. */
37
+ key: string;
38
+ /** The level it was logged at. */
39
+ level: LogLevelLabel;
40
+ /**
41
+ * Where it comes from, from a string `source` in the meta.
42
+ * @default undefined
43
+ */
44
+ source?: string;
45
+ /**
46
+ * A heading for the message, from a string `title` in the meta.
47
+ * @default undefined
48
+ */
49
+ title?: string;
50
+ /** The message, or the logged error's own message. */
51
+ text: string;
52
+ /** How many entries said it. */
53
+ count: number;
54
+ }
55
+
56
+ /** Which entries {@link groupLogEntries} keeps. */
57
+ export interface LogGroupOptions {
58
+ /**
59
+ * The least serious level shown. Debug and trace are for a developer, so
60
+ * they stay out of the page unless asked for.
61
+ * @default 'info'
62
+ */
63
+ minLevel?: LogLevelLabel;
64
+ }
65
+
66
+ /**
67
+ * The messages a page shows for some log entries: the same message said twice
68
+ * once, with its count, and the most serious first. Within one level the
69
+ * messages keep the order they were first said in, which is the order the
70
+ * work happened in.
71
+ *
72
+ * Entries from several loggers can be passed together, so what a run reported
73
+ * and what a model always says about its results read as one list.
74
+ * @param lines - What one or more loggers collected.
75
+ * @param options - See {@link LogGroupOptions}.
76
+ * @returns The messages to show, worst first.
77
+ */
78
+ export function groupLogEntries(
79
+ lines: readonly LogLine[],
80
+ options: LogGroupOptions = {},
81
+ ): LogGroup[] {
82
+ const minimum = SEVERITY[options.minLevel ?? 'info'];
83
+ const groups = new Map<string, LogGroup>();
84
+ for (const line of lines) {
85
+ const level = line.levelLabel;
86
+ if (level === 'silent' || SEVERITY[level] < minimum) continue;
87
+ const source = metaString(line, 'source');
88
+ const title = metaString(line, 'title');
89
+ const text = line.error?.message ?? line.message;
90
+ const key = [level, source ?? '', title ?? '', text].join('\u0000');
91
+ const group = groups.get(key);
92
+ if (group === undefined) {
93
+ groups.set(key, {
94
+ key,
95
+ level,
96
+ ...(source === undefined ? {} : { source }),
97
+ ...(title === undefined ? {} : { title }),
98
+ text,
99
+ count: 1,
100
+ });
101
+ } else {
102
+ group.count++;
103
+ }
104
+ }
105
+ return [...groups.values()].toSorted(
106
+ (first, second) => SEVERITY[second.level] - SEVERITY[first.level],
107
+ );
108
+ }
109
+
110
+ /**
111
+ * The most serious level among some messages: the one a single indicator
112
+ * standing for all of them is drawn in.
113
+ * @param groups - Messages from {@link groupLogEntries}.
114
+ * @returns The worst level, or `null` when there is no message.
115
+ */
116
+ export function worstLogLevel(
117
+ groups: readonly LogGroup[],
118
+ ): LogLevelLabel | null {
119
+ let worst: LogLevelLabel | null = null;
120
+ for (const group of groups) {
121
+ if (worst === null || SEVERITY[group.level] > SEVERITY[worst]) {
122
+ worst = group.level;
123
+ }
124
+ }
125
+ return worst;
126
+ }
127
+
128
+ /**
129
+ * The intent a message of this level is drawn in: an error in red, a warning
130
+ * in amber, context in the primary blue.
131
+ * @param level - The level it was logged at.
132
+ * @returns A Blueprint intent.
133
+ */
134
+ export function logIntent(level: LogLevelLabel): LogIntent {
135
+ return INTENTS[level];
136
+ }
137
+
138
+ const SEVERITY: Record<LogLevelLabel, number> = {
139
+ trace: 10,
140
+ debug: 20,
141
+ info: 30,
142
+ warn: 40,
143
+ error: 50,
144
+ fatal: 60,
145
+ };
146
+
147
+ const INTENTS: Record<LogLevelLabel, LogIntent> = {
148
+ trace: 'none',
149
+ debug: 'none',
150
+ info: 'primary',
151
+ warn: 'warning',
152
+ error: 'danger',
153
+ fatal: 'danger',
154
+ };
155
+
156
+ function metaString(line: LogLine, key: string): string | undefined {
157
+ const value = line.meta?.[key];
158
+ return typeof value === 'string' && value !== '' ? value : undefined;
159
+ }
@@ -0,0 +1,44 @@
1
+ import type { LogGroupOptions, LogLine } from './logGroups.ts';
2
+ import { groupLogEntries } from './logGroups.ts';
3
+
4
+ /**
5
+ * Log entries as plain data, to keep with a result, save in a file or post
6
+ * across a worker: a logged error becomes its message, and nothing else of a
7
+ * fifo-logger entry — its id, time and logger uuids — is kept.
8
+ * @param lines - What a logger collected.
9
+ * @returns The same messages, as plain objects in the same order.
10
+ */
11
+ export function plainLogLines(lines: readonly LogLine[]): LogLine[] {
12
+ const plain: LogLine[] = [];
13
+ for (const line of lines) {
14
+ plain.push({
15
+ levelLabel: line.levelLabel,
16
+ message: line.error?.message ?? line.message,
17
+ ...(line.meta === undefined ? {} : { meta: line.meta }),
18
+ });
19
+ }
20
+ return plain;
21
+ }
22
+
23
+ /**
24
+ * The messages as sentences, for an answer that carries text rather than a
25
+ * page: an MCP tool, a host's reply, a status line. Each message comes once,
26
+ * the worst first, prefixed by where it comes from and led by its title.
27
+ * @param lines - What one or more loggers collected.
28
+ * @param options - See {@link LogGroupOptions}.
29
+ * @returns One sentence per distinct message.
30
+ */
31
+ export function logMessages(
32
+ lines: readonly LogLine[],
33
+ options: LogGroupOptions = {},
34
+ ): string[] {
35
+ const messages: string[] = [];
36
+ for (const group of groupLogEntries(lines, options)) {
37
+ const text =
38
+ group.title === undefined ? group.text : `${group.title}. ${group.text}`;
39
+ messages.push(
40
+ group.source === undefined ? text : `${group.source}: ${text}`,
41
+ );
42
+ }
43
+ return messages;
44
+ }
@@ -0,0 +1,89 @@
1
+ import type { PopoverNextProps } from '@blueprintjs/core';
2
+ import { Button, PopoverNext } from '@blueprintjs/core';
3
+ import type { ReactElement } from 'react';
4
+
5
+ import { useChromeT } from '../../i18n/ui/useT.ts';
6
+ import { joinClassNames } from '../../shared/ui/joinClassNames.ts';
7
+ import type { LogLevelLabel, LogLine } from '../core/logGroups.ts';
8
+ import {
9
+ groupLogEntries,
10
+ logIntent,
11
+ worstLogLevel,
12
+ } from '../core/logGroups.ts';
13
+
14
+ import { LogGroupList } from './LogCallouts.tsx';
15
+ import { logIcon } from './logIcon.ts';
16
+
17
+ /** Props of {@link LogBadge}. */
18
+ export interface LogBadgeProps {
19
+ /** What one or more loggers collected, in the order they logged it. */
20
+ entries: readonly LogLine[];
21
+ /**
22
+ * The least serious level shown.
23
+ * @default 'info'
24
+ */
25
+ minLevel?: LogLevelLabel;
26
+ /**
27
+ * How many messages the popover shows before the rest are counted instead.
28
+ * @default every message
29
+ */
30
+ limit?: number;
31
+ /**
32
+ * Which side the messages open on.
33
+ * @default 'bottom-start'
34
+ */
35
+ placement?: PopoverNextProps['placement'];
36
+ /**
37
+ * Class names added to the button.
38
+ * @default undefined
39
+ */
40
+ className?: string;
41
+ }
42
+
43
+ /**
44
+ * One small button standing for every message a logger collected about what
45
+ * sits beside it, drawn in the colour of the most serious and counting them,
46
+ * which opens them all as callouts on hover. The messages stay one pointer
47
+ * away from the chart or the result they qualify instead of pushing it down
48
+ * the page.
49
+ * @param props - See {@link LogBadgeProps}.
50
+ * @returns The button, or nothing when there is no message to show.
51
+ */
52
+ export function LogBadge(props: LogBadgeProps): ReactElement | null {
53
+ const {
54
+ entries,
55
+ minLevel,
56
+ limit,
57
+ placement = 'bottom-start',
58
+ className,
59
+ } = props;
60
+ const t = useChromeT();
61
+ const groups = groupLogEntries(entries, { minLevel });
62
+ const worst = worstLogLevel(groups);
63
+ if (worst === null) return null;
64
+
65
+ return (
66
+ <PopoverNext
67
+ interactionKind="hover"
68
+ hoverOpenDelay={150}
69
+ placement={placement}
70
+ popoverClassName="log-badge__popover"
71
+ content={<LogGroupList groups={groups} limit={limit} />}
72
+ >
73
+ <Button
74
+ variant="minimal"
75
+ size="small"
76
+ intent={logIntent(worst)}
77
+ icon={logIcon(worst)}
78
+ className={joinClassNames('log-badge', 'no-print', className)}
79
+ aria-label={
80
+ groups.length === 1
81
+ ? t('log.badge.one')
82
+ : t('log.badge.other', { count: groups.length })
83
+ }
84
+ >
85
+ {groups.length > 1 ? groups.length : undefined}
86
+ </Button>
87
+ </PopoverNext>
88
+ );
89
+ }
@@ -0,0 +1,103 @@
1
+ import { Callout } from '@blueprintjs/core';
2
+ import type { ReactElement } from 'react';
3
+
4
+ import { useChromeT } from '../../i18n/ui/useT.ts';
5
+ import { InlineText } from '../../pedagogy/ui/InlineText.tsx';
6
+ import { joinClassNames } from '../../shared/ui/joinClassNames.ts';
7
+ import type { LogGroup, LogLevelLabel, LogLine } from '../core/logGroups.ts';
8
+ import { groupLogEntries, logIntent } from '../core/logGroups.ts';
9
+
10
+ import { logIcon } from './logIcon.ts';
11
+
12
+ /** Props of {@link LogCallouts}. */
13
+ export interface LogCalloutsProps {
14
+ /** What one or more loggers collected, in the order they logged it. */
15
+ entries: readonly LogLine[];
16
+ /**
17
+ * The least serious level shown.
18
+ * @default 'info'
19
+ */
20
+ minLevel?: LogLevelLabel;
21
+ /**
22
+ * How many messages are shown before the rest are counted instead.
23
+ * @default every message
24
+ */
25
+ limit?: number;
26
+ /**
27
+ * Class names added to the list.
28
+ * @default undefined
29
+ */
30
+ className?: string;
31
+ }
32
+
33
+ /**
34
+ * What a logger collected, as callouts in the page: one per distinct message,
35
+ * the most serious first, each in the colour and with the icon of its level.
36
+ *
37
+ * A message is authored prose — `` `code` ``, `{{CO2}}`, `**strong**`. A string
38
+ * `title` in its meta heads it and a string `source` labels where it comes
39
+ * from. A message said several times is shown once, with how many times.
40
+ * @param props - See {@link LogCalloutsProps}.
41
+ * @returns The callouts, or nothing when there is no message to show.
42
+ */
43
+ export function LogCallouts(props: LogCalloutsProps): ReactElement | null {
44
+ const { entries, minLevel, limit, className } = props;
45
+ const groups = groupLogEntries(entries, { minLevel });
46
+ if (groups.length === 0) return null;
47
+ return <LogGroupList groups={groups} limit={limit} className={className} />;
48
+ }
49
+
50
+ /** Props of {@link LogGroupList}. */
51
+ export interface LogGroupListProps {
52
+ /** The messages, as {@link groupLogEntries} ordered them. */
53
+ groups: readonly LogGroup[];
54
+ /**
55
+ * How many messages are shown before the rest are counted instead.
56
+ * @default every message
57
+ */
58
+ limit?: number;
59
+ /**
60
+ * Class names added to the list.
61
+ * @default undefined
62
+ */
63
+ className?: string;
64
+ }
65
+
66
+ /**
67
+ * Messages already grouped, as callouts.
68
+ * @param props - See {@link LogGroupListProps}.
69
+ * @returns The callouts.
70
+ */
71
+ export function LogGroupList(props: LogGroupListProps): ReactElement {
72
+ const { groups, limit = groups.length, className } = props;
73
+ const t = useChromeT();
74
+ const hidden = Math.max(0, groups.length - limit);
75
+ return (
76
+ <div className={joinClassNames('log-callouts', className)}>
77
+ {groups.slice(0, limit).map((group) => (
78
+ <Callout
79
+ key={group.key}
80
+ compact
81
+ intent={logIntent(group.level)}
82
+ icon={logIcon(group.level)}
83
+ title={group.title}
84
+ >
85
+ {group.source !== undefined && (
86
+ <span className="log-callouts__source">{group.source}</span>
87
+ )}
88
+ <InlineText text={group.text} />
89
+ {group.count > 1 && (
90
+ <span className="log-callouts__count">×{group.count}</span>
91
+ )}
92
+ </Callout>
93
+ ))}
94
+ {hidden > 0 && (
95
+ <span className="log-callouts__more">
96
+ {hidden === 1
97
+ ? t('log.more.one')
98
+ : t('log.more.other', { count: hidden })}
99
+ </span>
100
+ )}
101
+ </div>
102
+ );
103
+ }
@@ -0,0 +1,6 @@
1
+ export type { LogBadgeProps } from './LogBadge.tsx';
2
+ export { LogBadge } from './LogBadge.tsx';
3
+ export type { LogCalloutsProps, LogGroupListProps } from './LogCallouts.tsx';
4
+ export { LogCallouts, LogGroupList } from './LogCallouts.tsx';
5
+ export { logIcon } from './logIcon.ts';
6
+ export { useLogEntries } from './useLogEntries.ts';
@@ -0,0 +1,21 @@
1
+ import type { IconName } from '@blueprintjs/core';
2
+
3
+ import type { LogLevelLabel } from '../core/logGroups.ts';
4
+
5
+ /**
6
+ * The icon a message of this level is shown with.
7
+ * @param level - The level it was logged at.
8
+ * @returns A Blueprint icon name.
9
+ */
10
+ export function logIcon(level: LogLevelLabel): IconName {
11
+ return ICONS[level];
12
+ }
13
+
14
+ const ICONS: Record<LogLevelLabel, IconName> = {
15
+ trace: 'console',
16
+ debug: 'console',
17
+ info: 'info-sign',
18
+ warn: 'warning-sign',
19
+ error: 'error',
20
+ fatal: 'error',
21
+ };
@@ -0,0 +1,57 @@
1
+ import type { FifoLogger, LogEntry } from 'fifo-logger';
2
+ import { useCallback, useSyncExternalStore } from 'react';
3
+
4
+ /**
5
+ * What a logger holds, kept up to date as it logs: a page shows the messages
6
+ * of a long run while it is still running, and the list redraws only when
7
+ * something was logged.
8
+ *
9
+ * The logger is named, never read from a context: each result keeps a logger
10
+ * of its own, so a new run replaces its own messages and leaves the others'.
11
+ * react-science's `useFifoLogs` is for the one page-wide logger its
12
+ * `FifoLoggerProvider` holds.
13
+ * @param logger - The logger to read, or nothing while there is none.
14
+ * @returns Its entries, the same array until it logs again.
15
+ */
16
+ export function useLogEntries(
17
+ logger: FifoLogger | null | undefined,
18
+ ): readonly LogEntry[] {
19
+ const subscribe = useCallback(
20
+ (onChange: () => void) => {
21
+ if (logger == null) return noSubscription;
22
+ const listener = () => {
23
+ snapshots.delete(logger);
24
+ onChange();
25
+ };
26
+ logger.addEventListener('change', listener);
27
+ return () => {
28
+ logger.removeEventListener('change', listener);
29
+ };
30
+ },
31
+ [logger],
32
+ );
33
+ const read = useCallback(() => snapshotOf(logger), [logger]);
34
+ return useSyncExternalStore(subscribe, read, read);
35
+ }
36
+
37
+ // One array per logger until it logs again: `getLogs` builds a new one on every
38
+ // call, and a store has to answer the same value while nothing changed.
39
+ const snapshots = new WeakMap<FifoLogger, readonly LogEntry[]>();
40
+
41
+ const NO_ENTRIES: readonly LogEntry[] = [];
42
+
43
+ function snapshotOf(
44
+ logger: FifoLogger | null | undefined,
45
+ ): readonly LogEntry[] {
46
+ if (logger == null) return NO_ENTRIES;
47
+ let entries = snapshots.get(logger);
48
+ if (entries === undefined) {
49
+ entries = logger.getLogs({ includeChildren: true });
50
+ snapshots.set(logger, entries);
51
+ }
52
+ return entries;
53
+ }
54
+
55
+ function noSubscription(): void {
56
+ // Nothing was subscribed to.
57
+ }
@@ -51,6 +51,7 @@ export function OverlayBarButton(props: OverlayBarButtonProps): ReactElement {
51
51
 
52
52
  return (
53
53
  <PopoverNext
54
+ isOpen={open}
54
55
  placement={POPOVER_PLACEMENTS[placement]}
55
56
  onInteraction={(next) => setOpen(next)}
56
57
  content={
@@ -59,6 +59,7 @@ export function OverlayInfo(props: OverlayInfoProps): ReactElement {
59
59
 
60
60
  return (
61
61
  <PopoverNext
62
+ isOpen={open}
62
63
  placement={placement}
63
64
  onInteraction={(next) => setOpen(next)}
64
65
  content={<div style={explanationStyle(metrics)}>{children}</div>}
@@ -2,6 +2,7 @@ import type { Molecule } from 'openchemlib';
2
2
  import { ForceFieldMMFF94 } from 'openchemlib';
3
3
 
4
4
  import { ordinal } from '../../format/core/words.ts';
5
+ import type { LogLine } from '../../log/core/logGroups.ts';
5
6
 
6
7
  import type { ConformerOptions } from './conformerOptions.ts';
7
8
 
@@ -13,19 +14,20 @@ import type { ConformerOptions } from './conformerOptions.ts';
13
14
  * than an exception. A non-zero return code from `minimise()` is *not* a
14
15
  * failure: ethyne returns 2 with a perfect geometry.
15
16
  *
16
- * Warnings count the conformers the generator handed back, kept or not, so one
17
- * number never means two things — a row number would, once one is dropped.
17
+ * A warning names its conformer by the count of conformers the generator
18
+ * handed back, kept or not, so one number never means two things — a row
19
+ * number would, once one is dropped.
18
20
  * @param conformer - The conformer to relax. Mutated.
19
21
  * @param options - Force field and iteration budget.
20
22
  * @param produced - 1-based position of the conformer in the generator's output.
21
- * @param warnings - Receives one sentence when the energy is unknown.
23
+ * @param logs - Receives one warning when the energy is unknown.
22
24
  * @returns Total energy in kcal/mol, or `null` when none was computed.
23
25
  */
24
26
  export function minimiseConformer(
25
27
  conformer: Molecule,
26
28
  options: ConformerOptions,
27
29
  produced: number,
28
- warnings: string[],
30
+ logs: LogLine[],
29
31
  ): number | null {
30
32
  const algorithm = options.minimisation;
31
33
  if (algorithm === 'none') return null;
@@ -35,20 +37,44 @@ export function minimiseConformer(
35
37
  forceField.minimise({ maxIts: options.maxIterations });
36
38
  energy = forceField.getTotalEnergy();
37
39
  } catch (error) {
38
- warnings.push(
39
- `The ${ordinal(produced)} conformer produced could not be minimised with ${algorithm} (${readable(error)}), so its energy is unknown.`,
40
+ logs.push(
41
+ producedWarning(
42
+ produced,
43
+ `It could not be minimised with ${algorithm} (${readable(error)}), so its energy is unknown.`,
44
+ ),
40
45
  );
41
46
  return null;
42
47
  }
43
48
  if (!Number.isFinite(energy)) {
44
- warnings.push(
45
- `The ${ordinal(produced)} conformer produced minimised to a non-finite ${algorithm} energy, so its energy is unknown.`,
49
+ logs.push(
50
+ producedWarning(
51
+ produced,
52
+ `It minimised to a non-finite ${algorithm} energy, so its energy is unknown.`,
53
+ ),
46
54
  );
47
55
  return null;
48
56
  }
49
57
  return energy;
50
58
  }
51
59
 
60
+ /**
61
+ * The label of a conformer as the generator handed it back, which a message
62
+ * about it is shown under.
63
+ * @param produced - 1-based position of the conformer in the generator's output.
64
+ * @returns E.g. `3rd conformer produced`.
65
+ */
66
+ export function producedSource(produced: number): string {
67
+ return `${ordinal(produced)} conformer produced`;
68
+ }
69
+
70
+ function producedWarning(produced: number, message: string): LogLine {
71
+ return {
72
+ levelLabel: 'warn',
73
+ message,
74
+ meta: { source: producedSource(produced) },
75
+ };
76
+ }
77
+
52
78
  /** What {@link rankByEnergy} reads and rewrites on a conformer. */
53
79
  export interface RankedConformer {
54
80
  id: number;
@@ -1,6 +1,8 @@
1
+ import { FifoLogger } from 'fifo-logger';
1
2
  import { Molecule } from 'openchemlib';
2
3
 
3
4
  import { ordinal } from '../../format/core/words.ts';
5
+ import type { LogLine } from '../../log/core/logGroups.ts';
4
6
 
5
7
  import type { RefinedMinimum } from './conformerRefineRank.ts';
6
8
  import {
@@ -20,6 +22,7 @@ import {
20
22
  writeRelaxedCoordinates,
21
23
  } from './moleculeCoordinates.ts';
22
24
  import { toMolfileExport } from './molfileExport.ts';
25
+ import { relaxerLogLines } from './relaxerLogLines.ts';
23
26
 
24
27
  /** What refining one conformer produced, on top of its force-field numbers. */
25
28
  export interface ConformerRefinement {
@@ -70,8 +73,11 @@ export interface ConformerSetRefinement {
70
73
  reordered: number;
71
74
  }
72
75
 
73
- /** How a refinement pass is tuned, watched and cancelled. */
74
- export interface RefineConformersOptions extends RelaxerOptions {
76
+ /** How a refinement is tuned and watched; what it logs is kept on the set. */
77
+ export interface RefineConformersOptions extends Omit<
78
+ RelaxerOptions,
79
+ 'logger'
80
+ > {
75
81
  /**
76
82
  * Name of the method, for the label the UI shows.
77
83
  * @default 'GFN2-xTB'
@@ -155,11 +161,13 @@ export async function refineConformers(
155
161
  });
156
162
  }
157
163
 
164
+ const relaxerLog = new FifoLogger({ level: 'info' });
158
165
  const relaxed = await relax(
159
166
  prepared.map((entry) => entry.geometry),
160
167
  {
161
168
  onSettled: options.onSettled,
162
169
  signal: options.signal,
170
+ logger: relaxerLog,
163
171
  },
164
172
  );
165
173
  if (relaxed.length !== set.conformers.length) {
@@ -170,7 +178,9 @@ export async function refineConformers(
170
178
 
171
179
  const tolerance =
172
180
  options.sameEnergyTolerance ?? REFINED_SAME_ENERGY_TOLERANCE;
173
- const warnings = [...set.warnings];
181
+ const own: LogLine[] = [];
182
+ // Each structure's label, `null` for one merged away with its messages.
183
+ const sources = new Array<string | null>(prepared.length).fill(null);
174
184
  const kept: Conformer[] = [];
175
185
  const minima: RefinedMinimum[] = [];
176
186
  let merged = 0;
@@ -188,13 +198,14 @@ export async function refineConformers(
188
198
  merged++;
189
199
  continue;
190
200
  }
191
- for (const warning of result.warnings) {
192
- warnings.push(`The ${ordinal(conformer.id)} conformer: ${warning}`);
193
- }
201
+ const source = `${ordinal(conformer.id)} conformer`;
202
+ sources[index] = source;
194
203
  if (!result.converged) {
195
- warnings.push(
196
- `The ${ordinal(conformer.id)} conformer did not reach a ${method} minimum in ${result.cycles} cycles, so its refined energy is an upper bound.`,
197
- );
204
+ own.push({
205
+ levelLabel: 'warn',
206
+ message: `It did not reach a ${method} minimum in ${result.cycles} cycles, so its refined energy is an upper bound.`,
207
+ meta: { source },
208
+ });
198
209
  }
199
210
  kept.push({
200
211
  ...conformer,
@@ -214,15 +225,20 @@ export async function refineConformers(
214
225
  }
215
226
 
216
227
  if (merged > 0) {
217
- warnings.push(
218
- `${merged} conformer(s) relaxed into a ${method} minimum the set already held, so they were dropped.`,
219
- );
228
+ own.push({
229
+ levelLabel: 'info',
230
+ message: `${merged} conformer(s) relaxed into a ${method} minimum the set already held, so they were dropped.`,
231
+ });
220
232
  }
221
233
  const ranked = rankByRefinedEnergy(kept);
222
234
  return {
223
235
  ...set,
224
236
  conformers: ranked.conformers,
225
- warnings,
237
+ logs: [
238
+ ...set.logs,
239
+ ...relaxerLogLines(relaxerLog.getLogs(), sources),
240
+ ...own,
241
+ ],
226
242
  rankedBy: REFINED_RANKING,
227
243
  refinement: {
228
244
  method,