fix: add missing 0pt platform clients + registry services, wire test/typecheck scripts
The 0pt data-engine build has been broken on main since 2026-07-08 (commit
c897311). document-capture-service.ts, document-extraction-service.ts,
source-query-service.ts, external-writeback-service.ts (and, more recently,
dataset-iceberg-mapping-service.ts, iceberg-write-service.ts,
source-rate-policy-service.ts) all import six platform client modules and two
registry services that were never committed: src/clients/{aud0,cl0ud,n0d,
st0re}-client.ts, src/services/{dataset,source}-service.ts, plus
src/types/{dataset,source}.ts. `next build` failed with 17 "Module not found"
errors, and every route under app/api/0pt/* returned HTTP 500 in dev with the
Turbopack compile error leaked straight into the JSON response body.
This adds the missing files, following the same fetch-based client shape
already established by src/clients/st0re-lakehouse-client.ts (thin interface,
HTTP implementation, deterministic in-memory mock used when no base URL is
configured) and the same "pure core, thin in-memory registry, AUD0-evented
mutation" shape the existing SourceRatePolicyService/DatasetIcebergMappingService
already use for DatasetService/SourceService. Each new client mock is
documented as a mock/degrade path, never a fabricated success, per this
repo's CLAUDE.md honesty doctrine.
Also wires up "typecheck" and "test" npm scripts (tsc --noEmit / vitest run)
and adds vitest as a devDependency -- previously `pnpm test` failed
immediately with ERR_PNPM_NO_SCRIPT even though 13 test files (~89 cases)
already existed under tests/p0/.
Verified: `pnpm run typecheck` clean, `pnpm run test` 88 passed / 1 todo
(pre-existing live-ST0RE-only skip), `pnpm run build` exits 0 (39/39 routes
compiled, only the pre-existing intentional optional `mssql` import warning
remains). Re-verified live on port 4097: GET /api/0pt/sources/rate-policies,
GET /api/0pt/lakehouse/mappings, POST /api/0pt/documents/capture, GET
/api/0pt/documents/[id]/extraction, and both GET/POST
/api/0pt/sources/[id]/query now return real application responses (200/400/405)
instead of HTTP 500 with a leaked compile error.
Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5
parent
35b6ec63bf
commit
01cc225ffc
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* src/services/dataset-service.ts
|
||||
* ==================================
|
||||
* Owns the 0pt.dataset registry -- the thing `DatasetIcebergMappingService`
|
||||
* (src/services/dataset-iceberg-mapping-service.ts) and `IcebergWriteService`
|
||||
* (src/services/iceberg-write-service.ts) look a `dataset_id` up against
|
||||
* before pointing an Iceberg table mapping or a write at it. Deliberately
|
||||
* small and in-memory, same "pure core, thin registry" shape every other
|
||||
* P0 service in this repo already uses (SourceService, SourceRatePolicyService,
|
||||
* DatasetIcebergMappingService) -- this is NOT dataset content, schema, or
|
||||
* quality state; see docs/ICEBERG_WRITE_BOUNDARY.md for that boundary.
|
||||
*
|
||||
* In-memory scope: each `new DatasetService()` owns its own registry, same
|
||||
* pre-existing limitation source-service.ts and every other P0 registry
|
||||
* service in this repo documents (see docs/P0_QUERY_SOURCE_FLOW.md's "Known
|
||||
* gaps" section) -- callers that need a shared view across API routes pass
|
||||
* an existing instance in via the `datasetService` constructor option,
|
||||
* exactly like IcebergWriteService and DatasetIcebergMappingService already
|
||||
* do for each other.
|
||||
*/
|
||||
import { createAud0Client, type Aud0Client } from "../clients/aud0-client";
|
||||
import type { Dataset, DatasetStatus, DatasetType, CreateDatasetInput } from "../types/dataset";
|
||||
|
||||
interface DatasetRecord {
|
||||
dataset_id: string;
|
||||
tenant_id: string;
|
||||
project_id: string;
|
||||
name: string;
|
||||
dataset_type: string;
|
||||
status: string;
|
||||
description: string | null;
|
||||
correlation_id: string | null;
|
||||
created_at: string;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export class DatasetService {
|
||||
private datasets: Map<string, DatasetRecord> = new Map();
|
||||
private aud0: Aud0Client;
|
||||
|
||||
constructor(aud0BaseUrl?: string) {
|
||||
this.aud0 = createAud0Client(aud0BaseUrl);
|
||||
}
|
||||
|
||||
async registerDataset(input: CreateDatasetInput): Promise<Dataset> {
|
||||
if (this.datasets.has(input.dataset_id)) {
|
||||
throw new Error(`Dataset ${input.dataset_id} already exists`);
|
||||
}
|
||||
const now = new Date().toISOString();
|
||||
const record: DatasetRecord = {
|
||||
dataset_id: input.dataset_id,
|
||||
tenant_id: input.tenant_id,
|
||||
project_id: input.project_id,
|
||||
name: input.name,
|
||||
dataset_type: input.dataset_type,
|
||||
status: input.status ?? "active",
|
||||
description: input.description ?? null,
|
||||
correlation_id: input.correlation_id ?? null,
|
||||
created_at: now,
|
||||
updated_at: null,
|
||||
};
|
||||
this.datasets.set(input.dataset_id, record);
|
||||
|
||||
// 0pt.dataset_registered -- same "no mutation without an evented trail"
|
||||
// doctrine every other P0 registry service in this repo follows
|
||||
// (SourceService.createSource, SourceRatePolicyService.createSourceRatePolicy).
|
||||
await this.aud0.ingestEvent({
|
||||
event_id: `evt_0pt_dataset_registered_${input.dataset_id}`,
|
||||
event_type: "0pt.dataset_registered",
|
||||
tenant_id: input.tenant_id,
|
||||
project_id: input.project_id,
|
||||
actor_id: "system",
|
||||
app_id: "0pt",
|
||||
capability_id: "0pt.register_dataset",
|
||||
resource_type: "dataset",
|
||||
resource_id: input.dataset_id,
|
||||
risk_level: "L2",
|
||||
payload: { name: input.name, dataset_type: input.dataset_type },
|
||||
created_at: now,
|
||||
correlation_id: input.correlation_id ?? "",
|
||||
});
|
||||
|
||||
return this.toDataset(record);
|
||||
}
|
||||
|
||||
async getDataset(datasetId: string): Promise<Dataset> {
|
||||
const record = this.datasets.get(datasetId);
|
||||
if (!record) throw new Error(`Dataset ${datasetId} not found`);
|
||||
return this.toDataset(record);
|
||||
}
|
||||
|
||||
async listDatasets(filter?: { tenant_id?: string; project_id?: string; status?: DatasetStatus }): Promise<Dataset[]> {
|
||||
let results = Array.from(this.datasets.values());
|
||||
if (filter?.tenant_id) results = results.filter((r) => r.tenant_id === filter.tenant_id);
|
||||
if (filter?.project_id) results = results.filter((r) => r.project_id === filter.project_id);
|
||||
if (filter?.status) results = results.filter((r) => r.status === filter.status);
|
||||
return results.map((r) => this.toDataset(r));
|
||||
}
|
||||
|
||||
private toDataset(r: DatasetRecord): Dataset {
|
||||
return {
|
||||
dataset_id: r.dataset_id,
|
||||
tenant_id: r.tenant_id,
|
||||
project_id: r.project_id,
|
||||
name: r.name,
|
||||
dataset_type: r.dataset_type as DatasetType,
|
||||
status: r.status as DatasetStatus,
|
||||
description: r.description ?? undefined,
|
||||
correlation_id: r.correlation_id ?? undefined,
|
||||
created_at: r.created_at,
|
||||
updated_at: r.updated_at ?? undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* src/services/source-service.ts
|
||||
* =================================
|
||||
* Owns the 0pt.source registry -- a per-tenant registration of something
|
||||
* 0pt can query or crawl against (source_type "database" | "web" | "api" |
|
||||
* "file", carrying an `adapter_id` for the database case). This is the
|
||||
* registry `SourceQueryService.querySource()` (src/services/source-query-service.ts)
|
||||
* and `SourceRatePolicyService` (src/services/source-rate-policy-service.ts)
|
||||
* both look a `source_id` up against -- see docs/P0_QUERY_SOURCE_FLOW.md.
|
||||
*
|
||||
* Deliberately small and in-memory, same "pure core, thin registry" shape
|
||||
* every other P0 service in this repo already uses. In-memory scope: each
|
||||
* `new SourceService()` owns its own registry -- the same pre-existing
|
||||
* limitation docs/P0_QUERY_SOURCE_FLOW.md's "Known gaps" section documents
|
||||
* (a Source registered via one API route module is not yet visible to
|
||||
* another route module in the same running process). Callers that need a
|
||||
* shared view pass an existing instance in via the `sourceService`
|
||||
* constructor option, exactly like SourceQueryService/SourceRatePolicyService
|
||||
* already do.
|
||||
*/
|
||||
import { createAud0Client, type Aud0Client } from "../clients/aud0-client";
|
||||
import type { Source, SourceStatus, SourceType, CreateSourceInput } from "../types/source";
|
||||
|
||||
interface SourceRecord {
|
||||
source_id: string;
|
||||
tenant_id: string;
|
||||
project_id: string;
|
||||
source_type: string;
|
||||
name: string;
|
||||
adapter_id: string | null;
|
||||
access: string | null;
|
||||
status: string;
|
||||
correlation_id: string | null;
|
||||
created_at: string;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export class SourceService {
|
||||
private sources: Map<string, SourceRecord> = new Map();
|
||||
private aud0: Aud0Client;
|
||||
|
||||
constructor(aud0BaseUrl?: string) {
|
||||
this.aud0 = createAud0Client(aud0BaseUrl);
|
||||
}
|
||||
|
||||
async createSource(input: CreateSourceInput): Promise<Source> {
|
||||
if (this.sources.has(input.source_id)) {
|
||||
throw new Error(`Source ${input.source_id} already exists`);
|
||||
}
|
||||
const now = new Date().toISOString();
|
||||
const record: SourceRecord = {
|
||||
source_id: input.source_id,
|
||||
tenant_id: input.tenant_id,
|
||||
project_id: input.project_id,
|
||||
source_type: input.source_type,
|
||||
name: input.name,
|
||||
adapter_id: input.adapter_id ?? null,
|
||||
access: input.access ?? null,
|
||||
status: input.status ?? "active",
|
||||
correlation_id: input.correlation_id ?? null,
|
||||
created_at: now,
|
||||
updated_at: null,
|
||||
};
|
||||
this.sources.set(input.source_id, record);
|
||||
|
||||
// 0pt.source_created -- same "no mutation without an evented trail"
|
||||
// doctrine every other P0 registry service in this repo follows.
|
||||
await this.aud0.ingestEvent({
|
||||
event_id: `evt_0pt_source_created_${input.source_id}`,
|
||||
event_type: "0pt.source_created",
|
||||
tenant_id: input.tenant_id,
|
||||
project_id: input.project_id,
|
||||
actor_id: "system",
|
||||
app_id: "0pt",
|
||||
capability_id: "0pt.create_source",
|
||||
resource_type: "source",
|
||||
resource_id: input.source_id,
|
||||
risk_level: "L2",
|
||||
payload: { source_type: input.source_type, name: input.name, adapter_id: input.adapter_id },
|
||||
created_at: now,
|
||||
correlation_id: input.correlation_id ?? "",
|
||||
});
|
||||
|
||||
return this.toSource(record);
|
||||
}
|
||||
|
||||
async getSource(sourceId: string): Promise<Source> {
|
||||
const record = this.sources.get(sourceId);
|
||||
if (!record) throw new Error(`Source ${sourceId} not found`);
|
||||
return this.toSource(record);
|
||||
}
|
||||
|
||||
async listSources(filter?: { tenant_id?: string; project_id?: string; status?: SourceStatus }): Promise<Source[]> {
|
||||
let results = Array.from(this.sources.values());
|
||||
if (filter?.tenant_id) results = results.filter((r) => r.tenant_id === filter.tenant_id);
|
||||
if (filter?.project_id) results = results.filter((r) => r.project_id === filter.project_id);
|
||||
if (filter?.status) results = results.filter((r) => r.status === filter.status);
|
||||
return results.map((r) => this.toSource(r));
|
||||
}
|
||||
|
||||
private toSource(r: SourceRecord): Source {
|
||||
return {
|
||||
source_id: r.source_id,
|
||||
tenant_id: r.tenant_id,
|
||||
project_id: r.project_id,
|
||||
source_type: r.source_type as SourceType,
|
||||
name: r.name,
|
||||
adapter_id: r.adapter_id ?? undefined,
|
||||
access: (r.access as Source["access"]) ?? undefined,
|
||||
status: r.status as SourceStatus,
|
||||
correlation_id: r.correlation_id ?? undefined,
|
||||
created_at: r.created_at,
|
||||
updated_at: r.updated_at ?? undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user