import type { JsonObject } from "../state/json.js"; import type { JsonPatch } from "../workflows/json-patch.js"; export type { JsonPrimitive, JsonValue, JsonObject } from "../state/json.js"; export type MaybePromise = T | Promise; export type ManagedResourceConditionStatus = true | false | "unknown"; export type ManagedResourceCondition = { type: string; status: ManagedResourceConditionStatus; reason: string; message?: string; observedGeneration: number; lastTransitionTime: string; }; export type ChildWorkflowState = | "pending" | "running" | "waiting" | "succeeded" | "failed" | "interrupted"; export type ChildWorkflowReference = { requestId: string; runId?: string; state: ChildWorkflowState; attempt: number; }; export type ManagedResourceStatus = { observedGeneration: number; conditions: ManagedResourceCondition[]; resourceManagerStatus: TStatus; workflowRun?: ChildWorkflowReference; }; export type ManagedResource = { metadata: { uid: string; resourceManager: string; key: string; resourceVersion: number; generation: number; deletionTimestamp?: string; finalizers: string[]; }; spec: TSpec; status: ManagedResourceStatus; }; export type ManagedResourceRef = { resourceManager: string; key: string; }; export type ManagedResourceConditionInput = { type: string; status: ManagedResourceConditionStatus; reason: string; message?: string; }; export type ResourceManagerStatusPatch = { resourceManagerStatus?: TStatus; conditions?: ManagedResourceConditionInput[]; workflowRun?: ChildWorkflowReference | null; finalizers?: string[]; }; export type ReconcileResult = | { kind: "settled"; status?: ResourceManagerStatusPatch; } | { kind: "requeue"; afterMs?: number; status?: ResourceManagerStatusPatch; }; export type EffectState = "pending" | "applied" | "rejected" | "indeterminate"; export type EffectRecord = { key: string; resourceUid: string; generation: number; kind: string; state: EffectState; requestFingerprint: string; startedAt: string; completedAt?: string; externalRef?: string; error?: string; }; export type EffectObservation = | { state: "applied"; externalRef?: string } | { state: "not_applied" } | { state: "indeterminate" }; export type EffectApplication = | { state: "applied"; externalRef?: string } | { state: "rejected"; error: string } | { state: "indeterminate"; error?: string }; export type EffectDefinition = { key: string; kind: string; request: TRequest; observe: (signal: AbortSignal) => MaybePromise; apply: (signal: AbortSignal) => MaybePromise; }; export interface ResourceManagerEffects { ensure(definition: EffectDefinition): Promise; } export type ChildWorkflowRequest = { requestKey: string; workflow: string; input: unknown; }; export type ChildWorkflowRecord = ChildWorkflowReference & { resourceUid: string; requestKey: string; inputFingerprint: string; workflow: string; error?: string; }; export type ResourceManagerSettingsChangeRequest = { requestKey: string; runId: string; scopeId?: string; expectedChangeNumber?: number; patch: JsonPatch; }; export type ResourceManagerSettingsChangeResult = { runId: string; scopeId: string; changeNumber: number; adopted: boolean; }; export type ResourceManagerQueueFollowUpRequest = { requestKey: string; runId: string; prompt: string; }; export type ResourceManagerFollowUpResult = { runId: string; followUpId: string; order: number; state: string; adopted: boolean; }; export type ResourceManagerRemoveFollowUpRequest = { requestKey: string; runId: string; followUpId: string; }; export interface ResourceManagerWorkflows { ensure(request: ChildWorkflowRequest): Promise; changeSettings( request: ResourceManagerSettingsChangeRequest, ): Promise; queueFollowUp( request: ResourceManagerQueueFollowUpRequest, ): Promise; removeFollowUp( request: ResourceManagerRemoveFollowUpRequest, ): Promise; } export type ReconcileContext = { signal: AbortSignal; effects: ResourceManagerEffects; workflows: ResourceManagerWorkflows; settled(status?: ResourceManagerStatusPatch): ReconcileResult; requeue(status?: ResourceManagerStatusPatch): ReconcileResult; requeueAfter( afterMs: number, status?: ResourceManagerStatusPatch, ): ReconcileResult; }; export type ResourceManagerDefinition = { name: string; initialStatus: (spec: TSpec) => TStatus; reconcile: ( context: ReconcileContext, resource: ManagedResource, ) => MaybePromise>; timeoutMs?: number; }; export type AnyResourceManagerDefinition = { name: string; initialStatus(spec: unknown): unknown; reconcile( context: ReconcileContext, resource: ManagedResource, ): MaybePromise>; timeoutMs?: number; }; export type ResourceManagerQueueClaim = { resourceManager: string; key: string; ownerId: string; token: string; generation: number; queueVersion: number; resourceVersion: number; consecutiveErrors: number; expiresAt: string; }; export type ResourceManagerEvent = { seq: number; recordedAt: string; resourceManager: string; key: string; type: string; payload: JsonObject; };