import * as grpc from "@grpc/grpc-js"; import { TActivity } from "../types/activity.type"; import { TInput } from "../types/input.type"; import { TOrchestrator } from "../types/orchestrator.type"; import { TOutput } from "../types/output.type"; import { GrpcClient } from "../client/client-grpc"; import { MetadataGenerator } from "../utils/grpc-helper.util"; import { EntityFactory } from "../entities/task-entity"; import { Logger } from "../types/logger.type"; import { VersioningOptions } from "./versioning-options"; import { WorkItemFilters } from "./work-item-filters"; /** * Options for creating a TaskHubGrpcWorker. */ export interface TaskHubGrpcWorkerOptions { /** The host address to connect to. Defaults to "localhost:4001". */ hostAddress?: string; /** gRPC channel options. */ options?: grpc.ChannelOptions; /** Whether to use TLS. Defaults to false. */ useTLS?: boolean; /** Optional pre-configured channel credentials. If provided, useTLS is ignored. */ credentials?: grpc.ChannelCredentials; /** Optional function to generate per-call metadata (for taskhub, auth tokens, etc.). */ metadataGenerator?: MetadataGenerator; /** Optional logger instance. Defaults to ConsoleLogger. */ logger?: Logger; /** Optional timeout in milliseconds for graceful shutdown. Defaults to 30000. */ shutdownTimeoutMs?: number; /** Optional versioning options for filtering orchestrations by version. */ versioning?: VersioningOptions; /** * Optional work item filters to control which work items the worker receives. * By default, no filters are sent and the worker processes all work items. * Set to a WorkItemFilters object to use explicit filters. * Set to "auto" to auto-generate filters from the registered orchestrations, * activities, and entities. */ workItemFilters?: WorkItemFilters | "auto"; } export declare class TaskHubGrpcWorker { private _responseStream; private _registry; private _hostAddress?; private _tls?; private _grpcChannelOptions?; private _grpcChannelCredentials?; private _metadataGenerator?; private _isRunning; private _stopWorker; private _stub; private _logger; private _pendingWorkItems; private _shutdownTimeoutMs; private _backoff; private _versioning?; private _workItemFilters?; /** * Creates a new TaskHubGrpcWorker instance. * * @param options Configuration options for the worker. */ constructor(options: TaskHubGrpcWorkerOptions); /** * Creates a new TaskHubGrpcWorker instance. * * @param hostAddress The host address to connect to. Defaults to "localhost:4001". * @param options gRPC channel options. * @param useTLS Whether to use TLS. Defaults to false. * @param credentials Optional pre-configured channel credentials. If provided, useTLS is ignored. * @param metadataGenerator Optional function to generate per-call metadata (for taskhub, auth tokens, etc.). * @param logger Optional logger instance. Defaults to ConsoleLogger. * @param shutdownTimeoutMs Optional timeout in milliseconds for graceful shutdown. Defaults to 30000. * @deprecated Use the options object constructor instead. */ constructor(hostAddress?: string, options?: grpc.ChannelOptions, useTLS?: boolean, credentials?: grpc.ChannelCredentials, metadataGenerator?: MetadataGenerator, logger?: Logger, shutdownTimeoutMs?: number); /** * Helper to get metadata for gRPC calls. */ private _getMetadata; /** * Creates a new gRPC client and retries the worker. * Properly closes the old client to prevent connection leaks. */ private _createNewClientAndRetry; /** * Registers an orchestrator function with the worker. * * @param fn * @returns */ addOrchestrator(fn: TOrchestrator): string; /** * Registers an named orchestrator function with the worker. * * @param fn * @returns */ addNamedOrchestrator(name: string, fn: TOrchestrator): string; /** * Registers an activity function with the worker. * * @param fn * @returns */ addActivity(fn: TActivity): string; /** * Registers an named activity function with the worker. * * @param fn * @returns */ addNamedActivity(name: string, fn: TActivity): string; /** * Registers an entity with the worker. * * @param factory - Factory function that creates entity instances. * @returns The registered entity name (normalized to lowercase). * * @remarks * Entity names are derived from the factory function name and normalized to lowercase. */ addEntity(factory: EntityFactory): string; /** * Registers a named entity with the worker. * * @param name - The name to register the entity under. * @param factory - Factory function that creates entity instances. * @returns The registered entity name (normalized to lowercase). * * @remarks * Entity names are normalized to lowercase for case-insensitive matching. */ addNamedEntity(name: string, factory: EntityFactory): string; /** * Processes a single serialized TaskHubSidecarService OrchestratorRequest and * returns the serialized OrchestratorResponse. * * @param request - The protobuf-encoded OrchestratorRequest bytes. * @returns The protobuf-encoded OrchestratorResponse bytes. * * @remarks * This is intended for host integrations, such as Azure Functions, that drive a * single orchestration work item per invocation instead of running the * long-lived gRPC worker loop. It reuses the same execution path as the worker * loop, capturing the response in-process rather than completing it over gRPC. * Host integrations own any transport-specific encoding (for example base64). */ processOrchestratorRequest(request: Uint8Array): Promise; /** * Processes a single serialized TaskHubSidecarService EntityBatchRequest and * returns the serialized EntityBatchResult. * * @param request - The protobuf-encoded EntityBatchRequest bytes. * @returns The protobuf-encoded EntityBatchResult bytes. * * @remarks * This is intended for host integrations, such as Azure Functions, that drive a * single entity batch work item per invocation instead of running the * long-lived gRPC worker loop. It reuses the same execution path as the worker * loop, capturing the result in-process rather than completing it over gRPC. * Host integrations own any transport-specific encoding (for example base64). */ processEntityBatchRequest(request: Uint8Array): Promise; /** * In node.js we don't require a new thread as we have a main event loop * Therefore, we open the stream and simply listen through the eventemitter behind the scenes */ start(): Promise; internalRunWorker(client: GrpcClient, isRetry?: boolean): Promise; /** * Stop the worker and wait for any pending work items to complete. * Uses a configurable timeout (default 30s) to wait for in-flight work. */ stop(): Promise; /** * Builds the GetWorkItemsRequest, attaching work item filters based on configuration. * - undefined (default): no filters sent, worker receives all work items * - "auto": auto-generate filters from the registry * - explicit WorkItemFilters: use as provided */ private _buildGetWorkItemsRequest; /** * Result of version compatibility check. */ private _checkVersionCompatibility; /** * Extracts the orchestration version from the ExecutionStarted event in the request. */ private _getOrchestrationVersion; private _trackPendingWorkItem; /** * Executes an orchestrator request and tracks it as a pending work item. */ private _executeOrchestrator; /** * Internal implementation of orchestrator execution. */ private _executeOrchestratorInternal; /** * Executes an activity request and tracks it as a pending work item. */ private _executeActivity; /** * Internal implementation of activity execution. */ private _executeActivityInternal; /** * Executes an entity batch request and tracks it as a pending work item. */ private _executeEntity; /** * Internal implementation of entity batch execution. * * @param req - The entity batch request from the sidecar. * @param completionToken - The completion token for the work item. * @param stub - The gRPC stub for completing the task. * @param operationInfos - Optional V2 operation info list to include in the result. * * @remarks * This method looks up the entity by name, creates a TaskEntityShim, executes the batch, * and sends the result back to the sidecar. */ private _executeEntityInternal; /** * Executes an entity request (V2 format) and tracks it as a pending work item. */ private _executeEntityV2; /** * Internal implementation of V2 entity execution. * * @param req - The entity request (V2) from the sidecar. * @param completionToken - The completion token for the work item. * @param stub - The gRPC stub for completing the task. * * @remarks * This method handles the V2 entity request format which uses HistoryEvent * instead of OperationRequest. It converts the V2 format to V1 format * (EntityBatchRequest) and delegates to the existing execution logic. */ private _executeEntityV2Internal; /** * Creates an EntityBatchResult for when an entity is not found. * * @remarks * Returns a non-retriable error for each operation in the batch. */ private _createEntityNotFoundResult; /** * Sends the entity batch result to the sidecar. */ private _sendEntityResult; }