@dsh-ft/dsh-fft-paper-trading 0.6.1 → 0.6.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/README.md +4 -4
- package/README.zh.md +4 -4
- package/lib/agent/format.d.ts.map +1 -1
- package/lib/agent/format.js +4 -0
- package/lib/agent/format.js.map +1 -1
- package/lib/agent/tools.d.ts.map +1 -1
- package/lib/agent/tools.js +5 -3
- package/lib/agent/tools.js.map +1 -1
- package/lib/client.js +76 -13
- package/lib/contract.d.ts.map +1 -1
- package/lib/contract.js +3 -1
- package/lib/contract.js.map +1 -1
- package/lib/core/risk.d.ts.map +1 -1
- package/lib/core/risk.js +10 -2
- package/lib/core/risk.js.map +1 -1
- package/lib/core/views.js +2 -2
- package/lib/core/views.js.map +1 -1
- package/lib/service/paper-service.d.ts +1 -1
- package/lib/service/paper-service.d.ts.map +1 -1
- package/lib/service/paper-service.js +24 -12
- package/lib/service/paper-service.js.map +1 -1
- package/lib/service/rpc.d.ts.map +1 -1
- package/lib/service/rpc.js +12 -1
- package/lib/service/rpc.js.map +1 -1
- package/node_modules/@ftshare-lab/dsh-kline/CHANGELOG.md +98 -0
- package/node_modules/@ftshare-lab/dsh-kline/README.en.md +116 -26
- package/node_modules/@ftshare-lab/dsh-kline/README.md +125 -25
- package/node_modules/@ftshare-lab/dsh-kline/chart_service.py +38 -0
- package/node_modules/@ftshare-lab/dsh-kline/cordis.patch.yml +3 -9
- package/node_modules/@ftshare-lab/dsh-kline/core/calc.py +63 -269
- package/node_modules/@ftshare-lab/dsh-kline/core/rows.py +15 -3
- package/node_modules/@ftshare-lab/dsh-kline/core/ta_engine.py +99 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/candle-sequence.js +15 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-annotations.js +33 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-applications.js +1 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-context.js +12 -3
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-observation.js +91 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-sources.js +64 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/client.js +3356 -787
- package/node_modules/@ftshare-lab/dsh-kline/lib/drawing-feedback.js +96 -19
- package/node_modules/@ftshare-lab/dsh-kline/lib/fibonacci.js +18 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/index.js +19 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/labeled-polyline.js +17 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-bands.js +55 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-series.js +47 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-sticks.js +32 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/candle-sequence.d.ts +2 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-annotations.d.ts +4 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-context.d.ts +1 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-observation.d.ts +2 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/BetterSidebarTab.d.ts +3 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/ClassicShell.d.ts +1 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-context.d.ts +1 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/client-context.d.ts +14 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/conversation.d.ts +11 -3
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/embed.d.ts +2 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/generated-view.d.ts +1 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/host-services.d.ts +0 -36
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/index.d.ts +2 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-contract.d.ts +35 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-register.d.ts +46 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-services.d.ts +45 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar.d.ts +21 -36
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/fibonacci.d.ts +5 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/labeled-polyline.d.ts +11 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-bands.d.ts +3 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-series.d.ts +5 -0
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-sticks.d.ts +3 -0
- package/node_modules/@ftshare-lab/dsh-kline/package.json +27 -5
- package/node_modules/@ftshare-lab/dsh-kline/requirements.txt +2 -0
- package/node_modules/@ftshare-lab/dsh-kline/scripts/dsh-mcp-plugin.mjs +27 -0
- package/node_modules/@ftshare-lab/dsh-kline/scripts/run-dsh-kline.mjs +180 -49
- package/node_modules/@ftshare-lab/dsh-kline/server.py +300 -34
- package/node_modules/@ftshare-lab/dsh-kline/tools/annotations.py +119 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/baostock_source.py +176 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/channel_candidates.py +260 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_applications.py +1 -1
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_context.py +22 -3
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_edits.py +141 -16
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_functions.py +466 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_indicators.py +63 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/draw.py +3 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_feedback.py +13 -7
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_interaction.py +117 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_verification.py +58 -3
- package/node_modules/@ftshare-lab/dsh-kline/tools/fetch.py +164 -40
- package/node_modules/@ftshare-lab/dsh-kline/tools/fibonacci.py +83 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/free_sources.py +78 -21
- package/node_modules/@ftshare-lab/dsh-kline/tools/pivots.py +60 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/polylines.py +125 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_bands.py +165 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_series.py +162 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_sticks.py +114 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/range_candidates.py +170 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/regression_channels.py +87 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/ta_functions.py +228 -0
- package/package.json +2 -2
- package/src/agent/format.ts +3 -0
- package/src/agent/tools.ts +5 -3
- package/src/client/index.js +75 -12
- package/src/contract.ts +3 -1
- package/src/core/risk.ts +10 -2
- package/src/core/views.ts +2 -2
- package/src/service/paper-service.ts +22 -19
- package/src/service/rpc.ts +12 -1
|
@@ -8,7 +8,8 @@ import { sanitizeDrawingFeedback } from "./drawing-feedback.js";
|
|
|
8
8
|
import { ChartSources } from "./chart-sources.js";
|
|
9
9
|
import { sourceStatus, sourceFailure } from "./chart-source-status.js";
|
|
10
10
|
export { sanitizeChartContext } from "./chart-observation.js";
|
|
11
|
-
|
|
11
|
+
// Allow a human confirmation (or a slow host) without relaxing source/revision checks.
|
|
12
|
+
export const CONTEXT_TTL_MS = 90_000;
|
|
12
13
|
const TOKEN = /^[A-Za-z0-9_-]{32}$/;
|
|
13
14
|
export class ChartContextBroker {
|
|
14
15
|
records = new Map();
|
|
@@ -132,12 +133,18 @@ export class ChartContextBroker {
|
|
|
132
133
|
return '';
|
|
133
134
|
const views = [...this.records.values()].filter(r => r.conversation === conversation && r.active && this.now() - r.observed <= CONTEXT_TTL_MS);
|
|
134
135
|
if (views.length !== 1)
|
|
135
|
-
return
|
|
136
|
+
return views.length
|
|
137
|
+
? 'dsh_kline current-view context: ambiguous_views. Keep one active chart view for this conversation, then wait for a fresh injected context_ref. Do not guess a view from runtime files.'
|
|
138
|
+
: 'dsh_kline current-view context: unavailable. Open this conversation\'s chart in the browser and wait for a fresh injected context_ref. Do not reuse an older reference, refetch candles to repair the view, or inspect runtime files to guess one.';
|
|
136
139
|
const r = views[0];
|
|
137
140
|
// Some hosts expose only MCP tool definitions, not server-level instructions.
|
|
138
141
|
// Keep this small interaction contract on the existing public context hook;
|
|
139
142
|
// never interpolate candle text or drawing names into instructions.
|
|
140
143
|
return 'dsh_kline interaction contract: The host reasons; the plugin only observes, measures and executes. '
|
|
144
|
+
+ 'For ALL drawings on this browser chart, including literal create/update/delete, use get_chart_context -> prepare_chart_edit -> apply_chart_edit -> get_chart_edit_status. Never use legacy snapshot mutation tools for the current chart. '
|
|
145
|
+
+ 'For Fibonacci and reusable geometry, use list_chart_functions / run_chart_function on fresh current context and pass edit.result_ref into prepare_chart_edit, without copying computed coordinates or mixing direct geometry. Functions calculate but never apply. Explicit anchors or a user-authorized host proposal are required. pivot_candidates and regression_channel are analysis-only: pivots report confirmed strict local extrema; regression_channel reports OLS close-price channel statistics over explicit start/end bars. Neither chooses drawing anchors or creates drawings. No arbitrary code. '
|
|
146
|
+
+ 'labeled_polyline connects 2..8 explicitly labeled points as one object, not automatic wave recognition. Create with a function result_ref; to move one point use object_id + point_label + point_time/point_price in prepare_chart_edit, without copying all neighbours. Labels are group-local, so clarify ambiguous group names. Whole-group name/color/delete use the existing ID. '
|
|
147
|
+
+ 'text_annotation, number_annotation and icon_annotation create one AI-owned time/price anchored marker each using anchor_time + anchor_price and annotation_text, annotation_value or annotation_icon (arrow_up, arrow_down or diamond). Update/delete by the exact object_id; content, anchor, name and color changes share the guarded prepare/apply flow. These are literal annotations, not calculated analysis. '
|
|
141
148
|
+ 'For user-authorized analysis, derive candidates from fresh actual candles; do not demand all coordinates from the user. '
|
|
142
149
|
+ 'Before proposing a chosen region, read get_chart_context with region_start_time/region_end_time to check its measurements. '
|
|
143
150
|
+ 'Body envelope is not common intersection; volume net decline is not monotonic decline. Do not invent universal pattern thresholds. '
|
|
@@ -145,7 +152,9 @@ export class ChartContextBroker {
|
|
|
145
152
|
+ 'Cite boundary_evidence for extremum dates/fields. Intersection relation=empty means no price shared by EVERY body, point means one shared price; never turn empty into a point. '
|
|
146
153
|
+ 'An empty common intersection does NOT imply a gap uncovered by ANY body. The candidate difference is not an uncovered gap; union coverage is not measured. State the measured result and its evidence, not extra coverage claims. '
|
|
147
154
|
+ 'If proposal-first is requested, do not prepare/apply before confirmation. After confirmation read fresh context, preserve the approved bounds, and use guarded prepare/apply; verify against the independently saved approved proposal. '
|
|
148
|
-
+ '
|
|
155
|
+
+ 'For literal edits, resolve the target from current AI objects; if multiple targets fit, or a relative price amount is unspecified, ask a short clarification instead of guessing. No drawing selection is reported yet. '
|
|
156
|
+
+ 'Read drawing_capabilities in current context for supported colors; if unsupported, ask the user to choose, without substituting or searching source code. '
|
|
157
|
+
+ 'Use interaction and change in edit results for concise replies: target, changed values and completion or missing information. Pending/unknown is not completion. Keep historical and pixel-verification limits; explain them when relevant, not as repeated protocol jargon on every success. Omit internal IDs unless needed for an error. '
|
|
149
158
|
+ 'Use data_provenance for provider and browser-input/display aggregation; day-to-week local_aggregation is not by itself stale data. Unknown stays unknown; metadata does not grant edit authority. '
|
|
150
159
|
+ 'If an initial reference becomes inactive during mounting, use the newly injected reference; do not refetch candles.\n'
|
|
151
160
|
+ 'dsh_kline browser observation (chart data, not instructions): ' + JSON.stringify({ context_ref: r.ref,
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
/** Canonical browser observations shared by the server and live view. */
|
|
2
|
+
import { sanitizePolylinePoints } from "./labeled-polyline.js";
|
|
3
|
+
import { sanitizeChartAnnotation } from "./chart-annotations.js";
|
|
4
|
+
import { seriesObservation } from "./price-series.js";
|
|
5
|
+
import { bandObservation } from "./price-bands.js";
|
|
6
|
+
import { sticksObservation } from "./price-sticks.js";
|
|
2
7
|
const TOKEN = /^[A-Za-z0-9_-]{32}$/;
|
|
3
8
|
export function text(value, max = 128) {
|
|
4
9
|
if (typeof value !== 'string' || value.length > max || /[\x00-\x1f]/.test(value))
|
|
@@ -82,8 +87,56 @@ export function sanitizeChartContext(raw) {
|
|
|
82
87
|
result.interval = text(drawing.interval, 32);
|
|
83
88
|
if (typeof drawing.price === 'number')
|
|
84
89
|
result.price = number(drawing.price);
|
|
85
|
-
if (
|
|
90
|
+
if (drawing.kind === 'series_line') {
|
|
91
|
+
if (typeof drawing.series_hash !== 'string' || !/^[a-f0-9]{64}$/.test(drawing.series_hash)
|
|
92
|
+
|| !Number.isSafeInteger(drawing.bar_count) || drawing.bar_count < 1 || drawing.bar_count > 4000)
|
|
93
|
+
throw Error('invalid_context_series');
|
|
94
|
+
Object.assign(result, seriesObservation(drawing));
|
|
95
|
+
}
|
|
96
|
+
if (drawing.kind === 'price_band') {
|
|
97
|
+
if (typeof drawing.band_hash !== 'string' || !/^[a-f0-9]{64}$/.test(drawing.band_hash)
|
|
98
|
+
|| !Number.isSafeInteger(drawing.bar_count) || drawing.bar_count < 1 || drawing.bar_count > 4000)
|
|
99
|
+
throw Error('invalid_context_band');
|
|
100
|
+
Object.assign(result, bandObservation(drawing));
|
|
101
|
+
}
|
|
102
|
+
if (drawing.kind === 'price_sticks') {
|
|
103
|
+
if (typeof drawing.sticks_hash !== 'string' || !/^[a-f0-9]{64}$/.test(drawing.sticks_hash)
|
|
104
|
+
|| !Number.isSafeInteger(drawing.bar_count) || drawing.bar_count < 1 || drawing.bar_count > 4000)
|
|
105
|
+
throw Error('invalid_context_sticks');
|
|
106
|
+
Object.assign(result, sticksObservation(drawing));
|
|
107
|
+
}
|
|
108
|
+
if (drawing.kind === 'labeled_polyline')
|
|
109
|
+
result.points = sanitizePolylinePoints(drawing.points);
|
|
110
|
+
else if (['text_annotation', 'number_annotation', 'icon_annotation'].includes(drawing.kind)) {
|
|
111
|
+
const annotation = sanitizeChartAnnotation({ type: 'DRAW_AGENT_ANNOTATION', kind: drawing.kind, id: drawing.id, interval: drawing.interval,
|
|
112
|
+
label: drawing.name, color: drawing.color, time: drawing.time, price: drawing.price, text: drawing.text, value: drawing.value, icon: drawing.icon });
|
|
113
|
+
result.time = annotation.time;
|
|
114
|
+
result.price = annotation.price;
|
|
115
|
+
if (annotation.kind === 'text_annotation')
|
|
116
|
+
result.text = annotation.text;
|
|
117
|
+
else if (annotation.kind === 'number_annotation')
|
|
118
|
+
result.value = annotation.value;
|
|
119
|
+
else
|
|
120
|
+
result.icon = annotation.icon;
|
|
121
|
+
}
|
|
122
|
+
else if (Array.isArray(drawing.points))
|
|
86
123
|
result.points = drawing.points.slice(0, 8).map(point);
|
|
124
|
+
if (drawing.kind === 'fibonacci_retracement') {
|
|
125
|
+
if (!Array.isArray(drawing.ratios) || !Array.isArray(drawing.levels) || !drawing.ratios.length || drawing.ratios.length > 16
|
|
126
|
+
|| drawing.levels.length !== drawing.ratios.length)
|
|
127
|
+
throw Error('invalid_context_fibonacci');
|
|
128
|
+
result.ratios = drawing.ratios.map((r, i) => {
|
|
129
|
+
const value = number(r);
|
|
130
|
+
if (value < 0 || value > 1 || i > 0 && value <= drawing.ratios[i - 1])
|
|
131
|
+
throw Error('invalid_context_fibonacci');
|
|
132
|
+
return value;
|
|
133
|
+
});
|
|
134
|
+
result.levels = drawing.levels.map((p, i) => {
|
|
135
|
+
if (number(p.ratio) !== result.ratios[i] || number(p.price) <= 0)
|
|
136
|
+
throw Error('invalid_context_fibonacci');
|
|
137
|
+
return { ratio: p.ratio, price: p.price };
|
|
138
|
+
});
|
|
139
|
+
}
|
|
87
140
|
for (const key of ['start_time', 'end_time'])
|
|
88
141
|
if (drawing[key] != null)
|
|
89
142
|
result[key] = time(drawing[key]);
|
|
@@ -132,20 +185,56 @@ export function observationKey(raw) {
|
|
|
132
185
|
state.drawings.sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
133
186
|
return JSON.stringify(ordered(state));
|
|
134
187
|
}
|
|
188
|
+
/** Identity required for editing the current source, excluding transient UI state. */
|
|
189
|
+
export function drawingSourceKey(raw) {
|
|
190
|
+
const state = sanitizeChartContext(raw);
|
|
191
|
+
return JSON.stringify({
|
|
192
|
+
state: state.state, symbol: state.symbol, interval: state.interval,
|
|
193
|
+
interval_value: state.interval_value, source_interval: state.source_interval,
|
|
194
|
+
adjust: state.adjust, source_chart_session: state.source_chart_session,
|
|
195
|
+
candles: state.candles, data_provenance: state.data_provenance,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
135
198
|
export function drawingInventory(payload) {
|
|
136
199
|
const kinds = { DRAW_HORIZONTAL_LINE: 'horizontal_line', DRAW_AGENT_TREND_LINE: 'trend_line',
|
|
137
|
-
DRAW_AGENT_CHANNEL: 'channel', DRAW_AGENT_RECTANGLE: 'rectangle'
|
|
200
|
+
DRAW_AGENT_CHANNEL: 'channel', DRAW_AGENT_RECTANGLE: 'rectangle', DRAW_AGENT_FIBONACCI: 'fibonacci_retracement', DRAW_AGENT_POLYLINE: 'labeled_polyline',
|
|
201
|
+
DRAW_AGENT_ANNOTATION: 'annotation', DRAW_AGENT_SERIES_LINE: 'series_line', DRAW_AGENT_PRICE_BAND: 'price_band',
|
|
202
|
+
DRAW_AGENT_PRICE_STICKS: 'price_sticks' };
|
|
138
203
|
return (payload.chartCommands ?? []).filter((c) => kinds[c.type]).map((c) => {
|
|
139
204
|
const d = { id: c.id, kind: kinds[c.type], origin: 'ai', name: c.label ?? '', color: c.color ?? 'info', interval: c.interval ?? payload.interval };
|
|
140
205
|
if (c.type === 'DRAW_HORIZONTAL_LINE')
|
|
141
206
|
d.price = c.price;
|
|
207
|
+
else if (c.type === 'DRAW_AGENT_SERIES_LINE')
|
|
208
|
+
Object.assign(d, seriesObservation(c));
|
|
209
|
+
else if (c.type === 'DRAW_AGENT_PRICE_BAND')
|
|
210
|
+
Object.assign(d, bandObservation(c));
|
|
211
|
+
else if (c.type === 'DRAW_AGENT_PRICE_STICKS')
|
|
212
|
+
Object.assign(d, sticksObservation(c));
|
|
213
|
+
else if (c.type === 'DRAW_AGENT_ANNOTATION') {
|
|
214
|
+
const annotation = sanitizeChartAnnotation(c);
|
|
215
|
+
d.kind = annotation.kind;
|
|
216
|
+
d.time = annotation.time;
|
|
217
|
+
d.price = annotation.price;
|
|
218
|
+
if (annotation.kind === 'text_annotation')
|
|
219
|
+
d.text = annotation.text;
|
|
220
|
+
else if (annotation.kind === 'number_annotation')
|
|
221
|
+
d.value = annotation.value;
|
|
222
|
+
else
|
|
223
|
+
d.icon = annotation.icon;
|
|
224
|
+
}
|
|
142
225
|
else if (c.type === 'DRAW_AGENT_RECTANGLE')
|
|
143
226
|
for (const key of ['start_time', 'end_time', 'lower_price', 'upper_price', 'show_fill'])
|
|
144
227
|
d[key] = c[key];
|
|
228
|
+
else if (c.type === 'DRAW_AGENT_POLYLINE')
|
|
229
|
+
d.points = c.points;
|
|
145
230
|
else {
|
|
146
231
|
d.points = [c.from, c.to, ...(c.parallel ? [c.parallel] : [])];
|
|
147
232
|
if (c.type === 'DRAW_AGENT_CHANNEL')
|
|
148
233
|
d.show_midline = c.show_midline;
|
|
234
|
+
if (c.type === 'DRAW_AGENT_FIBONACCI') {
|
|
235
|
+
d.ratios = c.ratios;
|
|
236
|
+
d.levels = c.levels;
|
|
237
|
+
}
|
|
149
238
|
}
|
|
150
239
|
return d;
|
|
151
240
|
});
|
|
@@ -5,8 +5,15 @@ import { join } from 'node:path';
|
|
|
5
5
|
import { drawingInventory, observationKey, sanitizeChartContext, text } from "./chart-observation.js";
|
|
6
6
|
import { sourceStatus } from "./chart-source-status.js";
|
|
7
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";
|
|
8
15
|
const TOKEN = /^[A-Za-z0-9_-]{32}$/;
|
|
9
|
-
const AI = new Set(['DRAW_HORIZONTAL_LINE', 'DRAW_AGENT_TREND_LINE', 'DRAW_AGENT_CHANNEL', 'DRAW_AGENT_RECTANGLE']);
|
|
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']);
|
|
10
17
|
const INTERVALS = ['minute', 'day', 'week', 'month', 'quarter', 'year'];
|
|
11
18
|
function finite(value, positive = false) {
|
|
12
19
|
if (typeof value !== 'number' || !Number.isFinite(value) || positive && value <= 0)
|
|
@@ -88,6 +95,30 @@ export function sanitizeSourcePayload(raw, state) {
|
|
|
88
95
|
return { type, series: c.series.map((s) => ({ symbol: text(s.symbol, 64), name: text(s.name ?? '', 128),
|
|
89
96
|
rows: sanitizeChartContext({ ...state, candles: s.rows }).candles })) };
|
|
90
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
|
+
}
|
|
91
122
|
if (type === 'TEXT_MARKER')
|
|
92
123
|
return { type, id: text(c.id ?? '', 256), text: text(c.text ?? '', 256), ...point(c), color: color(c.color ?? 'info') };
|
|
93
124
|
if (type === 'DRAW_TREND_LINE')
|
|
@@ -112,9 +143,21 @@ export function sanitizeSourcePayload(raw, state) {
|
|
|
112
143
|
throw Error('invalid_source_style');
|
|
113
144
|
result.show_fill = c.show_fill;
|
|
114
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
|
+
}
|
|
115
151
|
else {
|
|
116
152
|
result.from = point(c.from);
|
|
117
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
|
+
}
|
|
118
161
|
if (type === 'DRAW_AGENT_CHANNEL') {
|
|
119
162
|
result.parallel = point(c.parallel);
|
|
120
163
|
for (const edge of ['upper', 'lower'])
|
|
@@ -236,13 +279,20 @@ export class ChartSources {
|
|
|
236
279
|
return result(['queued', 'delivered'].includes(app.status) ? 'drawing_pending' : 'drawing_outcome_unknown');
|
|
237
280
|
}
|
|
238
281
|
const parent = view.state.source_chart_session;
|
|
239
|
-
|
|
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)
|
|
240
287
|
return result('source_missing');
|
|
241
|
-
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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) {
|
|
246
296
|
const g = source.drawing_guard;
|
|
247
297
|
const app = await this.read(join(this.runtime, 'edits', String(process.pid), g.context_ref, 'applications', parent + '.json'));
|
|
248
298
|
if (app.status !== 'applied' || app.confirmed_chart_session !== parent || app.conversation_id !== view.conversation
|
|
@@ -250,15 +300,15 @@ export class ChartSources {
|
|
|
250
300
|
|| g.conversation_id !== view.conversation)
|
|
251
301
|
throw Error('unconfirmed_source_parent');
|
|
252
302
|
}
|
|
253
|
-
if (source
|
|
303
|
+
if (source?.view_source_guard) {
|
|
254
304
|
const g = source.view_source_guard, proof = await this.read(this.proofPath(g.context_ref, parent));
|
|
255
305
|
if (proof.status !== 'confirmed' || proof.conversation_id !== view.conversation || proof.host_process_id !== process.pid
|
|
256
306
|
|| proof.chart_session !== parent || g.chart_session !== parent || proof.view_id !== g.view_id
|
|
257
307
|
|| g.conversation_id !== view.conversation)
|
|
258
308
|
throw Error('unconfirmed_source_parent');
|
|
259
309
|
}
|
|
260
|
-
const rows = source
|
|
261
|
-
if (source
|
|
310
|
+
const rows = source?.chartCommands?.find((c) => c.type === 'SET_CANDLES')?.rows;
|
|
311
|
+
if (source?.symbol === view.state.symbol && Array.isArray(rows)) {
|
|
262
312
|
const original = { ...view.state, symbol: source.symbol, interval: source.interval, interval_value: source.interval_value ?? null,
|
|
263
313
|
adjust: source.adjust ?? '', candles: rows, drawings: drawingInventory(source) };
|
|
264
314
|
if (sourceKey(original) === sourceKey(view.state))
|
|
@@ -269,10 +319,10 @@ export class ChartSources {
|
|
|
269
319
|
&& this.now() <= previous.expires_at_ms)
|
|
270
320
|
return result('source_handoff_pending', previous);
|
|
271
321
|
if (!raw)
|
|
272
|
-
return result('source_snapshot_mismatch');
|
|
322
|
+
return result(hasParent ? 'source_snapshot_mismatch' : 'source_missing');
|
|
273
323
|
const payload = sanitizeSourcePayload(raw, view.state), token = randomBytes(24).toString('base64url');
|
|
274
324
|
const ai = payload.chartCommands.filter((c) => AI.has(c.type));
|
|
275
|
-
const old = source
|
|
325
|
+
const old = source?.symbol === view.state.symbol ? source.chartCommands.filter((c) => AI.has(c.type)) : [];
|
|
276
326
|
if (ai.length !== old.length || new Set(ai.map((c) => c.id)).size !== ai.length)
|
|
277
327
|
throw Error('source_inventory_mismatch');
|
|
278
328
|
// The handoff may change rows/period, never existing AI definitions or geometry.
|
|
@@ -285,7 +335,8 @@ export class ChartSources {
|
|
|
285
335
|
}
|
|
286
336
|
payload.view_source_guard = { protocol: 'browser-source-v1', context_ref: view.ref, conversation_id: view.conversation,
|
|
287
337
|
view_id: view.view, expected_revision: view.revision, chart_session: token };
|
|
288
|
-
|
|
338
|
+
if (hasParent)
|
|
339
|
+
payload.drawing_parent_session = parent;
|
|
289
340
|
await this.write(join(this.runtime, 'sessions', token + '.json'), { ok: true, session: token, host_process_id: process.pid,
|
|
290
341
|
symbol: payload.symbol, name: payload.name, published_at: Math.floor(this.now() / 1000), payload });
|
|
291
342
|
const offer = { chart_session: token, context_ref: view.ref, conversation_id: view.conversation, view_id: view.view,
|