|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class CallbackManager {
|
|
|
constructor(callbacks = []) {
|
|
|
this.callbacks = callbacks;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
add(callback) {
|
|
|
this.callbacks.push(callback);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async handleStart(runnable, input, config) {
|
|
|
await Promise.all(
|
|
|
this.callbacks.map(cb =>
|
|
|
this._safeCall(() => cb.onStart(runnable, input, config))
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async handleEnd(runnable, output, config) {
|
|
|
await Promise.all(
|
|
|
this.callbacks.map(cb =>
|
|
|
this._safeCall(() => cb.onEnd(runnable, output, config))
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async handleError(runnable, error, config) {
|
|
|
await Promise.all(
|
|
|
this.callbacks.map(cb =>
|
|
|
this._safeCall(() => cb.onError(runnable, error, config))
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async handleLLMNewToken(token, config) {
|
|
|
await Promise.all(
|
|
|
this.callbacks.map(cb =>
|
|
|
this._safeCall(() => cb.onLLMNewToken(token, config))
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async handleChainStep(stepName, output, config) {
|
|
|
await Promise.all(
|
|
|
this.callbacks.map(cb =>
|
|
|
this._safeCall(() => cb.onChainStep(stepName, output, config))
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _safeCall(fn) {
|
|
|
try {
|
|
|
await fn();
|
|
|
} catch (error) {
|
|
|
console.error('Callback error:', error);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export default CallbackManager;
|
|
|
|