feat(research): add projects and revisioned evidence-linked findings
This commit is contained in:
parent
618fb9e33c
commit
ee45257507
22 changed files with 2947 additions and 17 deletions
|
|
@ -14,6 +14,7 @@ import { createExchangeRoutes } from './exchange.routes';
|
|||
import { createHealthRoutes } from './health.routes';
|
||||
import { createMonitoringRoutes } from './monitoring.routes';
|
||||
import { createPipelineRoutes } from './pipeline.routes';
|
||||
import { createResearchRoutes } from './research.routes';
|
||||
import { createStrategyRoutes } from './strategies.routes';
|
||||
|
||||
export function createRoutes(
|
||||
|
|
@ -51,6 +52,7 @@ export function createRoutes(
|
|||
app.route('/', backtestV2Routes); // V2 routes also mounted at root
|
||||
app.route('/', createDatasetRoutes(container));
|
||||
app.route('/', createStrategyRoutes(container));
|
||||
app.route('/', createResearchRoutes(container));
|
||||
|
||||
return app;
|
||||
}
|
||||
|
|
|
|||
190
apps/stock/web-api/src/routes/research.routes.ts
Normal file
190
apps/stock/web-api/src/routes/research.routes.ts
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
import { Hono, type MiddlewareHandler } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { getCredentialHttpPrincipal } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { archiveResearchSchema, parseResearchInput } from '@stock-bot/stock-config/research';
|
||||
import { ResearchError } from '../services/research-errors';
|
||||
import { ResearchService } from '../services/research.service';
|
||||
|
||||
const emptyBody = z.object({}).strict();
|
||||
function includeArchived(value: string | undefined): boolean {
|
||||
if (value === undefined || value === 'false') {
|
||||
return false;
|
||||
}
|
||||
if (value === 'true') {
|
||||
return true;
|
||||
}
|
||||
throw new ResearchError(400, 'includeArchived must be true or false');
|
||||
}
|
||||
async function noMutationBody(request: Request): Promise<void> {
|
||||
const body = await request.text();
|
||||
if (body !== '') {
|
||||
emptyBody.parse(JSON.parse(body));
|
||||
}
|
||||
}
|
||||
|
||||
/** Research metadata only; the mounted security layer owns authenticated read access. */
|
||||
export function createResearchRoutes(container: IServiceContainer) {
|
||||
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) && !getCredentialHttpPrincipal(c.req.raw)) {
|
||||
return c.json({ error: 'Verified credential principal required' }, 401);
|
||||
}
|
||||
await next();
|
||||
return undefined;
|
||||
};
|
||||
for (const path of [
|
||||
'/api/v2/research-projects',
|
||||
'/api/v2/research-projects/*',
|
||||
'/api/v2/hypotheses/*',
|
||||
'/api/v2/analysis-artifacts/*',
|
||||
]) {
|
||||
app.use(path, credentials);
|
||||
}
|
||||
app.onError((error, c) => {
|
||||
if (error instanceof ResearchError) {
|
||||
return c.json({ error: error.message }, error.status);
|
||||
}
|
||||
if (error instanceof z.ZodError || error instanceof SyntaxError) {
|
||||
return c.json({ error: 'Invalid research request' }, 400);
|
||||
}
|
||||
container.logger.error('Research operation failed', { error });
|
||||
return c.json({ error: 'Research workspace unavailable' }, 500);
|
||||
});
|
||||
const actor = (request: Request) => {
|
||||
const principal = getCredentialHttpPrincipal(request);
|
||||
if (!principal) {
|
||||
throw new ResearchError(401, 'Verified credential principal required');
|
||||
}
|
||||
return principal;
|
||||
};
|
||||
|
||||
app.get('/api/v2/research-projects', async c =>
|
||||
c.json({
|
||||
projects: await service.listProjects(includeArchived(c.req.query('includeArchived'))),
|
||||
})
|
||||
);
|
||||
app.post('/api/v2/research-projects', async c =>
|
||||
c.json({ project: await service.createProject(await c.req.json(), actor(c.req.raw)) }, 201)
|
||||
);
|
||||
app.get('/api/v2/research-projects/:id', async c =>
|
||||
c.json({ project: await service.getProject(c.req.param('id')) })
|
||||
);
|
||||
app.patch('/api/v2/research-projects/:id', async c =>
|
||||
c.json({
|
||||
project: await service.updateProject(c.req.param('id'), await c.req.json(), actor(c.req.raw)),
|
||||
})
|
||||
);
|
||||
app.get('/api/v2/research-projects/:id/strategies', async c =>
|
||||
c.json({ strategies: await service.listProjectStrategies(c.req.param('id')) })
|
||||
);
|
||||
app.put('/api/v2/research-projects/:id/strategies/:strategyId', async c => {
|
||||
await noMutationBody(c.req.raw);
|
||||
await service.linkStrategy(c.req.param('id'), c.req.param('strategyId'), actor(c.req.raw));
|
||||
return c.body(null, 204);
|
||||
});
|
||||
app.delete('/api/v2/research-projects/:id/strategies/:strategyId', async c => {
|
||||
await noMutationBody(c.req.raw);
|
||||
await service.unlinkStrategy(c.req.param('id'), c.req.param('strategyId'), actor(c.req.raw));
|
||||
return c.body(null, 204);
|
||||
});
|
||||
|
||||
app.get('/api/v2/research-projects/:id/hypotheses', async c =>
|
||||
c.json({
|
||||
hypotheses: await service.listHypotheses(
|
||||
c.req.param('id'),
|
||||
includeArchived(c.req.query('includeArchived'))
|
||||
),
|
||||
})
|
||||
);
|
||||
app.post('/api/v2/research-projects/:id/hypotheses', async c =>
|
||||
c.json(
|
||||
{
|
||||
hypothesis: await service.createHypothesis(
|
||||
c.req.param('id'),
|
||||
await c.req.json(),
|
||||
actor(c.req.raw)
|
||||
),
|
||||
},
|
||||
201
|
||||
)
|
||||
);
|
||||
app.get('/api/v2/hypotheses/:id', async c =>
|
||||
c.json({ hypothesis: await service.getHypothesis(c.req.param('id')) })
|
||||
);
|
||||
app.get('/api/v2/hypotheses/:id/revisions', async c =>
|
||||
c.json({ revisions: await service.listHypothesisRevisions(c.req.param('id')) })
|
||||
);
|
||||
app.post('/api/v2/hypotheses/:id/revisions', async c =>
|
||||
c.json(
|
||||
{
|
||||
hypothesis: await service.reviseHypothesis(
|
||||
c.req.param('id'),
|
||||
await c.req.json(),
|
||||
actor(c.req.raw)
|
||||
),
|
||||
},
|
||||
201
|
||||
)
|
||||
);
|
||||
app.patch('/api/v2/hypotheses/:id', async c =>
|
||||
c.json({
|
||||
hypothesis: await service.archiveHypothesis(
|
||||
c.req.param('id'),
|
||||
parseResearchInput(archiveResearchSchema, await c.req.json()),
|
||||
actor(c.req.raw)
|
||||
),
|
||||
})
|
||||
);
|
||||
|
||||
app.get('/api/v2/research-projects/:id/analysis-artifacts', async c =>
|
||||
c.json({
|
||||
artifacts: await service.listArtifacts(
|
||||
c.req.param('id'),
|
||||
includeArchived(c.req.query('includeArchived'))
|
||||
),
|
||||
})
|
||||
);
|
||||
app.post('/api/v2/research-projects/:id/analysis-artifacts', async c =>
|
||||
c.json(
|
||||
{
|
||||
artifact: await service.createArtifact(
|
||||
c.req.param('id'),
|
||||
await c.req.json(),
|
||||
actor(c.req.raw)
|
||||
),
|
||||
},
|
||||
201
|
||||
)
|
||||
);
|
||||
app.get('/api/v2/analysis-artifacts/:id', async c =>
|
||||
c.json({ artifact: await service.getArtifact(c.req.param('id')) })
|
||||
);
|
||||
app.get('/api/v2/analysis-artifacts/:id/revisions', async c =>
|
||||
c.json({ revisions: await service.listArtifactRevisions(c.req.param('id')) })
|
||||
);
|
||||
app.post('/api/v2/analysis-artifacts/:id/revisions', async c =>
|
||||
c.json(
|
||||
{
|
||||
artifact: await service.reviseArtifact(
|
||||
c.req.param('id'),
|
||||
await c.req.json(),
|
||||
actor(c.req.raw)
|
||||
),
|
||||
},
|
||||
201
|
||||
)
|
||||
);
|
||||
app.patch('/api/v2/analysis-artifacts/:id', async c =>
|
||||
c.json({
|
||||
artifact: await service.archiveArtifact(
|
||||
c.req.param('id'),
|
||||
parseResearchInput(archiveResearchSchema, await c.req.json()),
|
||||
actor(c.req.raw)
|
||||
),
|
||||
})
|
||||
);
|
||||
return app;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue