refactor(api): compose research services with narrow collaborators (CL28)
This commit is contained in:
parent
3a63b2039e
commit
24487d35da
28 changed files with 281 additions and 94 deletions
|
|
@ -8,6 +8,7 @@ import { cors } from 'hono/cors';
|
|||
import type { HttpSecurity } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { createWebApiSecurity } from '../http-security';
|
||||
import { createResearchServices } from '../services/research-services';
|
||||
import { createBacktestRoutes } from './backtest.routes';
|
||||
import { createBacktestV2Routes } from './backtests-v2.routes';
|
||||
import { createDatasetRoutes } from './datasets.routes';
|
||||
|
|
@ -44,6 +45,7 @@ export function createRoutes(
|
|||
})
|
||||
);
|
||||
security.mount(app);
|
||||
const researchServices = createResearchServices(container);
|
||||
|
||||
// Create routes with container
|
||||
const healthRoutes = createHealthRoutes(container);
|
||||
|
|
@ -63,13 +65,14 @@ export function createRoutes(
|
|||
app.route('/', backtestRoutes); // Mounted at root since routes already have /api prefix
|
||||
app.route('/', backtestV2Routes); // V2 routes also mounted at root
|
||||
app.route('/', createDatasetRoutes(container));
|
||||
app.route('/', createStrategyRoutes(container));
|
||||
app.route('/', createResearchRoutes(container));
|
||||
app.route('/', createStrategyRoutes(container, researchServices.catalog));
|
||||
app.route('/', createResearchRoutes(container, researchServices.research));
|
||||
app.route('/', createResearchAgentRoutes(container, security.researchEnabled));
|
||||
app.route('/', createExperimentRoutes(container));
|
||||
app.route('/', createExperimentRoutes(container, researchServices));
|
||||
app.route(
|
||||
'/',
|
||||
createPaperDeploymentRoutes(container, {
|
||||
service: researchServices.paperDeployments,
|
||||
origin: integrations ? integrations.paperOrchestratorOrigin : process.env.ORCHESTRATOR_URL,
|
||||
origins: security.allowedOrigins,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { z } from 'zod';
|
|||
import { getResearchHttpPrincipal } from '@stock-bot/di';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { ChronologicalValidationService } from '../services/chronological-validation.service';
|
||||
import {
|
||||
parseCostSensitivityInput,
|
||||
parseCostSensitivitySubmission,
|
||||
|
|
@ -16,8 +15,8 @@ import {
|
|||
parseExperimentSubmission,
|
||||
} from '../services/experiment-input';
|
||||
import { parseExperimentRetry } from '../services/experiment-retry';
|
||||
import { ExperimentsService } from '../services/experiments.service';
|
||||
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
|
||||
import { createResearchServices, type ResearchServices } from '../services/research-services';
|
||||
import {
|
||||
parseValidationFinalization,
|
||||
parseValidationSelection,
|
||||
|
|
@ -29,14 +28,17 @@ import {
|
|||
parseWalkForwardInput,
|
||||
parseWalkForwardSubmission,
|
||||
} from '../services/walk-forward-input';
|
||||
import { WalkForwardService } from '../services/walk-forward.service';
|
||||
|
||||
/** Mounted HTTP security owns read authorization; every mutation additionally requires credential proof. */
|
||||
export function createExperimentRoutes(container: IServiceContainer) {
|
||||
export function createExperimentRoutes(
|
||||
container: IServiceContainer,
|
||||
services: Pick<
|
||||
ResearchServices,
|
||||
'experiments' | 'validations' | 'walkForwards'
|
||||
> = createResearchServices(container)
|
||||
) {
|
||||
const app = new Hono();
|
||||
const service = new ExperimentsService(container);
|
||||
const validations = new ChronologicalValidationService(container);
|
||||
const walkForwards = new WalkForwardService(container);
|
||||
const { experiments: service, validations, walkForwards } = services;
|
||||
const logger = getLogger('experiments-api');
|
||||
const credentials: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ import {
|
|||
paperAllowedOrigins,
|
||||
requirePaperOperator,
|
||||
} from '@stock-bot/trading-runtime/paper-operator';
|
||||
import { PaperDeploymentsService } from '../services/paper-deployments.service';
|
||||
import type { PaperDeploymentsService } from '../services/paper-deployments.service';
|
||||
import { ResearchError } from '../services/research-errors';
|
||||
import { createResearchServices } from '../services/research-services';
|
||||
|
||||
/** Forwards the original human credential; never substitutes a service token. */
|
||||
export async function forwardPaperCommand(
|
||||
|
|
@ -95,7 +96,7 @@ export function createPaperDeploymentRoutes(
|
|||
} = {}
|
||||
) {
|
||||
const app = new Hono(),
|
||||
service = options.service ?? new PaperDeploymentsService(container),
|
||||
service = options.service ?? createResearchServices(container).paperDeployments,
|
||||
origins = options.origins ?? paperAllowedOrigins();
|
||||
app.use('*', async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import type { IServiceContainer } from '@stock-bot/services';
|
|||
import { archiveResearchSchema, parseResearchInput } from '@stock-bot/stock-config/research';
|
||||
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
|
||||
import { ResearchError } from '../services/research-errors';
|
||||
import { ResearchService } from '../services/research.service';
|
||||
import { createResearchServices } from '../services/research-services';
|
||||
|
||||
const emptyBody = z.object({}).strict();
|
||||
function includeArchived(value: string | undefined): boolean {
|
||||
|
|
@ -25,9 +25,11 @@ async function noMutationBody(request: Request): Promise<void> {
|
|||
}
|
||||
|
||||
/** Research metadata only; the mounted security layer owns authenticated read access. */
|
||||
export function createResearchRoutes(container: IServiceContainer) {
|
||||
export function createResearchRoutes(
|
||||
container: IServiceContainer,
|
||||
service = createResearchServices(container).research
|
||||
) {
|
||||
const app = new Hono();
|
||||
const service = new ResearchService(container);
|
||||
const credentials: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getResearchHttpPrincipal(c.req.raw)) {
|
||||
|
|
|
|||
|
|
@ -11,9 +11,11 @@ const publication = z
|
|||
const parameters = z.object({ parameters: z.unknown() }).strict();
|
||||
const submissionKey = z.string().regex(/^[\x21-\x7e]{1,200}$/);
|
||||
|
||||
export function createStrategyRoutes(container: IServiceContainer) {
|
||||
export function createStrategyRoutes(
|
||||
container: IServiceContainer,
|
||||
service = new StrategyCatalogService(container)
|
||||
) {
|
||||
const app = new Hono();
|
||||
const service = new StrategyCatalogService(container);
|
||||
const credentials: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getResearchHttpPrincipal(c.req.raw)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue