feat: complete TASK-WP0-02 contract baseline
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
"typecheck": "tsc --noEmit -p tsconfig.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@dada/shared-contracts": "workspace:*",
|
||||
"@vibrant/core": "4.0.4",
|
||||
"@vibrant/quantizer-mmcq": "4.0.4",
|
||||
"fabric": "7.4.0",
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { isSseEvent, type SseEvent } from "@dada/shared-contracts";
|
||||
|
||||
export interface EventSyncDependencies {
|
||||
bootstrap: () => Promise<unknown>;
|
||||
refetchEntity: (entityRef: string) => Promise<unknown>;
|
||||
refetchModels: (input: {
|
||||
configSetVersion?: number;
|
||||
reason: "config" | "runtime";
|
||||
runtimeAvailabilityVersion?: number;
|
||||
}) => Promise<unknown>;
|
||||
}
|
||||
|
||||
export interface EventSyncController {
|
||||
handleDisconnect: () => Promise<void>;
|
||||
handleEvent: (event: unknown) => Promise<void>;
|
||||
}
|
||||
|
||||
async function refetchEventTruth(dependencies: EventSyncDependencies, event: SseEvent) {
|
||||
if ("config_set_version" in event) {
|
||||
await dependencies.refetchModels({
|
||||
configSetVersion: event.config_set_version,
|
||||
reason: "config",
|
||||
});
|
||||
return;
|
||||
}
|
||||
if ("runtime_availability_version" in event) {
|
||||
await dependencies.refetchModels({
|
||||
reason: "runtime",
|
||||
runtimeAvailabilityVersion: event.runtime_availability_version,
|
||||
});
|
||||
return;
|
||||
}
|
||||
await dependencies.refetchEntity(event.entity_ref);
|
||||
}
|
||||
|
||||
export function createEventSyncController(dependencies: EventSyncDependencies): EventSyncController {
|
||||
let lastEventId: number | undefined;
|
||||
let recoveryInProgress = false;
|
||||
|
||||
async function recoverOnce() {
|
||||
if (recoveryInProgress) return;
|
||||
recoveryInProgress = true;
|
||||
await dependencies.bootstrap();
|
||||
}
|
||||
|
||||
return {
|
||||
async handleDisconnect() {
|
||||
await recoverOnce();
|
||||
},
|
||||
async handleEvent(event: unknown) {
|
||||
if (!isSseEvent(event)) throw new Error("SSE event failed the non-sensitive schema.");
|
||||
|
||||
if (lastEventId !== undefined && event.event_id !== lastEventId + 1) {
|
||||
await recoverOnce();
|
||||
}
|
||||
lastEventId = event.event_id;
|
||||
|
||||
await refetchEventTruth(dependencies, event);
|
||||
|
||||
recoveryInProgress = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function connectEventSource(url: string, controller: EventSyncController) {
|
||||
const source = new EventSource(url, { withCredentials: true });
|
||||
source.onmessage = (message) => {
|
||||
void controller.handleEvent(JSON.parse(message.data) as SseEvent);
|
||||
};
|
||||
source.onerror = () => {
|
||||
void controller.handleDisconnect();
|
||||
};
|
||||
return source;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Generated from openapi/openapi.json. Do not edit by hand.
|
||||
|
||||
export interface ClientOptions { baseUrl?: string; fetch?: typeof globalThis.fetch; headers?: HeadersInit; }
|
||||
|
||||
export async function getBootstrap(options: ClientOptions = {}): Promise<{
|
||||
"app_version": string;
|
||||
"dependencies": Array<{
|
||||
"service_id": string;
|
||||
"status": "available" | "paused" | "degraded" | "unavailable";
|
||||
}>;
|
||||
"model_summary": {
|
||||
"config_set_version": number | null;
|
||||
"configured_default_model_id": string | null;
|
||||
"recommended_model_id": string | null;
|
||||
"runtime_availability_version": number | null;
|
||||
};
|
||||
"public_features": Array<{
|
||||
"feature_id": string;
|
||||
"status": "enabled" | "disabled" | "paused";
|
||||
}>;
|
||||
}> {
|
||||
const request = options.fetch ?? globalThis.fetch;
|
||||
const response = await request(`${options.baseUrl ?? ""}/api/v1/bootstrap`, { method: "GET", headers: options.headers ?? {} });
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
return response.json() as Promise<{
|
||||
"app_version": string;
|
||||
"dependencies": Array<{
|
||||
"service_id": string;
|
||||
"status": "available" | "paused" | "degraded" | "unavailable";
|
||||
}>;
|
||||
"model_summary": {
|
||||
"config_set_version": number | null;
|
||||
"configured_default_model_id": string | null;
|
||||
"recommended_model_id": string | null;
|
||||
"runtime_availability_version": number | null;
|
||||
};
|
||||
"public_features": Array<{
|
||||
"feature_id": string;
|
||||
"status": "enabled" | "disabled" | "paused";
|
||||
}>;
|
||||
}>;
|
||||
}
|
||||
|
||||
export function getEvents(options: Pick<ClientOptions, "baseUrl"> = {}): string {
|
||||
return `${options.baseUrl ?? ""}/api/v1/events`;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// Generated from openapi/openapi.json. Do not edit by hand.
|
||||
|
||||
export type BootstrapResponse = {
|
||||
"app_version": string;
|
||||
"dependencies": Array<{
|
||||
"service_id": string;
|
||||
"status": "available" | "paused" | "degraded" | "unavailable";
|
||||
}>;
|
||||
"model_summary": {
|
||||
"config_set_version": number | null;
|
||||
"configured_default_model_id": string | null;
|
||||
"recommended_model_id": string | null;
|
||||
"runtime_availability_version": number | null;
|
||||
};
|
||||
"public_features": Array<{
|
||||
"feature_id": string;
|
||||
"status": "enabled" | "disabled" | "paused";
|
||||
}>;
|
||||
};
|
||||
|
||||
export type CorrelationId = string;
|
||||
|
||||
export type ErrorDetails = {
|
||||
"capacity_status"?: "normal" | "warning" | "critical" | "full" | "unavailable";
|
||||
"current_task_ref"?: string;
|
||||
"field_errors"?: Array<{
|
||||
"field": string;
|
||||
"message_key": string;
|
||||
}>;
|
||||
"latest_version"?: number | string;
|
||||
"remaining_bytes"?: number;
|
||||
};
|
||||
|
||||
export type ErrorEnvelope = {
|
||||
"error": {
|
||||
"code": "BROWSER_UNSUPPORTED" | "MODEL_CONFIG_VERSION_CONFLICT" | "MODEL_DEFAULT_REPLACEMENT_REQUIRED" | "MODEL_DEFAULT_REPLACEMENT_INVALID" | "MODEL_RECOMMENDATION_PRIORITY_INVALID" | "MODEL_RECOMMENDATION_PRIORITY_CONFLICT" | "PRIVATE_CONTENT_NOTICE_ACK_REQUIRED" | "ASSET_HISTORY_REFERENCE_CONFLICT" | "ASSET_CLEANUP_CANDIDATE_STALE" | "STORAGE_CAPACITY_EXCEEDED";
|
||||
"correlation_id": string;
|
||||
"details": {
|
||||
"capacity_status"?: "normal" | "warning" | "critical" | "full" | "unavailable";
|
||||
"current_task_ref"?: string;
|
||||
"field_errors"?: Array<{
|
||||
"field": string;
|
||||
"message_key": string;
|
||||
}>;
|
||||
"latest_version"?: number | string;
|
||||
"remaining_bytes"?: number;
|
||||
};
|
||||
"error_category"?: "upstream_timeout" | "upstream_failed" | "safety_rejected" | "model_disabled" | "gateway_balance_insufficient" | "gateway_contract_invalid" | "reference_invalid" | "unknown_retryable" | "unknown_non_retryable";
|
||||
"message_key": string;
|
||||
};
|
||||
};
|
||||
|
||||
export type GenerationErrorCategory = "upstream_timeout" | "upstream_failed" | "safety_rejected" | "model_disabled" | "gateway_balance_insufficient" | "gateway_contract_invalid" | "reference_invalid" | "unknown_retryable" | "unknown_non_retryable";
|
||||
|
||||
export type ModelConfigSseEvent = {
|
||||
"config_set_version": number;
|
||||
"entity_ref": string;
|
||||
"event_id": number;
|
||||
"event_type": "model_config_changed";
|
||||
"occurred_at": string;
|
||||
};
|
||||
|
||||
export type ModelRuntimeSseEvent = {
|
||||
"entity_ref": string;
|
||||
"event_id": number;
|
||||
"event_type": "model_runtime_changed";
|
||||
"occurred_at": string;
|
||||
"runtime_availability_version": number;
|
||||
};
|
||||
|
||||
export type SseEvent = {
|
||||
"entity_ref": string;
|
||||
"event_id": number;
|
||||
"event_type": string;
|
||||
"occurred_at": string;
|
||||
"state_version": number;
|
||||
} | {
|
||||
"config_set_version": number;
|
||||
"entity_ref": string;
|
||||
"event_id": number;
|
||||
"event_type": "model_config_changed";
|
||||
"occurred_at": string;
|
||||
} | {
|
||||
"entity_ref": string;
|
||||
"event_id": number;
|
||||
"event_type": "model_runtime_changed";
|
||||
"occurred_at": string;
|
||||
"runtime_availability_version": number;
|
||||
};
|
||||
|
||||
export type StableEngineeringErrorCode = "BROWSER_UNSUPPORTED" | "MODEL_CONFIG_VERSION_CONFLICT" | "MODEL_DEFAULT_REPLACEMENT_REQUIRED" | "MODEL_DEFAULT_REPLACEMENT_INVALID" | "MODEL_RECOMMENDATION_PRIORITY_INVALID" | "MODEL_RECOMMENDATION_PRIORITY_CONFLICT" | "PRIVATE_CONTENT_NOTICE_ACK_REQUIRED" | "ASSET_HISTORY_REFERENCE_CONFLICT" | "ASSET_CLEANUP_CANDIDATE_STALE" | "STORAGE_CAPACITY_EXCEEDED";
|
||||
|
||||
export type StateSseEvent = {
|
||||
"entity_ref": string;
|
||||
"event_id": number;
|
||||
"event_type": string;
|
||||
"occurred_at": string;
|
||||
"state_version": number;
|
||||
};
|
||||
Reference in New Issue
Block a user