@orangecharts/wrun-chart-host 0.1.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 (198) hide show
  1. package/README.md +72 -0
  2. package/dist/constants/governed-exchanges.constants.d.ts +11 -0
  3. package/dist/constants/governed-exchanges.constants.js +11 -0
  4. package/dist/constants/indicator-sets.constants.d.ts +8 -0
  5. package/dist/constants/indicator-sets.constants.js +8 -0
  6. package/dist/constants/interval.constants.d.ts +34 -0
  7. package/dist/constants/interval.constants.js +121 -0
  8. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.d.ts +28 -0
  9. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.js +65 -0
  10. package/dist/constants/tpo-color-schemes.constants.d.ts +30 -0
  11. package/dist/constants/tpo-color-schemes.constants.js +32 -0
  12. package/dist/host-env.d.ts +102 -0
  13. package/dist/host-env.js +26 -0
  14. package/dist/index.d.ts +93 -0
  15. package/dist/index.js +96 -0
  16. package/dist/indicators/controls/tpo-letter.util.d.ts +2 -0
  17. package/dist/indicators/controls/tpo-letter.util.js +13 -0
  18. package/dist/indicators/controls/tpo-session.util.d.ts +99 -0
  19. package/dist/indicators/controls/tpo-session.util.js +389 -0
  20. package/dist/interfaces/kscript-diagnostics.interface.d.ts +64 -0
  21. package/dist/interfaces/kscript-diagnostics.interface.js +1 -0
  22. package/dist/interfaces/om-panel-data.interface.d.ts +103 -0
  23. package/dist/interfaces/om-panel-data.interface.js +7 -0
  24. package/dist/interfaces/wrun-runtime.interface.d.ts +2659 -0
  25. package/dist/interfaces/wrun-runtime.interface.js +55 -0
  26. package/dist/utility/chart/calendar-interval.util.d.ts +24 -0
  27. package/dist/utility/chart/calendar-interval.util.js +81 -0
  28. package/dist/utility/chart/candle-bar-end.util.d.ts +15 -0
  29. package/dist/utility/chart/candle-bar-end.util.js +23 -0
  30. package/dist/utility/chart/cme-realtime.util.d.ts +24 -0
  31. package/dist/utility/chart/cme-realtime.util.js +53 -0
  32. package/dist/utility/chart/interval-ladder.util.d.ts +27 -0
  33. package/dist/utility/chart/interval-ladder.util.js +92 -0
  34. package/dist/utility/chart/kscript-drawing-prep.util.d.ts +40 -0
  35. package/dist/utility/chart/kscript-drawing-prep.util.js +184 -0
  36. package/dist/utility/chart/kscript-drawing-safe-area.util.d.ts +95 -0
  37. package/dist/utility/chart/kscript-drawing-safe-area.util.js +805 -0
  38. package/dist/utility/chart/kscript-legend-entries.util.d.ts +166 -0
  39. package/dist/utility/chart/kscript-legend-entries.util.js +538 -0
  40. package/dist/utility/chart/kscript-output-role.util.d.ts +32 -0
  41. package/dist/utility/chart/kscript-output-role.util.js +215 -0
  42. package/dist/utility/chart/kscript-provenance-keys.util.d.ts +22 -0
  43. package/dist/utility/chart/kscript-provenance-keys.util.js +56 -0
  44. package/dist/utility/chart/kscript-run-result-normalize.util.d.ts +53 -0
  45. package/dist/utility/chart/kscript-run-result-normalize.util.js +476 -0
  46. package/dist/utility/chart/kscript-run-result-shape.util.d.ts +14 -0
  47. package/dist/utility/chart/kscript-run-result-shape.util.js +66 -0
  48. package/dist/utility/chart/kscript-titancharts-container.util.d.ts +29 -0
  49. package/dist/utility/chart/kscript-titancharts-container.util.js +151 -0
  50. package/dist/utility/chart/kscript-viewport-overlays.util.d.ts +83 -0
  51. package/dist/utility/chart/kscript-viewport-overlays.util.js +443 -0
  52. package/dist/utility/chart/om-panel-push-style.util.d.ts +78 -0
  53. package/dist/utility/chart/om-panel-push-style.util.js +301 -0
  54. package/dist/utility/chart/om-panel.util.d.ts +166 -0
  55. package/dist/utility/chart/om-panel.util.js +815 -0
  56. package/dist/utility/chart/om-series.util.d.ts +42 -0
  57. package/dist/utility/chart/om-series.util.js +330 -0
  58. package/dist/utility/chart/om-target.util.d.ts +11 -0
  59. package/dist/utility/chart/om-target.util.js +24 -0
  60. package/dist/utility/chart/om-widget.util.d.ts +148 -0
  61. package/dist/utility/chart/om-widget.util.js +413 -0
  62. package/dist/utility/chart/overlay-output-budget.util.d.ts +16 -0
  63. package/dist/utility/chart/overlay-output-budget.util.js +109 -0
  64. package/dist/utility/chart/overlay-paint-flags.util.d.ts +24 -0
  65. package/dist/utility/chart/overlay-paint-flags.util.js +55 -0
  66. package/dist/utility/chart/profile-row-height.util.d.ts +21 -0
  67. package/dist/utility/chart/profile-row-height.util.js +110 -0
  68. package/dist/utility/chart/session-interval.util.d.ts +1 -0
  69. package/dist/utility/chart/session-interval.util.js +35 -0
  70. package/dist/utility/chart/technical-script-legend-value.util.d.ts +18 -0
  71. package/dist/utility/chart/technical-script-legend-value.util.js +53 -0
  72. package/dist/utility/chart/trading-day.util.d.ts +70 -0
  73. package/dist/utility/chart/trading-day.util.js +238 -0
  74. package/dist/utility/chart/wrun-legend-name.util.d.ts +25 -0
  75. package/dist/utility/chart/wrun-legend-name.util.js +87 -0
  76. package/dist/utility/chart/wrun-run-result.util.d.ts +977 -0
  77. package/dist/utility/chart/wrun-run-result.util.js +5179 -0
  78. package/dist/utility/chart/wrun-visibility-chart.util.d.ts +25 -0
  79. package/dist/utility/chart/wrun-visibility-chart.util.js +22 -0
  80. package/dist/utility/indicators/heatmap-sensitivity.util.d.ts +62 -0
  81. package/dist/utility/indicators/heatmap-sensitivity.util.js +82 -0
  82. package/dist/utility/number-format.util.d.ts +68 -0
  83. package/dist/utility/number-format.util.js +188 -0
  84. package/dist/utility/v2/opra-governed.util.d.ts +1 -0
  85. package/dist/utility/v2/opra-governed.util.js +13 -0
  86. package/dist/utility/wrun/wrun-auto-row.util.d.ts +54 -0
  87. package/dist/utility/wrun/wrun-auto-row.util.js +96 -0
  88. package/dist/utility/wrun/wrun-batched-marks.util.d.ts +341 -0
  89. package/dist/utility/wrun/wrun-batched-marks.util.js +938 -0
  90. package/dist/utility/wrun/wrun-batched-readout.util.d.ts +75 -0
  91. package/dist/utility/wrun/wrun-batched-readout.util.js +171 -0
  92. package/dist/utility/wrun/wrun-box-segment.util.d.ts +185 -0
  93. package/dist/utility/wrun/wrun-box-segment.util.js +570 -0
  94. package/dist/utility/wrun/wrun-build-memo.util.d.ts +133 -0
  95. package/dist/utility/wrun/wrun-build-memo.util.js +301 -0
  96. package/dist/utility/wrun/wrun-builtin-twin-ports.util.d.ts +25 -0
  97. package/dist/utility/wrun/wrun-builtin-twin-ports.util.js +75 -0
  98. package/dist/utility/wrun/wrun-builtin-twin.util.d.ts +9 -0
  99. package/dist/utility/wrun/wrun-builtin-twin.util.js +40 -0
  100. package/dist/utility/wrun/wrun-canvas-cache.util.d.ts +28 -0
  101. package/dist/utility/wrun/wrun-canvas-cache.util.js +56 -0
  102. package/dist/utility/wrun/wrun-canvas-readout.util.d.ts +69 -0
  103. package/dist/utility/wrun/wrun-canvas-readout.util.js +53 -0
  104. package/dist/utility/wrun/wrun-canvases.util.d.ts +295 -0
  105. package/dist/utility/wrun/wrun-canvases.util.js +1432 -0
  106. package/dist/utility/wrun/wrun-cap-notice.util.d.ts +77 -0
  107. package/dist/utility/wrun/wrun-cap-notice.util.js +296 -0
  108. package/dist/utility/wrun/wrun-card-style.util.d.ts +154 -0
  109. package/dist/utility/wrun/wrun-card-style.util.js +539 -0
  110. package/dist/utility/wrun/wrun-chart-context.util.d.ts +89 -0
  111. package/dist/utility/wrun/wrun-chart-context.util.js +213 -0
  112. package/dist/utility/wrun/wrun-chart-names.util.d.ts +23 -0
  113. package/dist/utility/wrun/wrun-chart-names.util.js +33 -0
  114. package/dist/utility/wrun/wrun-chart-time.util.d.ts +12 -0
  115. package/dist/utility/wrun/wrun-chart-time.util.js +23 -0
  116. package/dist/utility/wrun/wrun-child-overlay-id.util.d.ts +13 -0
  117. package/dist/utility/wrun/wrun-child-overlay-id.util.js +44 -0
  118. package/dist/utility/wrun/wrun-color-facts.util.d.ts +40 -0
  119. package/dist/utility/wrun/wrun-color-facts.util.js +88 -0
  120. package/dist/utility/wrun/wrun-color-settings.lazy.d.ts +26 -0
  121. package/dist/utility/wrun/wrun-color-settings.lazy.js +9 -0
  122. package/dist/utility/wrun/wrun-color-settings.util.d.ts +351 -0
  123. package/dist/utility/wrun/wrun-color-settings.util.js +1363 -0
  124. package/dist/utility/wrun/wrun-footprint-modes.util.d.ts +23 -0
  125. package/dist/utility/wrun/wrun-footprint-modes.util.js +24 -0
  126. package/dist/utility/wrun/wrun-frames.d.ts +217 -0
  127. package/dist/utility/wrun/wrun-frames.js +1021 -0
  128. package/dist/utility/wrun/wrun-handles.util.d.ts +792 -0
  129. package/dist/utility/wrun/wrun-handles.util.js +2050 -0
  130. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.d.ts +156 -0
  131. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.js +342 -0
  132. package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +218 -0
  133. package/dist/utility/wrun/wrun-hosted-wire-caps.js +667 -0
  134. package/dist/utility/wrun/wrun-hosted-wire.util.d.ts +40 -0
  135. package/dist/utility/wrun/wrun-hosted-wire.util.js +27 -0
  136. package/dist/utility/wrun/wrun-hover.util.d.ts +182 -0
  137. package/dist/utility/wrun/wrun-hover.util.js +534 -0
  138. package/dist/utility/wrun/wrun-hud-looks.util.d.ts +246 -0
  139. package/dist/utility/wrun/wrun-hud-looks.util.js +587 -0
  140. package/dist/utility/wrun/wrun-integration-budget.util.d.ts +47 -0
  141. package/dist/utility/wrun/wrun-integration-budget.util.js +122 -0
  142. package/dist/utility/wrun/wrun-latest-snapshot.d.ts +19 -0
  143. package/dist/utility/wrun/wrun-latest-snapshot.js +7 -0
  144. package/dist/utility/wrun/wrun-levels-child.util.d.ts +131 -0
  145. package/dist/utility/wrun/wrun-levels-child.util.js +514 -0
  146. package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +75 -0
  147. package/dist/utility/wrun/wrun-lifecycle.contract.js +9 -0
  148. package/dist/utility/wrun/wrun-market-facts.util.d.ts +116 -0
  149. package/dist/utility/wrun/wrun-market-facts.util.js +238 -0
  150. package/dist/utility/wrun/wrun-output-style.util.d.ts +344 -0
  151. package/dist/utility/wrun/wrun-output-style.util.js +1239 -0
  152. package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +84 -0
  153. package/dist/utility/wrun/wrun-overlay-identity.util.js +142 -0
  154. package/dist/utility/wrun/wrun-overlay-result.util.d.ts +120 -0
  155. package/dist/utility/wrun/wrun-overlay-result.util.js +219 -0
  156. package/dist/utility/wrun/wrun-pack.util.d.ts +70 -0
  157. package/dist/utility/wrun/wrun-pack.util.js +301 -0
  158. package/dist/utility/wrun/wrun-pane-format.util.d.ts +180 -0
  159. package/dist/utility/wrun/wrun-pane-format.util.js +299 -0
  160. package/dist/utility/wrun/wrun-panel-style.lazy.d.ts +25 -0
  161. package/dist/utility/wrun/wrun-panel-style.lazy.js +15 -0
  162. package/dist/utility/wrun/wrun-panel-style.util.d.ts +174 -0
  163. package/dist/utility/wrun/wrun-panel-style.util.js +726 -0
  164. package/dist/utility/wrun/wrun-param-values.util.d.ts +69 -0
  165. package/dist/utility/wrun/wrun-param-values.util.js +230 -0
  166. package/dist/utility/wrun/wrun-refusal.util.d.ts +263 -0
  167. package/dist/utility/wrun/wrun-refusal.util.js +1259 -0
  168. package/dist/utility/wrun/wrun-registry-errors.util.d.ts +199 -0
  169. package/dist/utility/wrun/wrun-registry-errors.util.js +257 -0
  170. package/dist/utility/wrun/wrun-render-select.util.d.ts +336 -0
  171. package/dist/utility/wrun/wrun-render-select.util.js +1157 -0
  172. package/dist/utility/wrun/wrun-script-inputs.util.d.ts +434 -0
  173. package/dist/utility/wrun/wrun-script-inputs.util.js +1971 -0
  174. package/dist/utility/wrun/wrun-setting-words.lazy.d.ts +20 -0
  175. package/dist/utility/wrun/wrun-setting-words.lazy.js +9 -0
  176. package/dist/utility/wrun/wrun-setting-words.util.d.ts +79 -0
  177. package/dist/utility/wrun/wrun-setting-words.util.js +176 -0
  178. package/dist/utility/wrun/wrun-socket-row-store.d.ts +98 -0
  179. package/dist/utility/wrun/wrun-socket-row-store.js +417 -0
  180. package/dist/utility/wrun/wrun-style-flags.util.d.ts +18 -0
  181. package/dist/utility/wrun/wrun-style-flags.util.js +21 -0
  182. package/dist/utility/wrun/wrun-style-words.util.d.ts +148 -0
  183. package/dist/utility/wrun/wrun-style-words.util.js +439 -0
  184. package/dist/utility/wrun/wrun-table-look.util.d.ts +125 -0
  185. package/dist/utility/wrun/wrun-table-look.util.js +225 -0
  186. package/dist/utility/wrun/wrun-text-look.util.d.ts +51 -0
  187. package/dist/utility/wrun/wrun-text-look.util.js +227 -0
  188. package/dist/utility/wrun/wrun-text-params.util.d.ts +106 -0
  189. package/dist/utility/wrun/wrun-text-params.util.js +171 -0
  190. package/dist/utility/wrun/wrun-tpo-rows.util.d.ts +71 -0
  191. package/dist/utility/wrun/wrun-tpo-rows.util.js +438 -0
  192. package/dist/utility/wrun/wrun-twin-compute-inputs.util.d.ts +14 -0
  193. package/dist/utility/wrun/wrun-twin-compute-inputs.util.js +58 -0
  194. package/dist/utility/wrun/wrun-visibility-table.d.ts +437 -0
  195. package/dist/utility/wrun/wrun-visibility-table.js +237 -0
  196. package/dist/utility/wrun/wrun-volume-profile.util.d.ts +37 -0
  197. package/dist/utility/wrun/wrun-volume-profile.util.js +278 -0
  198. package/package.json +38 -0
