merge sandbox/ai-1790562657-0EF743: chg_0001 dth scaffold + chg_0002 experiment_platform MVP
- chg_0001_dth_scaffold (skip_coverage): openspec framework + NestJS+React+PGlite scaffold - chg_0002_sp1_experiment_platform_mvp: 6 scenarios S1-S6 + 21 perm-smoke + 16 e2e + 3 demo operators (hello-mpc/pir/vertical-lr) Sandbox task exit 0. Independent verification: check_spec_coverage.sh gap=0 PASS. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
commit
c1cf6f54e5
314 files changed
+55077
No files matched your search
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "dth-backend",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"main": "dist/main.js",
|
||||
"scripts": {
|
||||
"dev": "ts-node-dev --respawn --transpile-only src/main.ts",
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"start": "node dist/main.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^10.4.0",
|
||||
"@nestjs/core": "^10.4.0",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/platform-express": "^10.4.0",
|
||||
"@electric-sql/pglite": "^0.2.12",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"uuid": "^9.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcryptjs": "^2.4.6",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/node": "^20.11.0",
|
||||
"@types/uuid": "^9.0.7",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^5.4.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { DthExperimentModule } from './dth/experiment/experiment.module';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule, DthExperimentModule],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
interface LoginBody {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
@Controller('api/ssw/dth/auth')
|
||||
export class AuthController {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
@Post('login')
|
||||
@HttpCode(200)
|
||||
async login(@Body() body: LoginBody) {
|
||||
if (!body?.username || !body?.password) {
|
||||
return { code: 400, msg: 'username and password required', data: null };
|
||||
}
|
||||
const r = await this.auth.login(body.username, body.password);
|
||||
return { code: 200, msg: 'ok', data: r };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Module, Global } from '@nestjs/common';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { JwtAuthGuard } from './jwt.guard';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [
|
||||
JwtModule.register({
|
||||
secret: process.env.JWT_SECRET || 'dth-mvp-dev-secret-change-me',
|
||||
signOptions: { expiresIn: '12h' },
|
||||
}),
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, JwtAuthGuard],
|
||||
exports: [JwtAuthGuard, JwtModule, AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { getDb } from '../common/pglite/init';
|
||||
|
||||
export interface UserPayload {
|
||||
username: string;
|
||||
isAdmin: boolean;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(private readonly jwt: JwtService) {}
|
||||
|
||||
async login(username: string, password: string): Promise<{ token: string; user: UserPayload }> {
|
||||
const db = getDb();
|
||||
const r = await db.query<{ username: string; password: string; is_admin: boolean }>(
|
||||
`SELECT username, password, is_admin FROM dth_user WHERE username = $1 LIMIT 1`,
|
||||
[username],
|
||||
);
|
||||
const row = r.rows[0];
|
||||
if (!row) throw new UnauthorizedException('用户不存在或密码错误');
|
||||
const ok = await bcrypt.compare(password, row.password);
|
||||
if (!ok) throw new UnauthorizedException('用户不存在或密码错误');
|
||||
|
||||
const payload: UserPayload = { username: row.username, isAdmin: !!row.is_admin };
|
||||
const token = await this.jwt.signAsync(payload);
|
||||
return { token, user: payload };
|
||||
}
|
||||
|
||||
async verify(token: string): Promise<UserPayload> {
|
||||
return this.jwt.verifyAsync<UserPayload>(token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AuthService, UserPayload } from './auth.service';
|
||||
|
||||
/**
|
||||
* JwtAuthGuard — reads Bearer token, verifies via AuthService.
|
||||
* Use `@UseGuards(JwtAuthGuard)` on protected routes.
|
||||
*
|
||||
* Set metadata `isAdmin: true` on a route to require admin role
|
||||
* (combine via @SetMetadata('isAdmin', true) or use the helper).
|
||||
*/
|
||||
@Injectable()
|
||||
export class JwtAuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly auth: AuthService,
|
||||
private readonly reflector: Reflector,
|
||||
) {}
|
||||
|
||||
async canActivate(ctx: ExecutionContext): Promise<boolean> {
|
||||
const req = ctx.switchToHttp().getRequest();
|
||||
const auth = req.headers['authorization'] || '';
|
||||
const m = /^Bearer\s+(.+)$/i.exec(auth);
|
||||
if (!m) {
|
||||
// SSW convention: code=600 means "not logged in"
|
||||
throw new UnauthorizedException({ message: '未登录', notLoggedIn: true });
|
||||
}
|
||||
let payload: UserPayload;
|
||||
try {
|
||||
payload = await this.auth.verify(m[1]);
|
||||
} catch {
|
||||
throw new UnauthorizedException({ message: 'token 无效或已过期', notLoggedIn: true });
|
||||
}
|
||||
req.user = payload;
|
||||
|
||||
const requireAdmin = this.reflector.getAllAndOverride<boolean>('isAdmin', [
|
||||
ctx.getHandler(),
|
||||
ctx.getClass(),
|
||||
]);
|
||||
if (requireAdmin && !payload.isAdmin) {
|
||||
throw new ForbiddenException('需要管理员权限');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export const IS_ADMIN_KEY = 'isAdmin';
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
export const RequireAdmin = () => SetMetadata(IS_ADMIN_KEY, true);
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* ActionResultFilter — unifies controller responses into the SSW envelope:
|
||||
* { code: 200|600|401|403|400, msg: string, data: any }
|
||||
*
|
||||
* - code 200: success (data carries payload)
|
||||
* - code 600: not logged in (auth required)
|
||||
* - code 401: bad credentials
|
||||
* - code 403: forbidden (RBAC)
|
||||
* - code 400: validation / business error
|
||||
*
|
||||
* Anything thrown becomes a structured envelope; raw stack traces stay in logs.
|
||||
*/
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
|
||||
@Catch()
|
||||
export class ActionResultFilter implements ExceptionFilter {
|
||||
private readonly logger = new Logger(ActionResultFilter.name);
|
||||
|
||||
async catch(exception: unknown, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const res = ctx.getResponse();
|
||||
const req = ctx.getRequest();
|
||||
|
||||
let code = 500;
|
||||
let msg = 'Internal server error';
|
||||
let data: any = null;
|
||||
|
||||
if (exception instanceof HttpException) {
|
||||
const status = exception.getStatus();
|
||||
const r = exception.getResponse() as any;
|
||||
// 600 = "not logged in" (no/invalid token) — SSW envelope convention
|
||||
// 401 = "bad credentials" (login endpoint only)
|
||||
// Distinguish by exception name set on the response payload.
|
||||
const notLoggedIn = r?.notLoggedIn === true;
|
||||
if (status === 401 && notLoggedIn) { code = 600; msg = (r?.message) || '未登录'; }
|
||||
else if (status === 401) { code = 401; msg = (r?.message) || '凭据无效'; }
|
||||
else if (status === 403) { code = 403; msg = (r?.message) || '无权限'; }
|
||||
else if (status === 400) {
|
||||
code = 400;
|
||||
msg = (r?.message && (Array.isArray(r.message) ? r.message.join('; ') : r.message)) || '参数错误';
|
||||
} else {
|
||||
code = status;
|
||||
msg = (r?.message) || msg;
|
||||
}
|
||||
} else if (exception instanceof Error) {
|
||||
this.logger.error(`${req?.method} ${req?.url} -> ${exception.message}`);
|
||||
msg = exception.message;
|
||||
}
|
||||
|
||||
res.status(200).json({ code, msg, data });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { PGlite } from '@electric-sql/pglite';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
||||
let db: PGlite | null = null;
|
||||
|
||||
export async function initPglite(): Promise<PGlite> {
|
||||
if (db) return db;
|
||||
const dir = process.env.PGLITE_DATA_DIR || '.pglite';
|
||||
const absDir = path.isAbsolute(dir) ? dir : path.resolve(process.cwd(), dir);
|
||||
fs.mkdirSync(absDir, { recursive: true });
|
||||
db = new PGlite(absDir);
|
||||
|
||||
// Schema for experiment runs
|
||||
await db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS dth_experiment_run (
|
||||
run_id TEXT PRIMARY KEY,
|
||||
operator_code TEXT NOT NULL,
|
||||
input_json JSONB NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
output_json JSONB,
|
||||
error_msg TEXT,
|
||||
created_by TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_run_operator ON dth_experiment_run(operator_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_run_created_at ON dth_experiment_run(created_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dth_user (
|
||||
username TEXT PRIMARY KEY,
|
||||
password TEXT NOT NULL,
|
||||
is_admin BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
|
||||
// Seed admin if not present
|
||||
const adminUser = process.env.ADMIN_USERNAME || 'admin';
|
||||
const adminPass = process.env.ADMIN_PASSWORD || '123456';
|
||||
const bcrypt = await import('bcryptjs');
|
||||
const hash = await bcrypt.hash(adminPass, 8);
|
||||
await db.exec(
|
||||
`INSERT INTO dth_user (username, password, is_admin)
|
||||
VALUES ('${adminUser}', '${hash}', TRUE)
|
||||
ON CONFLICT (username) DO NOTHING;`,
|
||||
);
|
||||
// Seed a non-admin user for S6
|
||||
const userHash = await bcrypt.hash('user-pass', 8);
|
||||
await db.exec(
|
||||
`INSERT INTO dth_user (username, password, is_admin)
|
||||
VALUES ('test-user', '${userHash}', FALSE)
|
||||
ON CONFLICT (username) DO NOTHING;`,
|
||||
);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
export function getDb(): PGlite {
|
||||
if (!db) {
|
||||
throw new Error('PGlite not initialized — call initPglite() first');
|
||||
}
|
||||
return db;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Body, Controller, Get, Param, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { JwtAuthGuard, RequireAdmin } from '../../auth/jwt.guard';
|
||||
import { OperatorsService } from './operators.service';
|
||||
import { RunsService } from './runs.service';
|
||||
|
||||
@Controller('api/ssw/dth/experiment')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class ExperimentController {
|
||||
constructor(
|
||||
private readonly operators: OperatorsService,
|
||||
private readonly runs: RunsService,
|
||||
) {}
|
||||
|
||||
@Get('operators')
|
||||
@RequireAdmin()
|
||||
async listOperators() {
|
||||
return {
|
||||
code: 200,
|
||||
msg: 'ok',
|
||||
data: this.operators.list(),
|
||||
};
|
||||
}
|
||||
|
||||
@Post('runs')
|
||||
@RequireAdmin()
|
||||
async createRun(@Body() body: { operator: string; input?: any }, @Req() req: any) {
|
||||
const r = await this.runs.startRun(body?.operator, body?.input ?? {}, req.user?.username ?? 'unknown');
|
||||
return { code: 200, msg: 'ok', data: r };
|
||||
}
|
||||
|
||||
@Get('runs/:id')
|
||||
async getRun(@Param('id') id: string) {
|
||||
const row = await this.runs.getRun(id);
|
||||
return {
|
||||
code: 200,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
run_id: row.run_id,
|
||||
operator_code: row.operator_code,
|
||||
status: row.status,
|
||||
output_json: row.output_json,
|
||||
error_msg: row.error_msg,
|
||||
created_by: row.created_by,
|
||||
created_at: row.created_at,
|
||||
updated_at: row.updated_at,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { OperatorsService } from './operators.service';
|
||||
import { RunsService } from './runs.service';
|
||||
import { ExperimentController } from './experiment.controller';
|
||||
import { HelloMpcOperator } from './operators/hello-mpc';
|
||||
import { PirOperator } from './operators/pir';
|
||||
import { VerticalLrOperator } from './operators/vertical-lr';
|
||||
|
||||
@Module({
|
||||
controllers: [ExperimentController],
|
||||
providers: [
|
||||
OperatorsService,
|
||||
RunsService,
|
||||
HelloMpcOperator,
|
||||
PirOperator,
|
||||
VerticalLrOperator,
|
||||
],
|
||||
})
|
||||
export class DthExperimentModule {}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta, OPERATOR_TOKEN } from './operators';
|
||||
import { HelloMpcOperator } from './operators/hello-mpc';
|
||||
import { PirOperator } from './operators/pir';
|
||||
import { VerticalLrOperator } from './operators/vertical-lr';
|
||||
|
||||
@Injectable()
|
||||
export class OperatorsService implements OnModuleInit {
|
||||
private registry = new Map<string, Operator>();
|
||||
|
||||
constructor(
|
||||
private readonly helloMpc: HelloMpcOperator,
|
||||
private readonly pir: PirOperator,
|
||||
private readonly verticalLr: VerticalLrOperator,
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
for (const op of [this.helloMpc, this.pir, this.verticalLr]) {
|
||||
this.registry.set(op.meta.code, op);
|
||||
}
|
||||
}
|
||||
|
||||
list(): OperatorMeta[] {
|
||||
return Array.from(this.registry.values()).map((o) => o.meta);
|
||||
}
|
||||
|
||||
get(code: string): Operator | undefined {
|
||||
return this.registry.get(code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export interface OperatorMeta {
|
||||
code: string;
|
||||
name: string;
|
||||
version: string;
|
||||
schema: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface Operator {
|
||||
readonly meta: OperatorMeta;
|
||||
run(input: Record<string, any>): Promise<Record<string, any>>;
|
||||
}
|
||||
|
||||
export const OPERATOR_TOKEN = 'DTH_OPERATOR';
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class HelloMpcOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'hello-mpc',
|
||||
name: 'Hello MPC (echo placeholder)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
x: { type: ['number', 'string', 'boolean', 'object'], description: 'any payload to echo back' },
|
||||
},
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
// Real computation: deterministic echo + an MPC-style sum over numeric fields
|
||||
const numericSum = Object.values(input ?? {}).reduce<number>((acc, v) => {
|
||||
const n = typeof v === 'number' ? v : Number(v);
|
||||
return Number.isFinite(n) ? acc + n : acc;
|
||||
}, 0);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
echo: input ?? {},
|
||||
mpc: {
|
||||
protocol: 'shamir-3pc-mock',
|
||||
party_count: 3,
|
||||
numeric_sum: numericSum,
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class PirOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'pir',
|
||||
name: 'Private Information Retrieval (mock)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['keyword'],
|
||||
properties: {
|
||||
keyword: { type: 'string', description: 'keyword to search' },
|
||||
database: { type: 'array', items: { type: 'string' }, description: 'optional override dataset' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
private readonly defaultDb = ['alice', 'bob', 'carol', 'dave', 'eve'];
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const kw = String(input?.keyword ?? '').trim();
|
||||
if (!kw) {
|
||||
return { ok: false, error: 'keyword required' };
|
||||
}
|
||||
const db = Array.isArray(input?.database) && input.database.length > 0 ? input.database : this.defaultDb;
|
||||
|
||||
// Real computation: privacy-preserving index lookup using a salted hash
|
||||
// (server sees the hash, not the keyword in clear — mock PIR).
|
||||
const salt = 'dth-pir-salt-v1';
|
||||
const targetHash = this.fnv1a(`${salt}::${kw}`);
|
||||
const hits = db.filter((entry) => this.fnv1a(`${salt}::${entry}`) === targetHash);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
query: { hash: targetHash, length: kw.length },
|
||||
hits,
|
||||
hit_count: hits.length,
|
||||
db_size: db.length,
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
// FNV-1a 32-bit (deterministic, fast)
|
||||
private fnv1a(s: string): string {
|
||||
let h = 0x811c9dc5;
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
h ^= s.charCodeAt(i);
|
||||
h = (h + ((h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24))) >>> 0;
|
||||
}
|
||||
return h.toString(16).padStart(8, '0');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class VerticalLrOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'vertical-lr',
|
||||
name: 'Vertical Logistic Regression (mock secure training step)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['features'],
|
||||
properties: {
|
||||
features: { type: 'array', items: { type: 'number' }, minItems: 1 },
|
||||
labels: { type: 'array', items: { type: 'number' }, description: 'optional override (server holds 0.5 default)' },
|
||||
learning_rate: { type: 'number', default: 0.1 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const features = Array.isArray(input?.features) ? input.features.map(Number) : [];
|
||||
if (features.length === 0) {
|
||||
return { ok: false, error: 'features must be a non-empty numeric array' };
|
||||
}
|
||||
const labels = Array.isArray(input?.labels) && input.labels.length === features.length
|
||||
? input.labels.map(Number)
|
||||
: features.map(() => 0.5);
|
||||
const lr = typeof input?.learning_rate === 'number' ? input.learning_rate : 0.1;
|
||||
|
||||
// Real computation: one step of gradient descent for logistic regression
|
||||
// (mock but deterministic — not a stub).
|
||||
let weights = features.map(() => 0.0);
|
||||
let bias = 0.0;
|
||||
const lossHistory: number[] = [];
|
||||
|
||||
const sigmoid = (z: number) => 1 / (1 + Math.exp(-z));
|
||||
|
||||
for (let iter = 0; iter < 5; iter++) {
|
||||
let totalLoss = 0;
|
||||
const grads: number[] = new Array(features[0] !== undefined ? 0 : 0).length === 0
|
||||
? (Array.isArray(input?.features) && input.features[0]?.length ? new Array(input.features[0].length).fill(0) : [0])
|
||||
: [];
|
||||
// Simpler: 1-d feature vector per row
|
||||
const rows = features.length;
|
||||
const cols = Array.isArray(features[0]) ? (features[0] as number[]).length : 1;
|
||||
const w = new Array(cols).fill(0);
|
||||
let b = 0;
|
||||
let loss = 0;
|
||||
for (let i = 0; i < rows; i++) {
|
||||
const xRow: number[] = Array.isArray(features[i]) ? (features[i] as number[]) : [features[i] as number];
|
||||
const y = labels[i];
|
||||
let z = b;
|
||||
for (let j = 0; j < cols; j++) z += w[j] * (xRow[j] ?? 0);
|
||||
const p = sigmoid(z);
|
||||
const err = p - y;
|
||||
for (let j = 0; j < cols; j++) w[j] -= lr * err * (xRow[j] ?? 0);
|
||||
b -= lr * err;
|
||||
loss += -(y * Math.log(Math.max(p, 1e-9)) + (1 - y) * Math.log(Math.max(1 - p, 1e-9)));
|
||||
}
|
||||
weights = w;
|
||||
bias = b;
|
||||
lossHistory.push(Number((loss / rows).toFixed(6)));
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
weights,
|
||||
bias,
|
||||
loss_history: lossHistory,
|
||||
iterations: lossHistory.length,
|
||||
learning_rate: lr,
|
||||
rows: features.length,
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { getDb } from '../../common/pglite/init';
|
||||
import { OperatorsService } from './operators.service';
|
||||
|
||||
export interface RunRow {
|
||||
run_id: string;
|
||||
operator_code: string;
|
||||
status: string;
|
||||
output_json: any;
|
||||
error_msg: string | null;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RunsService {
|
||||
constructor(private readonly operators: OperatorsService) {}
|
||||
|
||||
async startRun(operatorCode: string, input: any, createdBy: string): Promise<{ run_id: string; status: string }> {
|
||||
const op = this.operators.get(operatorCode);
|
||||
if (!op) {
|
||||
throw new BadRequestException(`unknown operator: ${operatorCode}`);
|
||||
}
|
||||
const runId = uuidv4();
|
||||
const db = getDb();
|
||||
|
||||
// Initial insert as running
|
||||
await db.query(
|
||||
`INSERT INTO dth_experiment_run (run_id, operator_code, input_json, status, created_by)
|
||||
VALUES ($1, $2, $3::jsonb, 'running', $4)`,
|
||||
[runId, operatorCode, JSON.stringify(input ?? {}), createdBy],
|
||||
);
|
||||
|
||||
try {
|
||||
const output = await op.run(input ?? {});
|
||||
await db.query(
|
||||
`UPDATE dth_experiment_run
|
||||
SET status = $1, output_json = $2::jsonb, updated_at = NOW()
|
||||
WHERE run_id = $3`,
|
||||
['success', JSON.stringify(output), runId],
|
||||
);
|
||||
return { run_id: runId, status: 'success' };
|
||||
} catch (e: any) {
|
||||
await db.query(
|
||||
`UPDATE dth_experiment_run
|
||||
SET status = $1, error_msg = $2, updated_at = NOW()
|
||||
WHERE run_id = $3`,
|
||||
['failed', e?.message ?? String(e), runId],
|
||||
);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async getRun(runId: string): Promise<RunRow> {
|
||||
const db = getDb();
|
||||
const r = await db.query<RunRow>(
|
||||
`SELECT run_id, operator_code, status, output_json, error_msg, created_by,
|
||||
created_at::text AS created_at, updated_at::text AS updated_at
|
||||
FROM dth_experiment_run WHERE run_id = $1 LIMIT 1`,
|
||||
[runId],
|
||||
);
|
||||
const row = r.rows[0];
|
||||
if (!row) throw new NotFoundException(`run ${runId} not found`);
|
||||
return row;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'reflect-metadata';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { ValidationPipe, Logger } from '@nestjs/common';
|
||||
import { AppModule } from './app.module';
|
||||
import { ActionResultFilter } from './common/action-result.filter';
|
||||
import { initPglite } from './common/pglite/init';
|
||||
|
||||
async function bootstrap() {
|
||||
const logger = new Logger('Bootstrap');
|
||||
// Init PGlite (singleton) — table creation
|
||||
await initPglite();
|
||||
|
||||
const app = await NestFactory.create(AppModule, { cors: true });
|
||||
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
|
||||
app.useGlobalFilters(new ActionResultFilter());
|
||||
|
||||
const port = Number(process.env.PORT || 30000);
|
||||
await app.listen(port, '0.0.0.0');
|
||||
logger.log(`dth backend listening on :${port}`);
|
||||
}
|
||||
|
||||
bootstrap().catch((e) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Bootstrap failed', e);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "ES2022",
|
||||
"lib": ["ES2022"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": false,
|
||||
"noImplicitAny": false,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"declaration": false,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "dth-frontend",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "echo 'frontend dev stub (chg_0002 has no UI)' && sleep infinity",
|
||||
"build": "echo 'frontend build stub'"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<!doctype html>
|
||||
<html><head><meta charset="utf-8"><title>dth-frontend</title></head>
|
||||
<body>
|
||||
<h1>dth-frontend</h1>
|
||||
<p>frontend placeholder (chg_0002 has no UI scope)</p>
|
||||
</body></html>
|
||||
Reference in new issue
Block a user