@hotmeshio/hotmesh
Version:
Permanent-Memory Workflows & AI Agents
222 lines (221 loc) • 6.91 kB
TypeScript
import { HotMesh } from '../hotmesh';
import { Connection, Registry, WorkerConfig, WorkerOptions } from '../../types/memflow';
import { StreamData, StreamDataResponse } from '../../types/stream';
/**
* The *Worker* service Registers worker functions and connects them to the mesh,
* using the target backend provider/s (Postgres, NATS, etc).
*
* @example
* ```typescript
* import { MemFlow } from '@hotmeshio/hotmesh';
* import { Client as Postgres } from 'pg';
* import * as workflows from './workflows';
*
* async function run() {
* const worker = await MemFlow.Worker.create({
* connection: {
* class: Postgres,
* options: { connectionString: 'postgres://user:password@localhost:5432/db' }
* },
* taskQueue: 'default',
* workflow: workflows.example,
* });
*
* await worker.run();
* }
* ```
*/
export declare class WorkerService {
/**
* @private
*/
static activityRegistry: Registry;
/**
* @private
*/
static instances: Map<string, HotMesh | Promise<HotMesh>>;
/**
* @private
*/
workflowRunner: HotMesh;
/**
* @private
*/
activityRunner: HotMesh;
/**
* @private
*/
static getHotMesh: (workflowTopic: string, config?: Partial<WorkerConfig>, options?: WorkerOptions) => Promise<HotMesh>;
static hashOptions(connection: Connection): string;
/**
* @private
*/
constructor();
/**
* @private
*/
static activateWorkflow(hotMesh: HotMesh): Promise<void>;
/**
* @private
*/
static registerActivities<ACT>(activities: ACT): Registry;
/**
* Register activity workers for a task queue. Activities are invoked via message queue,
* so they can run on different servers from workflows.
*
* The task queue name gets `-activity` appended automatically for the worker topic.
* For example, `taskQueue: 'payment'` creates a worker listening on `payment-activity`.
*
* @param config - Worker configuration (connection, namespace, taskQueue)
* @param activities - Activity functions to register
* @param activityTaskQueue - Task queue name (without `-activity` suffix).
* Defaults to `config.taskQueue` if not provided.
*
* @returns Promise<HotMesh> The initialized activity worker
*
* @example
* ```typescript
* // Activity worker (can be on separate server)
* import { MemFlow } from '@hotmeshio/hotmesh';
* import { Client as Postgres } from 'pg';
*
* const activities = {
* async processPayment(amount: number): Promise<string> {
* return `Processed $${amount}`;
* },
* async sendEmail(to: string, subject: string): Promise<void> {
* // Send email
* }
* };
*
* await MemFlow.registerActivityWorker({
* connection: {
* class: Postgres,
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
* },
* taskQueue: 'payment' // Listens on 'payment-activity'
* }, activities, 'payment');
* ```
*
* @example
* ```typescript
* // Workflow worker (can be on different server)
* async function orderWorkflow(orderId: string, amount: number) {
* const { processPayment, sendEmail } = MemFlow.workflow.proxyActivities<{
* processPayment: (amount: number) => Promise<string>;
* sendEmail: (to: string, subject: string) => Promise<void>;
* }>({
* taskQueue: 'payment',
* retryPolicy: { maximumAttempts: 3 }
* });
*
* const result = await processPayment(amount);
* await sendEmail('customer@example.com', 'Order confirmed');
* return result;
* }
*
* await MemFlow.Worker.create({
* connection: {
* class: Postgres,
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
* },
* taskQueue: 'orders',
* workflow: orderWorkflow
* });
* ```
*
* @example
* ```typescript
* // Shared activity pool for interceptors
* await MemFlow.registerActivityWorker({
* connection: {
* class: Postgres,
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
* },
* taskQueue: 'shared'
* }, { auditLog, collectMetrics }, 'shared');
*
* const interceptor: WorkflowInterceptor = {
* async execute(ctx, next) {
* const { auditLog } = MemFlow.workflow.proxyActivities<{
* auditLog: (id: string, action: string) => Promise<void>;
* }>({
* taskQueue: 'shared',
* retryPolicy: { maximumAttempts: 3 }
* });
* await auditLog(ctx.get('workflowId'), 'started');
* return next();
* }
* };
* ```
*/
static registerActivityWorker(config: Partial<WorkerConfig>, activities: any, activityTaskQueue?: string): Promise<HotMesh>;
/**
* Create an activity callback function that can be used by activity workers
* @private
*/
static createActivityCallback(): (payload: StreamData) => Promise<StreamDataResponse>;
/**
* Connects a worker to the mesh.
*
* @example
* ```typescript
* import { MemFlow } from '@hotmeshio/hotmesh';
* import { Client as Postgres } from 'pg';
* import * as workflows from './workflows';
*
* async function run() {
* const worker = await MemFlow.Worker.create({
* connection: {
* class: Postgres,
* options: {
* connectionString: 'postgres://user:password@localhost:5432/db'
* },
* },
* taskQueue: 'default',
* workflow: workflows.example,
* });
*
* await worker.run();
* }
* ```
*/
static create(config: WorkerConfig): Promise<WorkerService>;
/**
* @private
*/
static resolveWorkflowTarget(workflow: object | Function, name?: string): [string, Function];
/**
* Run the connected worker; no-op (unnecessary to call)
*/
run(): Promise<void>;
/**
* @private
*/
initActivityWorker(config: WorkerConfig, activityTopic: string): Promise<HotMesh>;
/**
* @private
*/
wrapActivityFunctions(): Function;
/**
* @private
*/
initWorkflowWorker(config: WorkerConfig, workflowTopic: string, workflowFunction: Function): Promise<HotMesh>;
/**
* @private
*/
static Context: {
info: () => {
workflowId: string;
workflowTopic: string;
};
};
/**
* @private
*/
wrapWorkflowFunction(workflowFunction: Function, workflowTopic: string, config: WorkerConfig): Function;
/**
* @private
*/
static shutdown(): Promise<void>;
}