
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="2f98c5be-5235-52b3-8b39-68267b074868")}catch(e){}}();
import { PROGRESS_NOTIFICATION_INTERVAL_MS } from '../const.js';
export class ProgressTracker {
    constructor(options) {
        Object.defineProperty(this, "progressToken", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "sendNotification", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "currentProgress", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 0
        });
        Object.defineProperty(this, "intervalId", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "taskId", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "onStatusMessage", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.progressToken = options.progressToken;
        this.sendNotification = options.sendNotification;
        this.taskId = options.taskId;
        this.onStatusMessage = options.onStatusMessage;
    }
    async updateProgress(message) {
        this.currentProgress += 1;
        // Send progress notification only if progressToken and sendNotification are available
        if (this.progressToken && this.sendNotification) {
            try {
                const notification = {
                    method: 'notifications/progress',
                    params: {
                        progressToken: this.progressToken,
                        progress: this.currentProgress,
                        ...(message && { message }),
                    },
                    // Per MCP spec: progress notifications during task execution should include related-task metadata
                    ...(this.taskId && {
                        _meta: {
                            'io.modelcontextprotocol/related-task': {
                                taskId: this.taskId,
                            },
                        },
                    }),
                };
                await this.sendNotification(notification);
            }
            catch {
                // Silent fail - don't break execution
            }
        }
        // Update task statusMessage if callback is provided
        if (this.onStatusMessage && message) {
            try {
                await this.onStatusMessage(message);
            }
            catch {
                // Silent fail - don't break execution
            }
        }
    }
    startActorRunUpdates(runId, apifyClient, actorName) {
        this.stop();
        let lastStatus = '';
        let lastStatusMessage = '';
        this.intervalId = setInterval(async () => {
            try {
                const run = await apifyClient.run(runId).get();
                if (!run)
                    return;
                const { status, statusMessage } = run;
                // Only send notification if status or statusMessage changed
                if (status !== lastStatus || statusMessage !== lastStatusMessage) {
                    lastStatus = status;
                    lastStatusMessage = statusMessage || '';
                    const message = statusMessage
                        ? `${actorName}: ${statusMessage}`
                        : `${actorName}: ${status}`;
                    await this.updateProgress(message);
                    // Stop polling if Actor finished
                    if (status === 'SUCCEEDED' || status === 'FAILED' || status === 'ABORTED' || status === 'TIMED-OUT') {
                        this.stop();
                    }
                }
            }
            catch {
                // Silent fail - continue polling
            }
        }, PROGRESS_NOTIFICATION_INTERVAL_MS);
    }
    stop() {
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = undefined;
        }
    }
}
export function createProgressTracker(progressToken, sendNotification, taskId, onStatusMessage) {
    // Create tracker if we have either progress notification support or a status message callback
    if ((!progressToken || !sendNotification) && !onStatusMessage) {
        return null;
    }
    return new ProgressTracker({ progressToken, sendNotification, taskId, onStatusMessage });
}
//# sourceMappingURL=progress.js.map
//# debugId=2f98c5be-5235-52b3-8b39-68267b074868