@@ -0,0 +1,151 @@
1
+ import { classifyKScriptOutputRole, isKScriptDeclaredMarkerPlotType } from './kscript-output-role.util.js';
2
+ import { isRecord } from './kscript-run-result-shape.util.js';
3
+ export const KSCRIPT_CONTAINER_OUTPUT_KEY = '__kiyoKScriptContainer';
4
+ function countKScriptPlotOutputs(outputs) {
5
+ return Object.values(outputs).filter((output) => isRecord(output) && typeof output.plotType === 'string').length;
6
+ }
7
+ /**
8
+ * The container is the mounted PARENT output for every v2 run: the renderer
9
+ * mounts kScript overlays container-native (plot-roles Phase 5), so parent
10
+ * ELECTION no longer exists on this path. The container must still carry the
11
+ * overlay's timeline: titan builds the overlay's rawData (lazy-load
12
+ * bookkeeping, legend anchoring, and the under-delivery checks the fetch
13
+ * scheduler runs) from the parent's rows, and its own synthesis pads from
14
+ * combinedTimes, which the canonical wire may carry EMPTY. A container born
15
+ * with no rows mounts a zero-row overlay whose children carry full series:
16
+ * nothing anchors, and the scheduler refetches the "under-delivered" script
17
+ * forever. Share the densest child's rows so the parent timeline is real;
18
+ * `plotType: 'empty'` keeps it undrawn.
19
+ */
20
+ function isTimelineShapedRows(rows) {
21
+ const first = rows[0];
22
+ return Array.isArray(first) && typeof first[0] === 'number' && Number.isFinite(first[0]);
23
+ }
24
+ /**
25
+ * Tie-break rank when several outputs carry the same row count: a value plot
26
+ * (line, histogram, area) seeds the parent row ahead of a marker series and a
27
+ * decorator (background, label, table, guide). The parent's row is what the
28
+ * legend hover receives, so a flag or bgcolor series declared first must not
29
+ * become the overlay's readout. Row count still wins outright: the container's
30
+ * rows are the overlay timeline and may never come out shorter than before.
31
+ */
32
+ function timelineSeedRank(output) {
33
+ if (typeof output.plotType !== 'string')
34
+ return 3;
35
+ if (classifyKScriptOutputRole(output).isDecorator)
36
+ return 2;
37
+ if (isKScriptDeclaredMarkerPlotType(output.plotType))
38
+ return 1;
39
+ return 0;
40
+ }
41
+ /**
42
+ * The key of the output whose rows seed the container: the most rows, ties to
43
+ * the better seed rank, then declaration order; undefined when no output
44
+ * carries timestamped rows. Exported for the live tail patch, which proves the
45
+ * densest output of a whole result from its outputs' whole rows.
46
+ */
47
+ export function densestKScriptOutputKey(outputs) {
48
+ let densestKey;
49
+ let densestCount = 0;
50
+ let densestRank = Number.POSITIVE_INFINITY;
51
+ for (const [key, output] of Object.entries(outputs)) {
52
+ if (!isRecord(output))
53
+ continue;
54
+ const rows = output.data;
55
+ // Only timestamped series can seed the timeline: table/pie style outputs
56
+ // carry non-timeseries rows the renderer's validation rightly rejects.
57
+ if (!Array.isArray(rows) || rows.length === 0 || !isTimelineShapedRows(rows))
58
+ continue;
59
+ const rank = timelineSeedRank(output);
60
+ if (rows.length > densestCount || (rows.length === densestCount && rank < densestRank)) {
61
+ densestKey = key;
62
+ densestCount = rows.length;
63
+ densestRank = rank;
64
+ }
65
+ }
66
+ return densestKey;
67
+ }
68
+ function densestOutputRows(outputs) {
69
+ const key = densestKScriptOutputKey(outputs);
70
+ return key === undefined ? [] : (outputs[key].data ?? []);
71
+ }
72
+ /**
73
+ * A run result whose container spans its run: the wrun adapter's mark (`containerSpansRun`). A
74
+ * kScript container spans its engine timeline by itself (scalar series and marker skeletons keep a
75
+ * row on every bar the script ran); a wrun output reaches the preparation as [ts, value] rows whose
76
+ * NaN rows it drops, so its container only mirrors the densest output's finite rows.
77
+ */
78
+ export function kScriptContainerSpansRun(runResult) {
79
+ return isRecord(runResult) && runResult.containerSpansRun === true;
80
+ }
81
+ function finiteRowTime(row) {
82
+ return Array.isArray(row) && typeof row[0] === 'number' && Number.isFinite(row[0]) ? row[0] : undefined;
83
+ }
84
+ /**
85
+ * Lay a spanning container (kScriptContainerSpansRun) over the chart's bars of its run: the rows it
86
+ * mirrors stay as they are, and every chart bar from its first row (`span.fromMs`, else the chart's
87
+ * first bar, for a container that holds none: a frame-only parent) through the run's last bar
88
+ * (`span.toMs`) that holds no row gains an empty one at the row's width ([ts, NaN, ...]). The
89
+ * legend's hovered row (the exact-bar match) and its resting row (the newest one) then sit on the
90
+ * bar they name, as on a kScript overlay: a seed that stops before the newest bar no longer hands
91
+ * every legend cell its own last bar. Anything else, or a container with no gap, is returned as is.
92
+ */
93
+ export function withKScriptContainerSpanningRun(runResult, chartTimes, span) {
94
+ if (!kScriptContainerSpansRun(runResult) || chartTimes.length === 0 || !Number.isFinite(span.toMs))
95
+ return runResult;
96
+ const outputs = runResult.outputs;
97
+ if (!isRecord(outputs))
98
+ return runResult;
99
+ const container = outputs[KSCRIPT_CONTAINER_OUTPUT_KEY];
100
+ if (!isRecord(container))
101
+ return runResult;
102
+ const rows = Array.isArray(container.data) ? container.data : [];
103
+ const firstTs = rows.length > 0 ? finiteRowTime(rows[0]) : (span.fromMs ?? chartTimes[0]);
104
+ if (firstTs === undefined)
105
+ return runResult;
106
+ const width = Array.isArray(rows[0]) ? Math.max(2, rows[0].length) : 2;
107
+ const spanned = [];
108
+ let at = 0;
109
+ let added = 0;
110
+ for (const time of chartTimes) {
111
+ if (time < firstTs)
112
+ continue;
113
+ if (time > span.toMs)
114
+ break;
115
+ while (at < rows.length && (finiteRowTime(rows[at]) ?? Number.NEGATIVE_INFINITY) < time)
116
+ spanned.push(rows[at++]);
117
+ if (at < rows.length && finiteRowTime(rows[at]) === time) {
118
+ spanned.push(rows[at++]);
119
+ continue;
120
+ }
121
+ spanned.push([time, ...Array.from({ length: width - 1 }, () => Number.NaN)]);
122
+ added += 1;
123
+ }
124
+ if (added === 0)
125
+ return runResult;
126
+ while (at < rows.length)
127
+ spanned.push(rows[at++]);
128
+ return {
129
+ ...runResult,
130
+ outputs: { ...outputs, [KSCRIPT_CONTAINER_OUTPUT_KEY]: { ...container, data: spanned } },
131
+ };
132
+ }
133
+ export function withKScriptContainerOutput(runResult) {
134
+ if (!isRecord(runResult) || !isRecord(runResult.outputs))
135
+ return runResult;
136
+ if (isRecord(runResult.outputs[KSCRIPT_CONTAINER_OUTPUT_KEY]))
137
+ return runResult;
138
+ if (countKScriptPlotOutputs(runResult.outputs) === 0)
139
+ return runResult;
140
+ return {
141
+ ...runResult,
142
+ outputs: {
143
+ [KSCRIPT_CONTAINER_OUTPUT_KEY]: {
144
+ plotType: 'empty',
145
+ kScriptContainer: true,
146
+ data: densestOutputRows(runResult.outputs),
147
+ },
148
+ ...runResult.outputs,
149
+ },
150
+ };
151
+ }
@@ -0,0 +1,83 @@
1
+ import type { RuntimeDiagnostic } from '../../interfaces/kscript-diagnostics.interface.js';
2
+ export declare const KSCRIPT_VIEWPORT_MINI_CHART_PLOT_TYPE = "viewportMiniChartGrid";
3
+ export declare const KSCRIPT_VIEWPORT_OVERLAY_ANCHOR_KEY = "__kiyoViewportOverlayAnchor";
4
+ export interface KScriptViewportMiniChartPanel {
5
+ label: string;
6
+ bars: number[][];
7
+ }
8
+ type KScriptViewportMiniChartCandleStyle = 'candles' | 'hollow' | 'bars' | 'line' | 'area';
9
+ export interface KScriptViewportMiniChartGridPayload {
10
+ version: 1;
11
+ panels: KScriptViewportMiniChartPanel[];
12
+ title: string | null;
13
+ position: KScriptViewportMiniChartAnchor;
14
+ x: number;
15
+ y: number;
16
+ columns: number;
17
+ panelWidth: number;
18
+ panelHeight: number;
19
+ gap: number;
20
+ backgroundColor: string;
21
+ borderColor: string;
22
+ textColor: string;
23
+ bullColor: string;
24
+ bearColor: string;
25
+ wickColor: string;
26
+ candleStyle: KScriptViewportMiniChartCandleStyle;
27
+ maFastLength: number | null;
28
+ maFastColor: string;
29
+ maSlowLength: number | null;
30
+ maSlowColor: string;
31
+ maWidth: number;
32
+ showFastMA: boolean;
33
+ showSlowMA: boolean;
34
+ showChange: boolean;
35
+ showVolume: boolean;
36
+ }
37
+ type KScriptViewportMiniChartAnchor = 'top_left' | 'top_center' | 'top_right' | 'middle_left' | 'middle_center' | 'middle_right' | 'bottom_left' | 'bottom_center' | 'bottom_right';
38
+ export interface KScriptViewportOverlayEntry {
39
+ indicatorId: string;
40
+ outputKey: string;
41
+ kind: 'miniChartGrid';
42
+ payload: KScriptViewportMiniChartGridPayload;
43
+ zOrder: number;
44
+ }
45
+ export interface KScriptViewportOverlayExtraction {
46
+ runResult: unknown;
47
+ overlays: KScriptViewportOverlayEntry[];
48
+ diagnostics: RuntimeDiagnostic[];
49
+ }
50
+ type Listener = () => void;
51
+ export declare function extractKScriptViewportOverlays(indicatorId: string, runResult: unknown): KScriptViewportOverlayExtraction;
52
+ export declare function publishKScriptViewportOverlays(indicatorId: string, overlays: KScriptViewportOverlayEntry[]): void;
53
+ /** Drop an indicator's retained overlays on REAL removal/teardown only.
54
+ * Transient remounts and domain-switch blanks must keep the last payload
55
+ * (see the retention rule in overlayFromMiniChartOutput). */
56
+ export declare function removeKScriptViewportOverlays(indicatorId: string): void;
57
+ /** Engine teardown: drop the retained overlays of the indicators the departing
58
+ * engine mounted, except the ids in `keptIds` (the ones its pane still lists:
59
+ * a same-workspace remount republishes over its last payload, per the
60
+ * retention rule above). Entries carry no chart index, so the caller names
61
+ * the ids. One notification for the whole release. */
62
+ export declare function releaseKScriptViewportOverlays(mountedIds: Iterable<string>, keptIds?: ReadonlySet<string>): void;
63
+ /**
64
+ * Which overlays' grids the grid overlay shows (id -> shown). The workspace
65
+ * row's eye decides; an overlay the workspace does not list yet (mid-mount, or
66
+ * session-only) reads its engine meta. A wrun package's grid also follows the
67
+ * engine flag, which carries every app gate beside the eye (the Visibility
68
+ * intervals among them), as the package's child overlays do
69
+ * (wrun-panel-mount.util syncChildVisibility): gated off the current interval,
70
+ * its grid goes with it. A kScript grid keeps reading the row alone.
71
+ */
72
+ export declare function viewportOverlayOwnersShown(rows: ReadonlyArray<{
73
+ id?: unknown;
74
+ isHidden?: boolean;
75
+ settings?: unknown;
76
+ }>, metas: ReadonlyArray<{
77
+ id?: unknown;
78
+ isHidden?: boolean;
79
+ }>): Map<string, boolean>;
80
+ export declare function subscribeKScriptViewportOverlays(listener: Listener): () => void;
81
+ export declare function getKScriptViewportOverlaysSnapshot(): KScriptViewportOverlayEntry[];
82
+ export declare function clearKScriptViewportOverlaysForTests(): void;
83
+ export {};
@@ -0,0 +1,443 @@
1
+ import { isWrunPackageOverlaySettings } from '../wrun/wrun-overlay-identity.util.js';
2
+ export const KSCRIPT_VIEWPORT_MINI_CHART_PLOT_TYPE = 'viewportMiniChartGrid';
3
+ export const KSCRIPT_VIEWPORT_OVERLAY_ANCHOR_KEY = '__kiyoViewportOverlayAnchor';
4
+ const FULL_ANCHORS = new Set([
5
+ 'top_left',
6
+ 'top_center',
7
+ 'top_right',
8
+ 'middle_left',
9
+ 'middle_center',
10
+ 'middle_right',
11
+ 'bottom_left',
12
+ 'bottom_center',
13
+ 'bottom_right',
14
+ ]);
15
+ const overlaysByIndicator = new Map();
16
+ const listeners = new Set();
17
+ function isRecord(value) {
18
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
19
+ }
20
+ function viewportOverlayDiagnostic(message) {
21
+ return {
22
+ stage: 'render',
23
+ severity: 'error',
24
+ code: 'FE_KSCRIPT_VIEWPORT_OVERLAY_INVALID',
25
+ message,
26
+ suggestedFix: 'The script emitted a viewport overlay shape the chart adapter cannot safely render.',
27
+ };
28
+ }
29
+ function fail(message) {
30
+ throw new Error(message);
31
+ }
32
+ function requiredString(value, path) {
33
+ if (typeof value !== 'string' || value.trim().length === 0) {
34
+ fail(`${path} must be a non-empty string.`);
35
+ }
36
+ return value.trim();
37
+ }
38
+ function optionalTitle(value, path) {
39
+ if (value === null || value === undefined)
40
+ return null;
41
+ if (typeof value !== 'string')
42
+ fail(`${path} must be a string or null.`);
43
+ const trimmed = value.trim();
44
+ return trimmed.length > 0 ? trimmed : null;
45
+ }
46
+ function requiredFiniteNumber(value, path, min, max) {
47
+ if (typeof value !== 'number' || !Number.isFinite(value))
48
+ fail(`${path} must be a finite number.`);
49
+ if (value < min || value > max)
50
+ fail(`${path} must be between ${min} and ${max}.`);
51
+ return value;
52
+ }
53
+ function requiredInteger(value, path, min, max) {
54
+ const numberValue = requiredFiniteNumber(value, path, min, max);
55
+ if (!Number.isInteger(numberValue))
56
+ fail(`${path} must be an integer.`);
57
+ return numberValue;
58
+ }
59
+ function optionalPositiveInteger(value, path) {
60
+ if (value === null || value === undefined)
61
+ return null;
62
+ if (typeof value !== 'number' || !Number.isInteger(value) || value <= 0) {
63
+ fail(`${path} must be a positive integer or null.`);
64
+ }
65
+ return value;
66
+ }
67
+ function requiredBoolean(value, path) {
68
+ if (typeof value !== 'boolean')
69
+ fail(`${path} must be a boolean.`);
70
+ return value;
71
+ }
72
+ // 3.0.48+ styling fields: absent on payloads from older engines, so they take
73
+ // the engine's defaults instead of failing the whole overlay.
74
+ function optionalBoolean(value, path, fallback) {
75
+ if (value === null || value === undefined)
76
+ return fallback;
77
+ return requiredBoolean(value, path);
78
+ }
79
+ const MINI_CHART_CANDLE_STYLES = new Set([
80
+ 'candles',
81
+ 'hollow',
82
+ 'bars',
83
+ 'line',
84
+ 'area',
85
+ ]);
86
+ function optionalCandleStyle(value, path) {
87
+ if (value === null || value === undefined)
88
+ return 'candles';
89
+ if (typeof value === 'string' && MINI_CHART_CANDLE_STYLES.has(value)) {
90
+ return value;
91
+ }
92
+ fail(`${path} must be one of "candles", "hollow", "bars", "line", "area".`);
93
+ }
94
+ function requiredAnchor(value, path) {
95
+ const anchor = requiredString(value, path);
96
+ if (!FULL_ANCHORS.has(anchor)) {
97
+ fail(`${path} must be a full anchor string.`);
98
+ }
99
+ return anchor;
100
+ }
101
+ function normalizeMiniChartBars(value, path) {
102
+ if (!Array.isArray(value))
103
+ fail(`${path} must be an OHLCV row array.`);
104
+ if (value.length > 100)
105
+ fail(`${path} supports at most 100 bars.`);
106
+ return value.map((row, index) => {
107
+ if (!Array.isArray(row) || row.length < 5) {
108
+ fail(`${path}[${index}] must contain [time, open, high, low, close].`);
109
+ }
110
+ const normalized = row.slice(0, 6).map((cell) => Number(cell));
111
+ if (normalized.slice(0, 5).some((cell) => !Number.isFinite(cell))) {
112
+ fail(`${path}[${index}] contains a non-finite OHLC value.`);
113
+ }
114
+ if (normalized.length < 6 || !Number.isFinite(normalized[5]))
115
+ normalized[5] = Number.NaN;
116
+ return normalized;
117
+ });
118
+ }
119
+ function normalizeMiniChartPanels(value, path) {
120
+ if (!Array.isArray(value))
121
+ fail(`${path} must be an array.`);
122
+ if (value.length > 12)
123
+ fail(`${path} supports at most 12 panels.`);
124
+ return value.map((panel, index) => {
125
+ if (!isRecord(panel))
126
+ fail(`${path}[${index}] must be an object.`);
127
+ return {
128
+ label: requiredString(panel.label, `${path}[${index}].label`),
129
+ bars: normalizeMiniChartBars(panel.bars, `${path}[${index}].bars`),
130
+ };
131
+ });
132
+ }
133
+ function normalizeMiniChartGridPayload(value) {
134
+ if (!isRecord(value))
135
+ fail('viewportMiniChartGrid payload must be an object.');
136
+ return {
137
+ version: requiredInteger(value.version, 'payload.version', 1, 1),
138
+ panels: normalizeMiniChartPanels(value.panels, 'payload.panels'),
139
+ title: optionalTitle(value.title, 'payload.title'),
140
+ position: requiredAnchor(value.position, 'payload.position'),
141
+ x: requiredFiniteNumber(value.x, 'payload.x', -2000, 2000),
142
+ y: requiredFiniteNumber(value.y, 'payload.y', -2000, 2000),
143
+ columns: requiredInteger(value.columns, 'payload.columns', 1, 4),
144
+ panelWidth: requiredFiniteNumber(value.panelWidth, 'payload.panelWidth', 72, 320),
145
+ panelHeight: requiredFiniteNumber(value.panelHeight, 'payload.panelHeight', 48, 220),
146
+ gap: requiredFiniteNumber(value.gap, 'payload.gap', 0, 48),
147
+ backgroundColor: requiredString(value.backgroundColor, 'payload.backgroundColor'),
148
+ borderColor: requiredString(value.borderColor, 'payload.borderColor'),
149
+ textColor: requiredString(value.textColor, 'payload.textColor'),
150
+ bullColor: requiredString(value.bullColor, 'payload.bullColor'),
151
+ bearColor: requiredString(value.bearColor, 'payload.bearColor'),
152
+ wickColor: requiredString(value.wickColor, 'payload.wickColor'),
153
+ candleStyle: optionalCandleStyle(value.candleStyle, 'payload.candleStyle'),
154
+ maFastLength: optionalPositiveInteger(value.maFastLength, 'payload.maFastLength'),
155
+ maFastColor: requiredString(value.maFastColor, 'payload.maFastColor'),
156
+ maSlowLength: optionalPositiveInteger(value.maSlowLength, 'payload.maSlowLength'),
157
+ maSlowColor: requiredString(value.maSlowColor, 'payload.maSlowColor'),
158
+ maWidth: requiredFiniteNumber(value.maWidth, 'payload.maWidth', 0.5, 5),
159
+ showFastMA: requiredBoolean(value.showFastMA, 'payload.showFastMA'),
160
+ showSlowMA: requiredBoolean(value.showSlowMA, 'payload.showSlowMA'),
161
+ showChange: optionalBoolean(value.showChange, 'payload.showChange', false),
162
+ showVolume: optionalBoolean(value.showVolume, 'payload.showVolume', false),
163
+ };
164
+ }
165
+ function isMiniChartPayloadLike(value) {
166
+ return isRecord(value) && value.version === 1 && Array.isArray(value.panels);
167
+ }
168
+ function miniChartPayloadCandidatesFromRows(rows) {
169
+ if (isRecord(rows))
170
+ return [rows];
171
+ if (!Array.isArray(rows))
172
+ return [];
173
+ const candidates = [];
174
+ rows.forEach((row) => {
175
+ if (isRecord(row)) {
176
+ candidates.push(row);
177
+ return;
178
+ }
179
+ if (!Array.isArray(row))
180
+ return;
181
+ if (row.length === 1)
182
+ candidates.push(row[0]);
183
+ if (row.length >= 2)
184
+ candidates.push(row[1]);
185
+ row.forEach((cell) => {
186
+ if (isMiniChartPayloadLike(cell))
187
+ candidates.push(cell);
188
+ });
189
+ });
190
+ return candidates;
191
+ }
192
+ function payloadFromMiniChartOutput(output, outputKey) {
193
+ const candidates = [
194
+ ...miniChartPayloadCandidatesFromRows(output.data),
195
+ ...miniChartPayloadCandidatesFromRows(output.rawData),
196
+ ...miniChartPayloadCandidatesFromRows(output.activeData),
197
+ ];
198
+ for (const candidate of candidates) {
199
+ if (isMiniChartPayloadLike(candidate))
200
+ return normalizeMiniChartGridPayload(candidate);
201
+ }
202
+ const payloadLikeCandidate = candidates.find((candidate) => isRecord(candidate) && ('version' in candidate || 'panels' in candidate));
203
+ if (payloadLikeCandidate)
204
+ return normalizeMiniChartGridPayload(payloadLikeCandidate);
205
+ if (candidates.length === 0) {
206
+ fail(`viewportMiniChartGrid output "${outputKey}" must preserve its payload row in data, rawData, or activeData.`);
207
+ }
208
+ fail(`viewportMiniChartGrid output "${outputKey}" must store payload as [null, payload], [payload], or payload.`);
209
+ }
210
+ function outputKeyFromMiniChartOutput(output, fallback) {
211
+ for (const candidate of [output.sourcePlotId, output.outputKey, output.plotKey, output.plotId, output.id]) {
212
+ if (typeof candidate === 'string' && candidate.length > 0)
213
+ return candidate;
214
+ }
215
+ return fallback;
216
+ }
217
+ function zOrderFromMiniChartOutput(output) {
218
+ return typeof output.zOrder === 'number' && Number.isFinite(output.zOrder) ? output.zOrder : 0;
219
+ }
220
+ function previouslyPublishedOverlay(indicatorId, outputKey) {
221
+ return overlaysByIndicator.get(indicatorId)?.find((entry) => entry.outputKey === outputKey);
222
+ }
223
+ function overlayFromMiniChartOutput(indicatorId, outputKey, output) {
224
+ try {
225
+ return {
226
+ indicatorId,
227
+ outputKey,
228
+ kind: 'miniChartGrid',
229
+ payload: payloadFromMiniChartOutput(output, outputKey),
230
+ zOrder: zOrderFromMiniChartOutput(output),
231
+ };
232
+ }
233
+ catch (error) {
234
+ // The payload rides run results as an UNTIMED [null, payload] row, and
235
+ // time-keyed machinery (lazy merges, windowed applies, caches) can drop it
236
+ // while keeping the output itself. A payload-less update must never blank a
237
+ // working overlay: retain the last published payload and let the next full
238
+ // result replace it. Only a first paint with no prior payload stays loud.
239
+ const previous = previouslyPublishedOverlay(indicatorId, outputKey);
240
+ if (previous)
241
+ return { ...previous, zOrder: zOrderFromMiniChartOutput(output) };
242
+ throw error;
243
+ }
244
+ }
245
+ function viewportOverlayAnchorOutput() {
246
+ return {
247
+ plot: 'empty',
248
+ plotType: 'empty',
249
+ data: [],
250
+ rawData: [],
251
+ activeData: [],
252
+ colors: [],
253
+ width: 1,
254
+ sourcePlotId: KSCRIPT_VIEWPORT_OVERLAY_ANCHOR_KEY,
255
+ outputKey: KSCRIPT_VIEWPORT_OVERLAY_ANCHOR_KEY,
256
+ plotId: KSCRIPT_VIEWPORT_OVERLAY_ANCHOR_KEY,
257
+ outputRole: undefined,
258
+ paneParentCandidate: false,
259
+ showPriceDisplay: false,
260
+ kScriptViewportOverlayAnchor: true,
261
+ };
262
+ }
263
+ function viewportOverlayAnchorMetadataOutput(source) {
264
+ const settings = isRecord(source.settings)
265
+ ? {
266
+ ...source.settings,
267
+ colors: [],
268
+ showPriceDisplay: false,
269
+ }
270
+ : { colors: [], showPriceDisplay: false };
271
+ return {
272
+ ...source,
273
+ ...viewportOverlayAnchorOutput(),
274
+ nestedMeta: [],
275
+ settings,
276
+ };
277
+ }
278
+ function stripViewportOverlaysFromOutputsMap(indicatorId, outputs) {
279
+ const overlays = [];
280
+ const nextOutputs = {};
281
+ let changed = false;
282
+ for (const [outputKey, output] of Object.entries(outputs)) {
283
+ if (isRecord(output) && output.plotType === KSCRIPT_VIEWPORT_MINI_CHART_PLOT_TYPE) {
284
+ changed = true;
285
+ overlays.push(overlayFromMiniChartOutput(indicatorId, outputKey, output));
286
+ continue;
287
+ }
288
+ const stripped = stripViewportOverlaysFromMetadataOutput(indicatorId, output, outputKey, false);
289
+ if (stripped.changed)
290
+ changed = true;
291
+ overlays.push(...stripped.overlays);
292
+ nextOutputs[outputKey] = stripped.output;
293
+ }
294
+ if (changed && Object.keys(nextOutputs).length === 0) {
295
+ nextOutputs[KSCRIPT_VIEWPORT_OVERLAY_ANCHOR_KEY] = viewportOverlayAnchorOutput();
296
+ }
297
+ return { outputs: changed ? nextOutputs : outputs, overlays, changed };
298
+ }
299
+ function stripViewportOverlaysFromMetadataOutput(indicatorId, output, fallbackOutputKey, removeSelf) {
300
+ if (!isRecord(output))
301
+ return { output, overlays: [], changed: false, removedSelf: false };
302
+ if (output.plotType === KSCRIPT_VIEWPORT_MINI_CHART_PLOT_TYPE) {
303
+ const outputKey = outputKeyFromMiniChartOutput(output, fallbackOutputKey);
304
+ return {
305
+ output: removeSelf ? undefined : viewportOverlayAnchorMetadataOutput(output),
306
+ overlays: [overlayFromMiniChartOutput(indicatorId, outputKey, output)],
307
+ changed: true,
308
+ removedSelf: removeSelf,
309
+ };
310
+ }
311
+ let changed = false;
312
+ let nextOutput = null;
313
+ const overlays = [];
314
+ const clone = () => {
315
+ if (!nextOutput)
316
+ nextOutput = { ...output };
317
+ return nextOutput;
318
+ };
319
+ if (isRecord(output.outputs)) {
320
+ const strippedOutputs = stripViewportOverlaysFromOutputsMap(indicatorId, output.outputs);
321
+ if (strippedOutputs.changed) {
322
+ clone().outputs = strippedOutputs.outputs;
323
+ changed = true;
324
+ }
325
+ overlays.push(...strippedOutputs.overlays);
326
+ }
327
+ if (Array.isArray(output.nestedMeta)) {
328
+ const nextNestedMeta = [];
329
+ let nestedChanged = false;
330
+ output.nestedMeta.forEach((nested, index) => {
331
+ const stripped = stripViewportOverlaysFromMetadataOutput(indicatorId, nested, `${fallbackOutputKey}:nested:${index}`, true);
332
+ overlays.push(...stripped.overlays);
333
+ if (stripped.removedSelf) {
334
+ nestedChanged = true;
335
+ return;
336
+ }
337
+ if (stripped.changed)
338
+ nestedChanged = true;
339
+ nextNestedMeta.push(stripped.output);
340
+ });
341
+ if (nestedChanged) {
342
+ clone().nestedMeta = nextNestedMeta;
343
+ changed = true;
344
+ }
345
+ }
346
+ return {
347
+ output: nextOutput ?? output,
348
+ overlays,
349
+ changed,
350
+ removedSelf: false,
351
+ };
352
+ }
353
+ export function extractKScriptViewportOverlays(indicatorId, runResult) {
354
+ if (!isRecord(runResult)) {
355
+ return { runResult, overlays: [], diagnostics: [] };
356
+ }
357
+ try {
358
+ const stripped = stripViewportOverlaysFromMetadataOutput(indicatorId, runResult, indicatorId, false);
359
+ return {
360
+ runResult: stripped.output,
361
+ overlays: stripped.overlays,
362
+ diagnostics: [],
363
+ };
364
+ }
365
+ catch (error) {
366
+ // Same retention rule as overlayFromMiniChartOutput: a malformed update
367
+ // keeps whatever this indicator last published instead of clearing it.
368
+ return {
369
+ runResult,
370
+ overlays: overlaysByIndicator.get(indicatorId) ?? [],
371
+ diagnostics: [viewportOverlayDiagnostic(error instanceof Error ? error.message : 'Invalid viewport overlay.')],
372
+ };
373
+ }
374
+ }
375
+ export function publishKScriptViewportOverlays(indicatorId, overlays) {
376
+ if (overlays.length > 0)
377
+ overlaysByIndicator.set(indicatorId, overlays);
378
+ else
379
+ overlaysByIndicator.delete(indicatorId);
380
+ for (const listener of listeners)
381
+ listener();
382
+ }
383
+ /** Drop an indicator's retained overlays on REAL removal/teardown only.
384
+ * Transient remounts and domain-switch blanks must keep the last payload
385
+ * (see the retention rule in overlayFromMiniChartOutput). */
386
+ export function removeKScriptViewportOverlays(indicatorId) {
387
+ if (!overlaysByIndicator.delete(indicatorId))
388
+ return;
389
+ for (const listener of listeners)
390
+ listener();
391
+ }
392
+ /** Engine teardown: drop the retained overlays of the indicators the departing
393
+ * engine mounted, except the ids in `keptIds` (the ones its pane still lists:
394
+ * a same-workspace remount republishes over its last payload, per the
395
+ * retention rule above). Entries carry no chart index, so the caller names
396
+ * the ids. One notification for the whole release. */
397
+ export function releaseKScriptViewportOverlays(mountedIds, keptIds) {
398
+ let released = false;
399
+ for (const indicatorId of mountedIds) {
400
+ if (!keptIds?.has(indicatorId) && overlaysByIndicator.delete(indicatorId))
401
+ released = true;
402
+ }
403
+ if (!released)
404
+ return;
405
+ for (const listener of listeners)
406
+ listener();
407
+ }
408
+ /**
409
+ * Which overlays' grids the grid overlay shows (id -> shown). The workspace
410
+ * row's eye decides; an overlay the workspace does not list yet (mid-mount, or
411
+ * session-only) reads its engine meta. A wrun package's grid also follows the
412
+ * engine flag, which carries every app gate beside the eye (the Visibility
413
+ * intervals among them), as the package's child overlays do
414
+ * (wrun-panel-mount.util syncChildVisibility): gated off the current interval,
415
+ * its grid goes with it. A kScript grid keeps reading the row alone.
416
+ */
417
+ export function viewportOverlayOwnersShown(rows, metas) {
418
+ const engineHidden = new Set(metas.filter((meta) => meta?.isHidden === true).map((meta) => meta.id));
419
+ const shown = new Map();
420
+ for (const row of rows) {
421
+ if (typeof row?.id !== 'string')
422
+ continue;
423
+ const gated = isWrunPackageOverlaySettings(row.settings) && engineHidden.has(row.id);
424
+ shown.set(row.id, row.isHidden !== true && !gated);
425
+ }
426
+ for (const meta of metas) {
427
+ if (typeof meta?.id === 'string' && !shown.has(meta.id))
428
+ shown.set(meta.id, meta.isHidden !== true);
429
+ }
430
+ return shown;
431
+ }
432
+ export function subscribeKScriptViewportOverlays(listener) {
433
+ listeners.add(listener);
434
+ return () => listeners.delete(listener);
435
+ }
436
+ export function getKScriptViewportOverlaysSnapshot() {
437
+ return Array.from(overlaysByIndicator.values()).flat();
438
+ }
439
+ export function clearKScriptViewportOverlaysForTests() {
440
+ overlaysByIndicator.clear();
441
+ for (const listener of listeners)
442
+ listener();
443
+ }