File size: 1,924 Bytes
e706de2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
/**
* CallbackManager
*
* Event system for logging and monitoring
*
* @module src/utils/callback-manager.js
*/
export class CallbackManager {
constructor(callbacks = []) {
this.callbacks = callbacks;
}
/**
* Add a callback
*/
add(callback) {
this.callbacks.push(callback);
}
/**
* Call onStart for all callbacks
*/
async handleStart(runnable, input, config) {
await Promise.all(
this.callbacks.map(cb =>
this._safeCall(() => cb.onStart(runnable, input, config))
)
);
}
/**
* Call onEnd for all callbacks
*/
async handleEnd(runnable, output, config) {
await Promise.all(
this.callbacks.map(cb =>
this._safeCall(() => cb.onEnd(runnable, output, config))
)
);
}
/**
* Call onError for all callbacks
*/
async handleError(runnable, error, config) {
await Promise.all(
this.callbacks.map(cb =>
this._safeCall(() => cb.onError(runnable, error, config))
)
);
}
/**
* Call onLLMNewToken for all callbacks
*/
async handleLLMNewToken(token, config) {
await Promise.all(
this.callbacks.map(cb =>
this._safeCall(() => cb.onLLMNewToken(token, config))
)
);
}
/**
* Call onChainStep for all callbacks
*/
async handleChainStep(stepName, output, config) {
await Promise.all(
this.callbacks.map(cb =>
this._safeCall(() => cb.onChainStep(stepName, output, config))
)
);
}
/**
* Safely call a callback (don't let one callback crash others)
*/
async _safeCall(fn) {
try {
await fn();
} catch (error) {
console.error('Callback error:', error);
// Don't throw - callbacks shouldn't break the pipeline
}
}
}
export default CallbackManager;
|