|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import {Runnable} from '../../../../src/index.js';
|
|
|
import {BaseCallback} from '../../../../src/utils/callbacks.js';
|
|
|
|
|
|
class SimpleLoggerCallback extends BaseCallback {
|
|
|
constructor(options = {}) {
|
|
|
super();
|
|
|
this.showTimestamp = options.showTimestamp ?? false;
|
|
|
}
|
|
|
|
|
|
async onStart(runnable, input, config) {
|
|
|
|
|
|
console.log(`▶️ Starting: ${runnable.name}`)
|
|
|
console.log(` Input: ${input}`)
|
|
|
}
|
|
|
|
|
|
async onEnd(runnable, output, config) {
|
|
|
|
|
|
console.log(`✔️ Completed: ${runnable.name}`)
|
|
|
console.log(` Output: ${output}`)
|
|
|
}
|
|
|
|
|
|
async onError(runnable, error, config) {
|
|
|
|
|
|
console.log(` ❌ ${runnable.name}: ${error.message}`)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
class GreeterRunnable extends Runnable {
|
|
|
async _call(input, config) {
|
|
|
return `Hello, ${input}!`;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class UpperCaseRunnable extends Runnable {
|
|
|
async _call(input, config) {
|
|
|
if (typeof input !== 'string') {
|
|
|
throw new Error('Input must be a string');
|
|
|
}
|
|
|
return input.toUpperCase();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class ErrorRunnable extends Runnable {
|
|
|
async _call(input, config) {
|
|
|
throw new Error('Intentional error for testing');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async function exercise1() {
|
|
|
console.log('=== Exercise 1: Simple Logging Callback ===\n');
|
|
|
|
|
|
const logger = new SimpleLoggerCallback();
|
|
|
const config = {
|
|
|
callbacks: [logger]
|
|
|
};
|
|
|
|
|
|
|
|
|
console.log('--- Test 1: Normal Execution ---');
|
|
|
const greeter = new GreeterRunnable();
|
|
|
const result1 = await greeter.invoke("World", config);
|
|
|
console.log('Final result:', result1);
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
console.log('--- Test 2: Pipeline ---');
|
|
|
const upper = new UpperCaseRunnable();
|
|
|
const pipeline = greeter.pipe(upper);
|
|
|
const result2 = await pipeline.invoke("claude", config);
|
|
|
console.log('Final result:', result2);
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
console.log('--- Test 3: Error Handling ---');
|
|
|
const errorRunnable = new ErrorRunnable();
|
|
|
try {
|
|
|
await errorRunnable.invoke("test", config);
|
|
|
} catch (error) {
|
|
|
console.log('Caught error (expected):', error.message);
|
|
|
}
|
|
|
|
|
|
console.log('\n✓ Exercise 1 complete!');
|
|
|
}
|
|
|
|
|
|
|
|
|
exercise1().catch(console.error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|