import { type CallOptions, Metadata } from '@grpc/grpc-js'; import { Dayjs } from 'dayjs'; import { Status } from '../api/google/rpc/index.js'; import { Request, RetryOptions } from './request.js'; import { custom, customJson, Logger } from './util/logging.js'; /** Contains the default interval between successful operation polls. */ export declare const DEFAULT_POLL_INTERVAL_SEC = 1; /** Contains the maximum default delay after a retriable polling error. */ export declare const DEFAULT_POLL_ERROR_BACKOFF_MAX_MS = 30000; /** Calculates the delay in milliseconds after a retriable polling error. */ export type PollErrorBackoff = (attempt: number) => number; /** Controls operation polling requests and retries. */ export interface OperationWaitOptions extends RetryOptions { /** * Calculates the delay after each consecutive retriable polling error. * * The attempt starts at 1 and resets after a successful poll. By default, * the delay starts at one second, uses exponential backoff with 20% jitter, * and is capped at 30 seconds. Set this value to `null` to disable retries * after polling errors. */ pollErrorBackoff?: PollErrorBackoff | null; } /** * Defines a protobuf-compatible progress count. * * Generated code can represent an integer as a JavaScript number or as an * object that converts to a number or string. */ export type TickCount = number | { toNumber?: () => number; toString?: () => string; }; /** Defines completed and total work counts reported by a service. */ export interface ProgressTrackerWorkDone { /** Contains the total amount of work. */ totalTickCount?: TickCount | undefined; /** Contains the completed amount of work. */ doneTickCount?: TickCount | undefined; } /** Defines one progress step from an operation response. */ export interface ProgressTrackerStep { /** Contains the description. */ description?: string | undefined; /** Contains the start time. */ startedAt?: Dayjs | undefined; /** Contains the finish time. */ finishedAt?: Dayjs | undefined; /** Contains the work done. */ workDone?: ProgressTrackerWorkDone | undefined; } /** Defines progress data from an operation response. */ export interface ProgressTrackerProto { /** Contains the description. */ description?: string | undefined; /** Contains the start time. */ startedAt?: Dayjs | undefined; /** Contains the finish time. */ finishedAt?: Dayjs | undefined; /** Contains the estimated finish time. */ estimatedFinishedAt?: Dayjs | undefined; /** Contains the work done. */ workDone?: ProgressTrackerWorkDone | undefined; /** Contains the steps. */ steps?: ProgressTrackerStep[] | undefined; } /** * Describes one step in an operation. * * A service can omit steps. It can also return only active steps or some * completed steps. * * @example * ```ts * const tracker = op.progressTracker(); * if (tracker) { * for (const step of tracker.steps()) { * const fraction = step.workFraction(); * if (fraction === undefined) { * console.log(step.description()); * } else { * console.log(`${step.description()}: ${Math.round(fraction * 100)}%`); * } * } * } * ``` */ export interface CurrentStep { /** Returns a human-readable step description. */ description(): string; /** Returns the step start time when the service provides it. */ startedAt(): Dayjs | undefined; /** Returns the step finish time when the service provides it. */ finishedAt(): Dayjs | undefined; /** Returns work counts when the service provides them. */ workDone(): ProgressTrackerWorkDone | undefined; /** * Returns the completed work as a value from 0 to 1. * * Returns `undefined` when the work counts are missing or invalid. */ workFraction(): number | undefined; /** Returns a text form for logs. */ toString(): string; /** Returns a safe value for JSON logs. */ [customJson](): unknown; } /** * Reports progress for a long-running operation. * * {@link Operation.progressTracker} returns `undefined` when the service does * not provide progress. * * @example * ```ts * const tracker = op.progressTracker(); * if (tracker) { * console.log(tracker.description()); * const work = tracker.workFraction(); * if (work !== undefined) console.log(`Work: ${Math.round(work * 100)}%`); * const time = tracker.timeFraction(); * if (time !== undefined) console.log(`Time: ${Math.round(time * 100)}%`); * } * ``` */ export interface OperationProgressTracker extends CurrentStep { /** * Returns the estimated finish time. * * Returns the actual finish time when the operation has finished. */ estimatedFinishedAt(): Dayjs | undefined; /** * Returns the elapsed time as a value from 0 to 1. * * Returns `undefined` when the required times are missing or invalid. */ timeFraction(): number | undefined; /** Returns the reported steps. */ steps(): CurrentStep[]; } /** Defines all values for one saved request header in an operation response. */ export interface Operation_RequestHeader { /** Contains the values. */ values: string[]; } /** * Defines the generated operation fields that the runtime wrapper reads. * * Generated operation messages satisfy this interface. Use {@link Operation} * in application code because it provides polling and progress helpers. */ export interface GenericOperation { /** Contains the fully qualified runtime type name. */ $type: string; /** Contains the ID. */ id: string; /** Contains the description. */ description: string; /** Contains the creation time. */ createdAt?: Dayjs | undefined; /** Contains the ID of the creator. */ createdBy: string; /** Contains the finish time. */ finishedAt?: Dayjs | undefined; /** Contains the request. */ request?: { typeUrl: string; value: Uint8Array; } | undefined; /** Contains the request headers. */ requestHeaders: { [key: string]: Operation_RequestHeader; }; /** Contains the resource ID. */ resourceId: string; /** Contains the progress tracker. */ progressTracker?: ProgressTrackerProto | undefined; /** Contains the progress data. */ progressData?: { typeUrl: string; value: Uint8Array; } | undefined; /** Contains the status. */ status?: Status | undefined; } /** * Defines the operation service method that {@link Operation} uses for polling. * * Generated operation service clients satisfy this interface. */ export interface OperationService { /** Gets the latest state of an operation. */ get(req: { id: string; }, metadata?: Metadata | undefined, options?: (Partial & RetryOptions) | undefined): Request>; } /** * Polls a long-running operation and exposes its current state. * * Mutating service methods often return an operation instead of the final * resource. {@link Operation.wait} completes for both successful and failed * operations. After it completes, inspect {@link Operation.status} or * {@link Operation.successful}. It rejects only when polling cannot continue. * * @example * ```ts * const op = await service.create(req).result; * await op.wait(); * if (!op.successful()) { * throw new Error(`operation failed: ${op.status()?.message}`); * } * console.log('resource ID', op.resourceId()); * ``` */ export declare class Operation { /** Formats the current operation state for Node.js inspection. */ [custom]: () => string; private _op; private readonly service; private logger; /** Contains the fully qualified runtime type name. */ readonly $type: 'nebius.sdk.Operation'; /** Contains the protobuf type name of the wrapped operation. */ readonly innerType: string; /** * Creates an operation wrapper. * * Generated clients create this object with the correct operation service. * Application code normally receives it from a service request. */ constructor(_op: GenericOperation, service: OperationService, logger: Logger); /** Converts the value to string. */ toString(): string; /** Returns a JSON-safe value for logs. */ [customJson](): unknown; /** Returns the operation ID. */ id(): string; /** Returns the human-readable operation description. */ description(): string; /** Returns the operation creation time. */ createdAt(): Dayjs | undefined; /** Returns the ID of the user or service account that created the operation. */ createdBy(): string; /** Returns the operation finish time. */ finishedAt(): Dayjs | undefined; /** * Checks whether the operation finished successfully. * * Returns `false` while the operation is still running. */ successful(): boolean; /** * Returns the latest source protobuf object. * * Treat this object as read-only. {@link update} replaces it with the next * response from the service. */ raw(): GenericOperation; /** Returns the final status, or `undefined` while the operation is running. */ status(): Status | undefined; /** Checks whether the service has returned a final status. */ done(): boolean; /** * Returns the affected resource ID. * * A service can return an empty string before it assigns the resource ID. */ resourceId(): string; /** * Returns the progress tracker. * * Returns `undefined` when the service does not provide progress. * * @example * ```ts * const tracker = op.progressTracker(); * if (tracker) { * console.log(tracker.description()); * const steps = tracker.steps(); * if (steps.length > 0) console.log('first step', steps[0].description()); * } * ``` */ progressTracker(): OperationProgressTracker | undefined; /** * Polls the operation until the service returns a final status. * * The method updates this object in place. It continues after a polling call * reaches its deadline, because the remote operation can still be running. * Consecutive retriable polling errors use exponential backoff with jitter. * It rethrows non-retriable polling errors. A resolved promise does not mean * that the operation succeeded; call {@link successful} or inspect * {@link status}. The method returns immediately when the operation ID is * empty. * * @param intervalSec Sets the poll interval in seconds. The default is 1. * @param metadata Sends metadata with every polling request. * @param options Sets gRPC deadlines, request retries, and poll-error backoff. * @example * ```ts * await op.wait(1); // poll once per second * ``` */ wait(intervalSec?: number, metadata?: Metadata | undefined, options?: (OperationWaitOptions & Partial) | undefined): Promise; /** * Gets the latest operation state from the operation service. * * The method replaces the wrapped state in place. It does nothing when the * operation has no ID. Request errors reject the returned promise. * * @example * ```ts * await op.update(); * if (op.done()) console.log('finished', op.status()); * ``` */ update(metadata?: Metadata | undefined, options?: (Partial & RetryOptions) | undefined): Promise; } /** * Returns a read-only progress view for an operation. * * The view reads the current operation state, so it reflects later * {@link Operation.update} calls. Returns `undefined` when the operation or * tracker is missing. * * @example * ```ts * const tracker = wrapProgressTracker(op); * if (tracker) console.log(tracker.description()); * ``` */ export declare function wrapProgressTracker(operation: Operation | undefined): OperationProgressTracker | undefined; //# sourceMappingURL=operation.d.ts.map