@dsh-ft/dsh-fft-paper-trading 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +9 -1
- package/package.json +2 -5
- package/src/client/index.js +8 -0
- package/node_modules/@ftshare-lab/dsh-kline/CHANGELOG.md +0 -564
- package/node_modules/@ftshare-lab/dsh-kline/LICENSE +0 -21
- package/node_modules/@ftshare-lab/dsh-kline/PROVENANCE.md +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/README.en.md +0 -265
- package/node_modules/@ftshare-lab/dsh-kline/README.md +0 -281
- package/node_modules/@ftshare-lab/dsh-kline/chart_service.py +0 -686
- package/node_modules/@ftshare-lab/dsh-kline/config/basic-symbols.json +0 -570
- package/node_modules/@ftshare-lab/dsh-kline/config/dsh-kline.patch.yml +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/config/dsh-tool-catalog.json +0 -3783
- package/node_modules/@ftshare-lab/dsh-kline/cordis.patch.yml +0 -33
- package/node_modules/@ftshare-lab/dsh-kline/core/__init__.py +0 -0
- package/node_modules/@ftshare-lab/dsh-kline/core/adjustment.py +0 -16
- package/node_modules/@ftshare-lab/dsh-kline/core/calc.py +0 -683
- package/node_modules/@ftshare-lab/dsh-kline/core/rows.py +0 -228
- package/node_modules/@ftshare-lab/dsh-kline/core/ta_engine.py +0 -99
- package/node_modules/@ftshare-lab/dsh-kline/lib/candle-sequence.js +0 -15
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-adjustment.js +0 -7
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-annotations.js +0 -33
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-applications.js +0 -171
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-context.js +0 -164
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-observation.js +0 -241
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-service-status.js +0 -82
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-source-status.js +0 -39
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-sources.js +0 -349
- package/node_modules/@ftshare-lab/dsh-kline/lib/client.js +0 -15445
- package/node_modules/@ftshare-lab/dsh-kline/lib/drawing-feedback.js +0 -252
- package/node_modules/@ftshare-lab/dsh-kline/lib/fibonacci.js +0 -18
- package/node_modules/@ftshare-lab/dsh-kline/lib/ftshare-settings-bridge.js +0 -159
- package/node_modules/@ftshare-lab/dsh-kline/lib/index.js +0 -279
- package/node_modules/@ftshare-lab/dsh-kline/lib/labeled-polyline.js +0 -17
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-bands.js +0 -55
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-series.js +0 -47
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-sticks.js +0 -32
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/candle-sequence.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-adjustment.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-annotations.d.ts +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-applications.d.ts +0 -26
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-context.d.ts +0 -14
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-observation.d.ts +0 -8
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-service-status.d.ts +0 -22
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-source-status.d.ts +0 -47
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-sources.d.ts +0 -25
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/BetterSidebarTab.d.ts +0 -12
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/ClassicShell.d.ts +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/KlineContent.d.ts +0 -37
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/PaperKlinePanel.d.ts +0 -15
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/canvas-panel.d.ts +0 -33
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-apply.d.ts +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-context.d.ts +0 -23
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-session-resolver.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/client-context.d.ts +0 -14
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/conversation.d.ts +0 -39
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/default-tab.d.ts +0 -3
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/embed.d.ts +0 -37
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/generated-view.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/horizontal-lines.d.ts +0 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/host-services.d.ts +0 -55
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/index.d.ts +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/mode-preferences.d.ts +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-contract.d.ts +0 -52
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-register.d.ts +0 -46
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-services.d.ts +0 -45
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar.d.ts +0 -47
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/sidebar-integration.d.ts +0 -12
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/ui-onboarding.d.ts +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/version.d.ts +0 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/drawing-feedback.d.ts +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/fibonacci.d.ts +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/ftshare-settings-bridge.d.ts +0 -50
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/index.d.ts +0 -26
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/labeled-polyline.d.ts +0 -11
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-bands.d.ts +0 -3
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-series.d.ts +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-sticks.d.ts +0 -3
- package/node_modules/@ftshare-lab/dsh-kline/package.json +0 -136
- package/node_modules/@ftshare-lab/dsh-kline/requirements.txt +0 -6
- package/node_modules/@ftshare-lab/dsh-kline/scripts/bootstrap.sh +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/scripts/dsh-bootstrap.mjs +0 -179
- package/node_modules/@ftshare-lab/dsh-kline/scripts/dsh-mcp-plugin.mjs +0 -27
- package/node_modules/@ftshare-lab/dsh-kline/scripts/run-dsh-kline.mjs +0 -655
- package/node_modules/@ftshare-lab/dsh-kline/scripts/run-dsh-kline.sh +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/server.py +0 -1734
- package/node_modules/@ftshare-lab/dsh-kline/tools/__init__.py +0 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/annotations.py +0 -119
- package/node_modules/@ftshare-lab/dsh-kline/tools/baostock_source.py +0 -176
- package/node_modules/@ftshare-lab/dsh-kline/tools/calc.py +0 -39
- package/node_modules/@ftshare-lab/dsh-kline/tools/channel_candidates.py +0 -260
- package/node_modules/@ftshare-lab/dsh-kline/tools/channel_preview.py +0 -82
- package/node_modules/@ftshare-lab/dsh-kline/tools/channels.py +0 -104
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_applications.py +0 -204
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_context.py +0 -186
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_edits.py +0 -345
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_functions.py +0 -466
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_indicators.py +0 -63
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_measurements.py +0 -84
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_source_status.py +0 -32
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_sources.py +0 -31
- package/node_modules/@ftshare-lab/dsh-kline/tools/draw.py +0 -394
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_feedback.py +0 -45
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_interaction.py +0 -117
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_verification.py +0 -141
- package/node_modules/@ftshare-lab/dsh-kline/tools/fetch.py +0 -4681
- package/node_modules/@ftshare-lab/dsh-kline/tools/fibonacci.py +0 -83
- package/node_modules/@ftshare-lab/dsh-kline/tools/foreign_daily.py +0 -185
- package/node_modules/@ftshare-lab/dsh-kline/tools/free_sources.py +0 -483
- package/node_modules/@ftshare-lab/dsh-kline/tools/lines.py +0 -91
- package/node_modules/@ftshare-lab/dsh-kline/tools/pivots.py +0 -60
- package/node_modules/@ftshare-lab/dsh-kline/tools/polylines.py +0 -125
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_bands.py +0 -165
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_series.py +0 -162
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_sticks.py +0 -114
- package/node_modules/@ftshare-lab/dsh-kline/tools/range_candidates.py +0 -170
- package/node_modules/@ftshare-lab/dsh-kline/tools/rectangles.py +0 -76
- package/node_modules/@ftshare-lab/dsh-kline/tools/regression_channels.py +0 -87
- package/node_modules/@ftshare-lab/dsh-kline/tools/ta_functions.py +0 -228
- package/node_modules/@ftshare-lab/dsh-kline/tools/trend_lines.py +0 -106
- package/node_modules/@ftshare-lab/dsh-kline/tools/watchlist.py +0 -149
- package/node_modules/@ftshare-lab/dsh-kline/view/ft-logo.jpg +0 -0
- package/node_modules/@ftshare-lab/dsh-kline/view/vendor/klinecharts.min.js +0 -7
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
/** Browser-input snapshots. A handoff changes only source identity, never the chart. */
|
|
2
|
-
import { randomBytes } from 'node:crypto';
|
|
3
|
-
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import { drawingInventory, observationKey, sanitizeChartContext, text } from "./chart-observation.js";
|
|
6
|
-
import { sourceStatus } from "./chart-source-status.js";
|
|
7
|
-
import { normalizeAdjustment } from "./chart-adjustment.js";
|
|
8
|
-
import { fibonacciOverlayPoints } from "./fibonacci.js";
|
|
9
|
-
import { sanitizePolylinePoints } from "./labeled-polyline.js";
|
|
10
|
-
import { sanitizeChartAnnotation } from "./chart-annotations.js";
|
|
11
|
-
import { sanitizePriceSeriesCommand } from "./price-series.js";
|
|
12
|
-
import { sanitizePriceBandCommand } from "./price-bands.js";
|
|
13
|
-
import { candleSequenceHash } from "./candle-sequence.js";
|
|
14
|
-
import { sanitizePriceSticksCommand } from "./price-sticks.js";
|
|
15
|
-
const TOKEN = /^[A-Za-z0-9_-]{32}$/;
|
|
16
|
-
const AI = new Set(['DRAW_HORIZONTAL_LINE', 'DRAW_AGENT_TREND_LINE', 'DRAW_AGENT_CHANNEL', 'DRAW_AGENT_RECTANGLE', 'DRAW_AGENT_FIBONACCI', 'DRAW_AGENT_POLYLINE', 'DRAW_AGENT_ANNOTATION', 'DRAW_AGENT_SERIES_LINE', 'DRAW_AGENT_PRICE_BAND', 'DRAW_AGENT_PRICE_STICKS']);
|
|
17
|
-
const INTERVALS = ['minute', 'day', 'week', 'month', 'quarter', 'year'];
|
|
18
|
-
function finite(value, positive = false) {
|
|
19
|
-
if (typeof value !== 'number' || !Number.isFinite(value) || positive && value <= 0)
|
|
20
|
-
throw Error('invalid_source_number');
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
function point(raw) {
|
|
24
|
-
if (!Number.isSafeInteger(raw?.time) || raw.time <= 0)
|
|
25
|
-
throw Error('invalid_source_point');
|
|
26
|
-
return { time: raw.time, price: finite(raw.price, true) };
|
|
27
|
-
}
|
|
28
|
-
function color(raw) {
|
|
29
|
-
if (!['info', 'warning', 'success', 'danger'].includes(raw))
|
|
30
|
-
throw Error('invalid_source_color');
|
|
31
|
-
return raw;
|
|
32
|
-
}
|
|
33
|
-
function canonicalAI(state) {
|
|
34
|
-
return observationKey({ ...state, drawings: state.drawings.filter((d) => d.origin === 'ai') });
|
|
35
|
-
}
|
|
36
|
-
function sourceKey(state) {
|
|
37
|
-
// A source can have fewer candles than the new view. Its data key must
|
|
38
|
-
// not validate that new view's viewport indices against the old row count.
|
|
39
|
-
const s = sanitizeChartContext({ ...state, visible_range: { available: false }, selection: null });
|
|
40
|
-
s.adjust = normalizeAdjustment(s.adjust) ?? s.adjust;
|
|
41
|
-
return observationKey({ ...s, name: '', range_key: '', source_interval: '', ui_interval: '', display_volume_unit: '', active_page: '',
|
|
42
|
-
source_chart_session: null, visible_range: { available: false }, selection: null, selection_status: 'none', indicators: [], data_status: '',
|
|
43
|
-
data_provenance: null, drawings: s.drawings.filter((d) => d.origin === 'ai') });
|
|
44
|
-
}
|
|
45
|
-
/** Keep only renderer fields; never copy arbitrary metadata, URLs or provider secrets. */
|
|
46
|
-
export function sanitizeSourcePayload(raw, state) {
|
|
47
|
-
if (raw?.symbol !== state.symbol || raw?.interval !== state.interval || normalizeAdjustment(raw?.adjust) === null
|
|
48
|
-
|| normalizeAdjustment(raw?.adjust) !== normalizeAdjustment(state.adjust)
|
|
49
|
-
|| !Array.isArray(raw.chartCommands) || raw.chartCommands.length > 256)
|
|
50
|
-
throw Error('source_payload_mismatch');
|
|
51
|
-
let candleCount = 0;
|
|
52
|
-
const commands = raw.chartCommands.map((c) => {
|
|
53
|
-
const type = c.type;
|
|
54
|
-
if (type === 'SET_CANDLES') {
|
|
55
|
-
candleCount++;
|
|
56
|
-
const cleaned = sanitizeChartContext({ ...state, candles: c.rows });
|
|
57
|
-
if (JSON.stringify(cleaned.candles) !== JSON.stringify(state.candles))
|
|
58
|
-
throw Error('source_rows_mismatch');
|
|
59
|
-
return { type, rows: cleaned.candles };
|
|
60
|
-
}
|
|
61
|
-
if (type === 'SET_RANGE')
|
|
62
|
-
return { type, start: finite(c.start, true), end: finite(c.end, true) };
|
|
63
|
-
if (type === 'SET_INDICATOR_SERIES') {
|
|
64
|
-
if (!['ma', 'vwap', 'vol', 'macd', 'kdj', 'boll', 'rsi', 'atr'].includes(c.name)
|
|
65
|
-
|| !['candle', 'vol', 'macd', 'kdj', 'rsi', 'atr'].includes(c.pane)
|
|
66
|
-
|| !c.data || typeof c.data !== 'object' || Array.isArray(c.data) || Object.keys(c.data).length > 32)
|
|
67
|
-
throw Error('invalid_source_indicator');
|
|
68
|
-
const params = {};
|
|
69
|
-
for (const key of ['period', 'ma_period', 'fast', 'slow', 'signal', 'n', 'm1', 'm2', 'std_mult'])
|
|
70
|
-
if (c.params?.[key] != null)
|
|
71
|
-
params[key] = finite(c.params[key], true);
|
|
72
|
-
if (c.params?.periods) {
|
|
73
|
-
if (!Array.isArray(c.params.periods) || c.params.periods.length > 8)
|
|
74
|
-
throw Error('invalid_source_indicator');
|
|
75
|
-
params.periods = c.params.periods.map((p) => finite(p, true));
|
|
76
|
-
}
|
|
77
|
-
if (c.params?.anchor === 'loaded_context')
|
|
78
|
-
params.anchor = 'loaded_context';
|
|
79
|
-
const data = {};
|
|
80
|
-
for (const [key, values] of Object.entries(c.data)) {
|
|
81
|
-
if (!/^(?:ma\d{1,3}|vwap|vol_ma|dif|dea|hist|k|d|j|mid|upper|lower|rsi|atr)$/.test(key)
|
|
82
|
-
|| !values || typeof values !== 'object' || Array.isArray(values) || Object.keys(values).length > 12000)
|
|
83
|
-
throw Error('invalid_source_indicator');
|
|
84
|
-
data[key] = Object.fromEntries(Object.entries(values).map(([time, value]) => {
|
|
85
|
-
if (!/^[1-9]\d{0,12}$/.test(time))
|
|
86
|
-
throw Error('invalid_source_indicator_time');
|
|
87
|
-
return [time, finite(value)];
|
|
88
|
-
}));
|
|
89
|
-
}
|
|
90
|
-
return { type, name: c.name, pane: c.pane, params, data };
|
|
91
|
-
}
|
|
92
|
-
if (type === 'SET_COMPARISON_SERIES') {
|
|
93
|
-
if (!Array.isArray(c.series) || c.series.length > 3)
|
|
94
|
-
throw Error('invalid_source_comparison');
|
|
95
|
-
return { type, series: c.series.map((s) => ({ symbol: text(s.symbol, 64), name: text(s.name ?? '', 128),
|
|
96
|
-
rows: sanitizeChartContext({ ...state, candles: s.rows }).candles })) };
|
|
97
|
-
}
|
|
98
|
-
if (type === 'DRAW_AGENT_ANNOTATION') {
|
|
99
|
-
const annotation = sanitizeChartAnnotation(c);
|
|
100
|
-
if (annotation.interval !== state.interval)
|
|
101
|
-
throw Error('invalid_source_interval');
|
|
102
|
-
return annotation;
|
|
103
|
-
}
|
|
104
|
-
if (type === 'DRAW_AGENT_SERIES_LINE') {
|
|
105
|
-
const clean = sanitizePriceSeriesCommand(c, state.candles.length, state.interval);
|
|
106
|
-
if (clean.interval === state.interval && clean.row_hash && clean.row_hash !== candleSequenceHash(state.candles))
|
|
107
|
-
throw Error('source_sequence_rows_mismatch');
|
|
108
|
-
return clean;
|
|
109
|
-
}
|
|
110
|
-
if (type === 'DRAW_AGENT_PRICE_BAND') {
|
|
111
|
-
const clean = sanitizePriceBandCommand(c, state.candles.length, state.interval);
|
|
112
|
-
if (clean.interval === state.interval && clean.row_hash && clean.row_hash !== candleSequenceHash(state.candles))
|
|
113
|
-
throw Error('source_sequence_rows_mismatch');
|
|
114
|
-
return clean;
|
|
115
|
-
}
|
|
116
|
-
if (type === 'DRAW_AGENT_PRICE_STICKS') {
|
|
117
|
-
const clean = sanitizePriceSticksCommand(c, state.candles.length, state.interval);
|
|
118
|
-
if (clean.interval === state.interval && clean.row_hash && clean.row_hash !== candleSequenceHash(state.candles))
|
|
119
|
-
throw Error('source_sequence_rows_mismatch');
|
|
120
|
-
return clean;
|
|
121
|
-
}
|
|
122
|
-
if (type === 'TEXT_MARKER')
|
|
123
|
-
return { type, id: text(c.id ?? '', 256), text: text(c.text ?? '', 256), ...point(c), color: color(c.color ?? 'info') };
|
|
124
|
-
if (type === 'DRAW_TREND_LINE')
|
|
125
|
-
return { type, id: text(c.id ?? '', 256), label: text(c.label ?? '', 128),
|
|
126
|
-
from: point(c.from), to: point(c.to), color: color(c.color ?? 'info') };
|
|
127
|
-
if (!AI.has(type))
|
|
128
|
-
throw Error('unsupported_source_command');
|
|
129
|
-
const result = { type, id: text(c.id, 256), label: text(c.label ?? '', 48), color: color(c.color ?? 'info') };
|
|
130
|
-
if (c.interval != null) {
|
|
131
|
-
if (!INTERVALS.includes(c.interval))
|
|
132
|
-
throw Error('invalid_source_interval');
|
|
133
|
-
result.interval = c.interval;
|
|
134
|
-
}
|
|
135
|
-
if (type === 'DRAW_HORIZONTAL_LINE')
|
|
136
|
-
result.price = finite(c.price, true);
|
|
137
|
-
else if (type === 'DRAW_AGENT_RECTANGLE') {
|
|
138
|
-
result.start_time = point({ time: c.start_time, price: c.lower_price }).time;
|
|
139
|
-
result.end_time = point({ time: c.end_time, price: c.upper_price }).time;
|
|
140
|
-
result.lower_price = finite(c.lower_price, true);
|
|
141
|
-
result.upper_price = finite(c.upper_price, true);
|
|
142
|
-
if (typeof c.show_fill !== 'boolean')
|
|
143
|
-
throw Error('invalid_source_style');
|
|
144
|
-
result.show_fill = c.show_fill;
|
|
145
|
-
}
|
|
146
|
-
else if (type === 'DRAW_AGENT_POLYLINE') {
|
|
147
|
-
if (!result.interval)
|
|
148
|
-
throw Error('invalid_source_interval');
|
|
149
|
-
result.points = sanitizePolylinePoints(c.points);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
result.from = point(c.from);
|
|
153
|
-
result.to = point(c.to);
|
|
154
|
-
if (type === 'DRAW_AGENT_FIBONACCI') {
|
|
155
|
-
fibonacciOverlayPoints(c);
|
|
156
|
-
if (!result.interval)
|
|
157
|
-
throw Error('invalid_source_interval');
|
|
158
|
-
Object.assign(result, { ratios: [...c.ratios], levels: c.levels.map((p) => ({ ratio: p.ratio, price: p.price })),
|
|
159
|
-
geometry_basis: 'linear_price_retracement' });
|
|
160
|
-
}
|
|
161
|
-
if (type === 'DRAW_AGENT_CHANNEL') {
|
|
162
|
-
result.parallel = point(c.parallel);
|
|
163
|
-
for (const edge of ['upper', 'lower'])
|
|
164
|
-
result[edge] = { from: point(c[edge]?.from), to: point(c[edge]?.to) };
|
|
165
|
-
for (const key of ['width', 'signed_offset', 'slope_per_bar'])
|
|
166
|
-
result[key] = finite(c[key], key === 'width');
|
|
167
|
-
if (typeof c.show_midline !== 'boolean' || !['ascending', 'descending', 'horizontal'].includes(c.direction)
|
|
168
|
-
|| c.geometry_basis !== 'candle_index_linear_price')
|
|
169
|
-
throw Error('invalid_source_channel');
|
|
170
|
-
Object.assign(result, { show_midline: c.show_midline, direction: c.direction, geometry_basis: c.geometry_basis });
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
return result;
|
|
174
|
-
});
|
|
175
|
-
if (candleCount !== 1)
|
|
176
|
-
throw Error('invalid_source_candles');
|
|
177
|
-
const meta = { source: state.data_provenance.provider, status: state.data_status };
|
|
178
|
-
const zone = state.data_provenance.aggregation?.timezone ?? raw.data_meta?.exchange_timezone;
|
|
179
|
-
if (typeof zone === 'string' && zone.length <= 64 && /^[A-Za-z_+-]+(?:\/[A-Za-z0-9_+-]+){0,2}$/.test(zone)) {
|
|
180
|
-
try {
|
|
181
|
-
new Intl.DateTimeFormat('en', { timeZone: zone });
|
|
182
|
-
meta.exchange_timezone = zone;
|
|
183
|
-
}
|
|
184
|
-
catch { }
|
|
185
|
-
}
|
|
186
|
-
if (typeof raw.data_meta?.updated_at === 'number' && Number.isFinite(raw.data_meta.updated_at) && raw.data_meta.updated_at > 0)
|
|
187
|
-
meta.updated_at = raw.data_meta.updated_at;
|
|
188
|
-
const payload = { symbol: state.symbol, name: state.name, interval: state.interval, interval_value: state.interval_value,
|
|
189
|
-
adjust: state.adjust, range_key: state.range_key, count: state.candles.length, indicators: state.indicators,
|
|
190
|
-
indicators_explicit: true, data_meta: meta, chartCommands: commands };
|
|
191
|
-
if (canonicalAI({ ...state, drawings: drawingInventory(payload) }) !== canonicalAI(state))
|
|
192
|
-
throw Error('source_inventory_mismatch');
|
|
193
|
-
return payload;
|
|
194
|
-
}
|
|
195
|
-
export class ChartSources {
|
|
196
|
-
offers = new Map();
|
|
197
|
-
runtime;
|
|
198
|
-
now;
|
|
199
|
-
constructor(runtime, now = () => Date.now()) { this.runtime = runtime; this.now = now; }
|
|
200
|
-
proofPath(ref, token) {
|
|
201
|
-
if (!TOKEN.test(ref) || !TOKEN.test(token))
|
|
202
|
-
throw Error('invalid_source_identity');
|
|
203
|
-
return join(this.runtime, 'sources', String(process.pid), ref, token + '.json');
|
|
204
|
-
}
|
|
205
|
-
async read(path) {
|
|
206
|
-
const data = await readFile(path, 'utf8');
|
|
207
|
-
if (data.length > 8 * 1024 * 1024)
|
|
208
|
-
throw Error('invalid_source_size');
|
|
209
|
-
return JSON.parse(data);
|
|
210
|
-
}
|
|
211
|
-
async write(path, value) {
|
|
212
|
-
await mkdir(join(path, '..'), { recursive: true, mode: 0o700 });
|
|
213
|
-
const tmp = path + '.' + randomBytes(8).toString('hex') + '.tmp';
|
|
214
|
-
await writeFile(tmp, JSON.stringify(value), { mode: 0o600, flag: 'wx' });
|
|
215
|
-
await rename(tmp, path);
|
|
216
|
-
}
|
|
217
|
-
async acknowledge(view, receipt) {
|
|
218
|
-
const offer = this.offers.get(view.ref);
|
|
219
|
-
if (!TOKEN.test(receipt?.chart_session ?? '') || !['bound', 'rejected'].includes(receipt.status))
|
|
220
|
-
throw Error('invalid_source_receipt');
|
|
221
|
-
// A view can expire/remount while a receipt is in flight. Reject and let
|
|
222
|
-
// that browser restore its prior identity; never leave publication stuck.
|
|
223
|
-
if (!offer || receipt.chart_session !== offer.chart_session)
|
|
224
|
-
return { chart_session: receipt.chart_session, status: 'rejected' };
|
|
225
|
-
if (view.conversation !== offer.conversation_id || view.view !== offer.view_id)
|
|
226
|
-
return { chart_session: offer.chart_session, status: 'rejected' };
|
|
227
|
-
if (offer.status !== 'offered')
|
|
228
|
-
return { chart_session: offer.chart_session, status: offer.status };
|
|
229
|
-
const expected = { ...offer.base_state, source_chart_session: offer.chart_session };
|
|
230
|
-
offer.status = receipt.status === 'bound' && view.active && !view.ambiguous && this.now() <= offer.expires_at_ms
|
|
231
|
-
&& view.conversation === offer.conversation_id && view.view === offer.view_id
|
|
232
|
-
&& observationKey(view.state) === observationKey(expected) ? 'confirmed' : 'rejected';
|
|
233
|
-
await this.write(this.proofPath(view.ref, offer.chart_session), { host_process_id: process.pid, context_ref: view.ref,
|
|
234
|
-
conversation_id: offer.conversation_id, view_id: offer.view_id, chart_session: offer.chart_session, status: offer.status,
|
|
235
|
-
acknowledged_at_ms: this.now() });
|
|
236
|
-
return { chart_session: offer.chart_session, status: offer.status };
|
|
237
|
-
}
|
|
238
|
-
async offer(view, raw) {
|
|
239
|
-
return (await this.assess(view, raw)).handoff;
|
|
240
|
-
}
|
|
241
|
-
async assess(view, raw) {
|
|
242
|
-
const result = (reason, handoff) => ({ status: sourceStatus(reason), handoff });
|
|
243
|
-
for (const [ref, item] of this.offers)
|
|
244
|
-
if (this.now() > item.expires_at_ms + 300000)
|
|
245
|
-
this.offers.delete(ref);
|
|
246
|
-
if (!view.active)
|
|
247
|
-
return result('view_inactive');
|
|
248
|
-
if (view.ambiguous)
|
|
249
|
-
return result('ambiguous_views');
|
|
250
|
-
if (!view.applyCapable)
|
|
251
|
-
return result('client_unsupported');
|
|
252
|
-
if (view.state.state !== 'ready' || !view.state.candles.length || !view.state.symbol)
|
|
253
|
-
return result('chart_not_ready');
|
|
254
|
-
if (view.state.candles.length > 4000)
|
|
255
|
-
return result('source_limit_exceeded');
|
|
256
|
-
if (!INTERVALS.includes(view.state.interval) || normalizeAdjustment(view.state.adjust) === null
|
|
257
|
-
|| view.state.interval === 'minute' && view.state.interval_value === null)
|
|
258
|
-
return result('source_metadata_unknown');
|
|
259
|
-
// A pending drawing must finish or fail explicitly before its source can change.
|
|
260
|
-
let pending;
|
|
261
|
-
try {
|
|
262
|
-
pending = await this.read(join(this.runtime, 'edits', String(process.pid), view.ref, 'pending.json'));
|
|
263
|
-
}
|
|
264
|
-
catch (error) {
|
|
265
|
-
if (error?.code !== 'ENOENT')
|
|
266
|
-
throw error;
|
|
267
|
-
}
|
|
268
|
-
if (pending) {
|
|
269
|
-
if (!TOKEN.test(pending.candidate_chart_session ?? ''))
|
|
270
|
-
return result('drawing_outcome_unknown');
|
|
271
|
-
let app;
|
|
272
|
-
try {
|
|
273
|
-
app = await this.read(join(this.runtime, 'edits', String(process.pid), view.ref, 'applications', pending.candidate_chart_session + '.json'));
|
|
274
|
-
}
|
|
275
|
-
catch {
|
|
276
|
-
return result('drawing_outcome_unknown');
|
|
277
|
-
}
|
|
278
|
-
if (!['applied', 'rejected', 'failed'].includes(app.status))
|
|
279
|
-
return result(['queued', 'delivered'].includes(app.status) ? 'drawing_pending' : 'drawing_outcome_unknown');
|
|
280
|
-
}
|
|
281
|
-
const parent = view.state.source_chart_session;
|
|
282
|
-
const hasParent = TOKEN.test(parent ?? '');
|
|
283
|
-
// A chart opened by the browser's symbol search has no Agent-created
|
|
284
|
-
// parent. Bootstrap only from this locally rendered chart; an initial
|
|
285
|
-
// source must not inherit any unverified AI object.
|
|
286
|
-
if (!hasParent && raw?.local_render !== true)
|
|
287
|
-
return result('source_missing');
|
|
288
|
-
let source = null;
|
|
289
|
-
if (hasParent) {
|
|
290
|
-
const saved = await this.read(join(this.runtime, 'sessions', parent + '.json'));
|
|
291
|
-
if (saved.host_process_id !== process.pid || saved.session !== parent || saved.ok !== true)
|
|
292
|
-
throw Error('source_parent_unavailable');
|
|
293
|
-
source = saved.payload;
|
|
294
|
-
}
|
|
295
|
-
if (source?.drawing_guard) {
|
|
296
|
-
const g = source.drawing_guard;
|
|
297
|
-
const app = await this.read(join(this.runtime, 'edits', String(process.pid), g.context_ref, 'applications', parent + '.json'));
|
|
298
|
-
if (app.status !== 'applied' || app.confirmed_chart_session !== parent || app.conversation_id !== view.conversation
|
|
299
|
-
|| app.host_process_id !== process.pid || app.view_id !== g.view_id || app.request_id !== g.request_id
|
|
300
|
-
|| g.conversation_id !== view.conversation)
|
|
301
|
-
throw Error('unconfirmed_source_parent');
|
|
302
|
-
}
|
|
303
|
-
if (source?.view_source_guard) {
|
|
304
|
-
const g = source.view_source_guard, proof = await this.read(this.proofPath(g.context_ref, parent));
|
|
305
|
-
if (proof.status !== 'confirmed' || proof.conversation_id !== view.conversation || proof.host_process_id !== process.pid
|
|
306
|
-
|| proof.chart_session !== parent || g.chart_session !== parent || proof.view_id !== g.view_id
|
|
307
|
-
|| g.conversation_id !== view.conversation)
|
|
308
|
-
throw Error('unconfirmed_source_parent');
|
|
309
|
-
}
|
|
310
|
-
const rows = source?.chartCommands?.find((c) => c.type === 'SET_CANDLES')?.rows;
|
|
311
|
-
if (source?.symbol === view.state.symbol && Array.isArray(rows)) {
|
|
312
|
-
const original = { ...view.state, symbol: source.symbol, interval: source.interval, interval_value: source.interval_value ?? null,
|
|
313
|
-
adjust: source.adjust ?? '', candles: rows, drawings: drawingInventory(source) };
|
|
314
|
-
if (sourceKey(original) === sourceKey(view.state))
|
|
315
|
-
return result('source_matches_view');
|
|
316
|
-
}
|
|
317
|
-
const previous = this.offers.get(view.ref);
|
|
318
|
-
if (previous?.status === 'offered' && observationKey(previous.base_state) === observationKey(view.state)
|
|
319
|
-
&& this.now() <= previous.expires_at_ms)
|
|
320
|
-
return result('source_handoff_pending', previous);
|
|
321
|
-
if (!raw)
|
|
322
|
-
return result(hasParent ? 'source_snapshot_mismatch' : 'source_missing');
|
|
323
|
-
const payload = sanitizeSourcePayload(raw, view.state), token = randomBytes(24).toString('base64url');
|
|
324
|
-
const ai = payload.chartCommands.filter((c) => AI.has(c.type));
|
|
325
|
-
const old = source?.symbol === view.state.symbol ? source.chartCommands.filter((c) => AI.has(c.type)) : [];
|
|
326
|
-
if (ai.length !== old.length || new Set(ai.map((c) => c.id)).size !== ai.length)
|
|
327
|
-
throw Error('source_inventory_mismatch');
|
|
328
|
-
// The handoff may change rows/period, never existing AI definitions or geometry.
|
|
329
|
-
const ordered = (x) => Array.isArray(x) ? x.map(ordered) : x && typeof x === 'object'
|
|
330
|
-
? Object.fromEntries(Object.keys(x).sort().map(k => [k, ordered(x[k])])) : x;
|
|
331
|
-
for (const c of ai) {
|
|
332
|
-
const parent = old.find((p) => p.id === c.id && p.type === c.type);
|
|
333
|
-
if (!parent || !c.id || Object.keys(c).some(k => JSON.stringify(ordered(c[k])) !== JSON.stringify(ordered(parent[k]))))
|
|
334
|
-
throw Error('source_ai_changed');
|
|
335
|
-
}
|
|
336
|
-
payload.view_source_guard = { protocol: 'browser-source-v1', context_ref: view.ref, conversation_id: view.conversation,
|
|
337
|
-
view_id: view.view, expected_revision: view.revision, chart_session: token };
|
|
338
|
-
if (hasParent)
|
|
339
|
-
payload.drawing_parent_session = parent;
|
|
340
|
-
await this.write(join(this.runtime, 'sessions', token + '.json'), { ok: true, session: token, host_process_id: process.pid,
|
|
341
|
-
symbol: payload.symbol, name: payload.name, published_at: Math.floor(this.now() / 1000), payload });
|
|
342
|
-
const offer = { chart_session: token, context_ref: view.ref, conversation_id: view.conversation, view_id: view.view,
|
|
343
|
-
expected_revision: view.revision, base_state: view.state, expires_at_ms: this.now() + 30000, status: 'offered' };
|
|
344
|
-
await this.write(this.proofPath(view.ref, token), { host_process_id: process.pid, context_ref: view.ref,
|
|
345
|
-
conversation_id: view.conversation, view_id: view.view, chart_session: token, status: 'offered' });
|
|
346
|
-
this.offers.set(view.ref, offer);
|
|
347
|
-
return result('source_handoff_pending', offer);
|
|
348
|
-
}
|
|
349
|
-
}
|