UNPKG

@just-every/task

Version:
157 lines 5.7 kB
/** * Helper for thought delay processing */ import { createToolFunction } from '@just-every/ensemble'; import { VALID_THOUGHT_DELAYS, DEFAULT_THOUGHT_DELAY } from '../utils/constants.js'; import { validateThoughtDelay } from '../utils/validation.js'; import { withErrorHandling } from '../utils/errors.js'; // Thought utilities for managing thought delay and related tools export let thoughtDelay = DEFAULT_THOUGHT_DELAY; // Use AbortController for interrupting thought delays let delayAbortController = new AbortController(); export function setDelayInterrupted(interrupted) { try { if (interrupted) { // If we're interrupting, abort the current controller delayAbortController.abort(); } else { // If we're clearing the interruption, create a new controller delayAbortController = new AbortController(); } } catch (error) { console.error('[ThoughtDelay] Error setting delay interrupted state:', error); // Create a new controller as fallback delayAbortController = new AbortController(); } } export function isDelayInterrupted() { return delayAbortController.signal.aborted; } // Get the current abort signal for thought delay export function getDelayAbortSignal() { return delayAbortController.signal; } export function getThoughtDelay() { return thoughtDelay; } export function getValidThoughtDelays() { return VALID_THOUGHT_DELAYS; } /** * Execute an optimized thought delay with interrupt support * * Implements intelligent delays between agent thoughts to: * - Allow time for external processes to complete * - Provide natural pacing for complex reasoning * - Enable interruption for urgent messages or user input * * Uses AbortController for clean cancellation. * * @returns Promise that resolves when delay completes or is interrupted * * @example * ```typescript * // Basic delay execution * await runThoughtDelay(); * * // With interruption handling * try { * await runThoughtDelay(); * } catch (error) { * console.log('Delay was interrupted'); * } * ``` */ export async function runThoughtDelay() { if (thoughtDelay > 0) { console.log(`[Task] Thought delay: ${thoughtDelay} seconds`); // Create a new controller for this delay delayAbortController = new AbortController(); const signal = delayAbortController.signal; // Simple delay implementation try { await new Promise((resolve, reject) => { const timeoutId = setTimeout(resolve, thoughtDelay * 1000); signal.addEventListener('abort', () => { clearTimeout(timeoutId); const error = new Error('Delay was aborted'); error.name = 'AbortError'; reject(error); }, { once: true }); }); } catch (error) { if (error instanceof Error && error.name === 'AbortError') { console.log('[Task] Thought delay interrupted'); throw error; } throw error; } } } /** * Execute thought delay with a specific AbortController (task-scoped) * * @param controller - Task-specific AbortController * @param delaySeconds - Delay in seconds */ export async function runThoughtDelayWithController(controller, delaySeconds) { if (delaySeconds > 0) { console.log(`[Task] Thought delay: ${delaySeconds} seconds`); const signal = controller.signal; // Simple delay implementation try { await new Promise((resolve, reject) => { const timeoutId = setTimeout(resolve, delaySeconds * 1000); signal.addEventListener('abort', () => { clearTimeout(timeoutId); const error = new Error('Delay was aborted'); error.name = 'AbortError'; reject(error); }, { once: true }); }); } catch (error) { if (error instanceof Error && error.name === 'AbortError') { console.log('[Task] Thought delay interrupted'); throw error; } throw error; } } } /** * Set the thought delay for the agent * @param delay - The delay to set (0, 2, 4, 8, 16, 32, 64, or 128 seconds) * @returns The new delay value or error message */ export const setThoughtDelay = withErrorHandling((delay) => { validateThoughtDelay(delay); thoughtDelay = parseInt(delay); console.log(`[Task] Thought delay set to ${thoughtDelay} seconds`); return `Thought delay set to ${thoughtDelay} seconds`; }, 'thought_management'); /** * Get thought management tools * * @returns Array of tool functions for managing thought delays */ export function getThoughtTools() { const tools = []; tools.push(createToolFunction(setThoughtDelay, 'Change the delay between agent thoughts', { delay: { type: 'string', description: 'Delay in seconds (0, 2, 4, 8, 16, 32, 64, or 128)', enum: VALID_THOUGHT_DELAYS, }, }, undefined, 'set_thought_delay'), createToolFunction(() => { setDelayInterrupted(true); return 'Thought delay interrupted'; }, 'Interrupt the current thought delay to proceed immediately', {}, undefined, 'interrupt_delay'), createToolFunction(() => { return `Current thought delay: ${thoughtDelay} seconds`; }, 'Get the current thought delay setting', {}, undefined, 'get_thought_delay')); return tools; } //# sourceMappingURL=thought_utils.js.map