UNPKG

@layr-labs/hourglass-performer

Version:

TypeScript SDK for building Hourglass AVS performers

136 lines 4.28 kB
"use strict"; // Task context and metadata handling for the performer framework Object.defineProperty(exports, "__esModule", { value: true }); exports.MetricsStage = exports.ValidationStage = exports.TaskPipeline = exports.TaskContextBuilder = void 0; /** * Task context builder for creating task execution contexts */ class TaskContextBuilder { constructor(request, logger, timeout = 5000) { this.request = request; this.logger = logger; this.timeout = timeout; this.abortController = new AbortController(); } /** * Build the task context */ build() { const executionId = this.generateExecutionId(); const startTime = Date.now(); // Set up timeout setTimeout(() => { this.abortController.abort(); }, this.timeout); return { request: this.request, startTime, executionId, logger: this.createTaskLogger(executionId), signal: this.abortController.signal, metadata: new Map(), }; } /** * Cancel the task execution */ cancel() { this.abortController.abort(); } /** * Generate a unique execution ID */ generateExecutionId() { const timestamp = Date.now().toString(36); const random = Math.random().toString(36).substr(2, 5); return `${this.request.taskId}-${timestamp}-${random}`; } /** * Create a task-specific logger */ createTaskLogger(executionId) { const taskMeta = { taskId: this.request.taskId, executionId, }; return { error: (message, meta) => this.logger.error(message, { ...taskMeta, ...meta }), warn: (message, meta) => this.logger.warn(message, { ...taskMeta, ...meta }), info: (message, meta) => this.logger.info(message, { ...taskMeta, ...meta }), debug: (message, meta) => this.logger.debug(message, { ...taskMeta, ...meta }), }; } } exports.TaskContextBuilder = TaskContextBuilder; /** * Task execution pipeline for processing tasks through multiple stages */ class TaskPipeline { constructor() { this.stages = []; } /** * Add a processing stage to the pipeline */ addStage(stage) { this.stages.push(stage); return this; } /** * Execute all stages in the pipeline */ async execute(context) { for (const stage of this.stages) { if (context.signal.aborted) { throw new Error('Task execution was cancelled'); } context.logger.debug(`Executing stage: ${stage.name}`); const stageStart = Date.now(); try { await stage.execute(context); const stageDuration = Date.now() - stageStart; context.logger.debug(`Stage completed: ${stage.name}`, { duration: `${stageDuration}ms` }); } catch (error) { const stageDuration = Date.now() - stageStart; context.logger.error(`Stage failed: ${stage.name}`, { duration: `${stageDuration}ms`, error: error instanceof Error ? error.message : 'Unknown error' }); throw error; } } } } exports.TaskPipeline = TaskPipeline; /** * Built-in processing stages */ class ValidationStage { constructor(validator) { this.validator = validator; this.name = 'validation'; } async execute(context) { await this.validator(context); } } exports.ValidationStage = ValidationStage; class MetricsStage { constructor(metricsCollector) { this.metricsCollector = metricsCollector; this.name = 'metrics'; } async execute(context) { const metrics = { taskId: context.request.taskId, executionId: context.executionId, startTime: context.startTime, payloadSize: context.request.payload.length, }; context.metadata.set('metrics', metrics); this.metricsCollector(metrics); } } exports.MetricsStage = MetricsStage; //# sourceMappingURL=taskContext.js.map