@hotmeshio/hotmesh
Version:
Permanent-Memory Workflows & AI Agents
115 lines (114 loc) • 4.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Await = void 0;
const errors_1 = require("../../modules/errors");
const utils_1 = require("../../modules/utils");
const collator_1 = require("../collator");
const pipe_1 = require("../pipe");
const telemetry_1 = require("../telemetry");
const stream_1 = require("../../types/stream");
const activity_1 = require("./activity");
class Await extends activity_1.Activity {
constructor(config, data, metadata, hook, engine, context) {
super(config, data, metadata, hook, engine, context);
}
//******** INITIAL ENTRY POINT (A) ********//
async process() {
this.logger.debug('await-process', {
jid: this.context.metadata.jid,
gid: this.context.metadata.gid,
aid: this.metadata.aid,
});
let telemetry;
try {
await this.verifyEntry();
telemetry = new telemetry_1.TelemetryService(this.engine.appId, this.config, this.metadata, this.context);
telemetry.startActivitySpan(this.leg);
this.mapInputData();
//save state and authorize reentry
const transaction = this.store.transact();
//todo: await this.registerTimeout();
const messageId = await this.execActivity(transaction);
await collator_1.CollatorService.authorizeReentry(this, transaction);
await this.setState(transaction);
await this.setStatus(0, transaction);
const multiResponse = (await transaction.exec());
//telemetry
telemetry.mapActivityAttributes();
const jobStatus = this.resolveStatus(multiResponse);
telemetry.setActivityAttributes({
'app.activity.mid': messageId,
'app.job.jss': jobStatus,
});
return this.context.metadata.aid;
}
catch (error) {
if (error instanceof errors_1.InactiveJobError) {
this.logger.error('await-inactive-job-error', { error });
return;
}
else if (error instanceof errors_1.GenerationalError) {
this.logger.info('process-event-generational-job-error', { error });
return;
}
else if (error instanceof errors_1.GetStateError) {
this.logger.error('await-get-state-error', { error });
return;
}
else if (error instanceof errors_1.CollationError) {
if (error.fault === 'duplicate') {
this.logger.info('await-collation-overage', {
job_id: this.context.metadata.jid,
guid: this.context.metadata.guid,
});
return;
}
//unknown collation error
this.logger.error('await-collation-error', { error });
}
else {
this.logger.error('await-process-error', { error });
}
telemetry?.setActivityError(error.message);
throw error;
}
finally {
telemetry?.endActivitySpan();
this.logger.debug('await-process-end', {
jid: this.context.metadata.jid,
gid: this.context.metadata.gid,
aid: this.metadata.aid,
});
}
}
async execActivity(transaction) {
const topic = pipe_1.Pipe.resolve(this.config.subtype, this.context);
const streamData = {
metadata: {
guid: (0, utils_1.guid)(),
jid: this.context.metadata.jid,
gid: this.context.metadata.gid,
dad: this.metadata.dad,
aid: this.metadata.aid,
topic,
spn: this.context['$self'].output.metadata?.l1s,
trc: this.context.metadata.trc,
},
type: stream_1.StreamDataType.AWAIT,
data: this.context.data,
};
if (this.config.await !== true) {
const doAwait = pipe_1.Pipe.resolve(this.config.await, this.context);
if (doAwait === false) {
streamData.metadata.await = false;
}
}
if (this.config.retry) {
streamData.policies = {
retry: this.config.retry,
};
}
return (await this.engine.router?.publishMessage(null, streamData, transaction));
}
}
exports.Await = Await;