feat(research): prepare stored data by declared sessions (RP03)
This commit is contained in:
parent
302ba60e53
commit
d2b78eacdf
10 changed files with 805 additions and 72 deletions
|
|
@ -4,7 +4,10 @@ import {
|
|||
preparationManifestSchema,
|
||||
} from '@stock-bot/research/dataset-contracts';
|
||||
import { candidateExecutionSchema } from '@stock-bot/stock-config/candidate-execution';
|
||||
import { canonicalRunRequestSchema } from '@stock-bot/stock-config/canonical-research';
|
||||
import {
|
||||
canonicalRunRequestSchema,
|
||||
declaredStoredSourceWindows,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import type {
|
||||
CanonicalDatasetCatalogEntry,
|
||||
CanonicalDatasetPreparation,
|
||||
|
|
@ -218,14 +221,29 @@ function metadataIdentity(value: PreparationMetadata) {
|
|||
.sort((a, b) => compareText(a.instrumentId, b.instrumentId)),
|
||||
});
|
||||
}
|
||||
/** Preview caller declarations only; the server still validates the complete metadata and bars. */
|
||||
export function storedSourceSessionScope(raw: unknown) {
|
||||
const parsed = preparationMetadata.safeParse(raw);
|
||||
return parsed.success && parsed.data.series.length === 1
|
||||
? declaredStoredSourceWindows(parsed.data)
|
||||
: null;
|
||||
}
|
||||
export function parseStoredDatasetPreparation(
|
||||
raw: unknown,
|
||||
suppliedMetadata: unknown
|
||||
suppliedMetadata: unknown,
|
||||
selection?: CanonicalStoredSourcePreparation['selection']
|
||||
): CanonicalStoredSourcePreparation {
|
||||
const envelope = z
|
||||
.object({ input: storedSourceInput, preparation: z.unknown() })
|
||||
.object({
|
||||
input: storedSourceInput,
|
||||
preparation: z.unknown(),
|
||||
selection: z.literal('declared-sessions').optional(),
|
||||
})
|
||||
.strict()
|
||||
.parse(raw);
|
||||
if (envelope.selection !== selection) {
|
||||
throw new Error('The stored source response does not match the requested selection.');
|
||||
}
|
||||
const preparation = parseDatasetPreparation(envelope.preparation);
|
||||
const expected = preparationMetadata.parse(suppliedMetadata);
|
||||
const { calendars, series, executionSeries, bounds, universe, corporateActions } =
|
||||
|
|
@ -268,7 +286,7 @@ export function parseStoredDatasetPreparation(
|
|||
) {
|
||||
throw new Error('The stored source response does not match its metadata and prepared dataset.');
|
||||
}
|
||||
return { input, preparation };
|
||||
return { input, preparation, ...(selection ? { selection } : {}) };
|
||||
}
|
||||
export class DatasetPreparationRequestError extends Error {
|
||||
constructor(
|
||||
|
|
@ -317,12 +335,13 @@ export const backtestApiV2 = {
|
|||
prepareDatasetRequest({ input, expectedHash }, signal),
|
||||
async previewStoredDataset(
|
||||
metadata: unknown,
|
||||
signal?: AbortSignal
|
||||
signal?: AbortSignal,
|
||||
selection?: CanonicalStoredSourcePreparation['selection']
|
||||
): Promise<CanonicalStoredSourcePreparation> {
|
||||
const response = await fetch(`${API_BASE_URL}/api/v2/datasets/canonical/stored-source`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ metadata }),
|
||||
body: JSON.stringify({ metadata, ...(selection ? { selection } : {}) }),
|
||||
signal,
|
||||
});
|
||||
if (!response.ok) {
|
||||
|
|
@ -332,7 +351,7 @@ export const backtestApiV2 = {
|
|||
if (response.status !== 200) {
|
||||
throw new Error('Unexpected stored source status');
|
||||
}
|
||||
return parseStoredDatasetPreparation(await response.json(), metadata);
|
||||
return parseStoredDatasetPreparation(await response.json(), metadata, selection);
|
||||
} catch {
|
||||
throw new Error('The stored source response could not be verified.');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue