@apify/actors-mcp-server
Version:
Model Context Protocol Server for Apify
92 lines • 3.29 kB
JavaScript
import { ApifyClient } from '../apify-client.js';
import { PROGRESS_NOTIFICATION_INTERVAL_MS } from '../const.js';
export class ProgressTracker {
constructor(progressToken, sendNotification) {
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
});
this.progressToken = progressToken;
this.sendNotification = sendNotification;
}
async updateProgress(message) {
this.currentProgress += 1;
try {
const notification = {
method: 'notifications/progress',
params: {
progressToken: this.progressToken,
progress: this.currentProgress,
...(message && { message }),
},
};
await this.sendNotification(notification);
}
catch {
// Silent fail - don't break execution
}
}
startActorRunUpdates(runId, apifyToken, actorName) {
this.stop();
const client = new ApifyClient({ token: apifyToken });
let lastStatus = '';
let lastStatusMessage = '';
this.intervalId = setInterval(async () => {
try {
const run = await client.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) {
if (!progressToken || !sendNotification) {
return null;
}
return new ProgressTracker(progressToken, sendNotification);
}
//# sourceMappingURL=progress.js.map