refactor: share research domain and simplify application workflows

This commit is contained in:
Boki 2026-10-26 15:29:43 -04:00
parent 805a9f8fd4
commit 8c39ccf7ea
58 changed files with 3805 additions and 3081 deletions

View file

@ -15,7 +15,8 @@ import {
validationSummarySchema as summary,
ValidationApiError,
} from '@stock-bot/stock-config/research-responses';
import { API_BASE_URL, apiFetch } from '@/lib/api';
import { API_BASE_URL } from '@/lib/api';
import { requestJson } from '@/lib/requestJson';
export {
parseValidationDetail,
@ -26,41 +27,20 @@ export {
export type { ValidationResultEvidence };
const envelope = (body: unknown, key: string) => z.record(z.unknown()).parse(body)[key];
async function request<T>(
function request<T>(
path: string,
init: RequestInit,
status: number,
parse: (body: unknown) => T
): Promise<T> {
let response: Response;
try {
response = await apiFetch(`${API_BASE_URL}/api/v2/experiments/validation${path}`, init);
} catch (cause) {
if (init.signal?.aborted) {
throw cause;
}
throw new ValidationApiError(
'The validation response was lost. Retry the same request to recover its receipt.',
null
);
}
const body: unknown = await response.json().catch(() => null);
if (!response.ok) {
const error = z.object({ error: z.string() }).safeParse(body);
throw new ValidationApiError(
error.success ? error.data.error : `Validation request failed (${response.status})`,
response.status
);
}
try {
if (response.status !== status) {
throw invalid();
}
return parse(body);
} catch {
throw invalid();
}
return requestJson(`${API_BASE_URL}/api/v2/experiments/validation${path}`, init, status, parse, {
create: (message, code) => new ValidationApiError(message, code),
invalid,
lostMessage: 'The validation response was lost. Retry the same request to recover its receipt.',
failureLabel: 'Validation request failed',
});
}
const post = (body: unknown, key?: string): RequestInit => ({
method: 'POST',
body: JSON.stringify(body),

View file

@ -23,7 +23,8 @@ import {
sameExperimentInput as same,
type ExperimentRetryTarget,
} from '@stock-bot/stock-config/research-responses';
import { API_BASE_URL, apiFetch } from '@/lib/api';
import { API_BASE_URL } from '@/lib/api';
import { requestJson } from '@/lib/requestJson';
export {
ExperimentApiError,
@ -41,41 +42,21 @@ export {
type WorkbenchExperimentPreviewView,
} from '@stock-bot/stock-config/research-responses';
async function request<T>(
function request<T>(
path: string,
init: RequestInit,
expectedStatus: number,
status: number,
parse: (body: unknown) => T
): Promise<T> {
let response: Response;
try {
response = await apiFetch(`${API_BASE_URL}/api/v2/experiments${path}`, init);
} catch (cause) {
if (init.signal?.aborted) {
throw cause;
}
throw new ExperimentApiError(
return requestJson(`${API_BASE_URL}/api/v2/experiments${path}`, init, status, parse, {
create: (message, code) => new ExperimentApiError(message, code),
invalid,
lostMessage:
'The experiment response was lost. Retry the same submission to check whether it was accepted.',
null
);
}
const body: unknown = await response.json().catch(() => null);
if (!response.ok) {
const parsed = z.object({ error: z.string() }).safeParse(body);
throw new ExperimentApiError(
parsed.success ? parsed.data.error : `Experiment request failed (${response.status})`,
response.status
);
}
try {
if (response.status !== expectedStatus) {
throw invalid();
}
return parse(body);
} catch {
throw invalid();
}
failureLabel: 'Experiment request failed',
});
}
const envelope = (body: unknown, field: string): unknown =>
z.record(z.unknown()).parse(body)[field];
const json = (body: unknown): RequestInit => ({

View file

@ -16,7 +16,8 @@ import {
type WalkForwardSubmission,
type WalkForwardSummary,
} from '@stock-bot/stock-config/walk-forward';
import { API_BASE_URL, apiFetch } from '@/lib/api';
import { API_BASE_URL } from '@/lib/api';
import { requestJson } from '@/lib/requestJson';
export {
WalkForwardApiError,
@ -27,41 +28,27 @@ export {
} from '@stock-bot/stock-config/research-responses';
const envelope = (body: unknown, key: string) => z.record(z.unknown()).parse(body)[key];
async function request<T>(
function request<T>(
path: string,
init: RequestInit,
status: number,
parse: (body: unknown) => T
): Promise<T> {
let response: Response;
try {
response = await apiFetch(`${API_BASE_URL}/api/v2/experiments/walk-forward${path}`, init);
} catch (cause) {
if (init.signal?.aborted) {
throw cause;
return requestJson(
`${API_BASE_URL}/api/v2/experiments/walk-forward${path}`,
init,
status,
parse,
{
create: (message, code) => new WalkForwardApiError(message, code),
invalid,
lostMessage:
'The walk-forward response was lost. Retry the same request to recover its receipt.',
failureLabel: 'Walk-forward request failed',
}
throw new WalkForwardApiError(
'The walk-forward response was lost. Retry the same request to recover its receipt.',
null
);
}
const body: unknown = await response.json().catch(() => null);
if (!response.ok) {
const error = z.object({ error: z.string() }).safeParse(body);
throw new WalkForwardApiError(
error.success ? error.data.error : `Walk-forward request failed (${response.status})`,
response.status
);
}
try {
if (response.status !== status) {
throw invalid();
}
return parse(body);
} catch {
throw invalid();
}
);
}
const post = (body: unknown, key?: string): RequestInit => ({
method: 'POST',
body: JSON.stringify(body),