@hotmeshio/hotmesh
Version:
Permanent-Memory Workflows & AI Agents
53 lines (52 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleepFor = void 0;
const common_1 = require("./common");
const didRun_1 = require("./didRun");
/**
* Sleeps the workflow for a specified duration, deterministically.
* On replay, it will not actually sleep again, but resume after sleep.
*
* @example
* // Basic usage - sleep for a specific duration
* await MemFlow.workflow.sleepFor('2 seconds');
*
* @example
* // Using with Promise.all for parallel operations
* const [greeting, timeInSeconds] = await Promise.all([
* someActivity(name),
* MemFlow.workflow.sleepFor('1 second')
* ]);
*
* @example
* // Multiple sequential sleeps
* await MemFlow.workflow.sleepFor('1 seconds'); // First pause
* await MemFlow.workflow.sleepFor('2 seconds'); // Second pause
*
* @param {string} duration - A human-readable duration string (e.g., '1m', '2 hours', '30 seconds').
* @returns {Promise<number>} The resolved duration in seconds.
*/
async function sleepFor(duration) {
const [didRunAlready, execIndex, result] = await (0, didRun_1.didRun)('sleep');
if (didRunAlready) {
return result.duration;
}
const store = common_1.asyncLocalStorage.getStore();
const interruptionRegistry = store.get('interruptionRegistry');
const workflowId = store.get('workflowId');
const workflowDimension = store.get('workflowDimension') ?? '';
const interruptionMessage = {
workflowId,
duration: (0, common_1.s)(duration),
index: execIndex,
workflowDimension,
};
interruptionRegistry.push({
code: common_1.HMSH_CODE_MEMFLOW_SLEEP,
type: 'MemFlowSleepError',
...interruptionMessage,
});
await (0, common_1.sleepImmediate)();
throw new common_1.MemFlowSleepError(interruptionMessage);
}
exports.sleepFor = sleepFor;