File size: 6,180 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**

 * Exercise 15: Config Merging and Child Configs

 *

 * Goal: Understand how configs inherit and merge

 *

 * In this exercise, you'll:

 * 1. Create parent and child configs

 * 2. See how child configs inherit from parents

 * 3. Understand callback accumulation

 * 4. Learn when to use config.merge() vs config.child()

 *

 * This is crucial for nested Runnable calls!

 */

import {RunnableConfig} from '../../../../src/core/context.js';
import {Runnable} from '../../../../src/index.js';
import {BaseCallback} from '../../../../src/utils/callbacks.js';

// Simple callback to show when it's called
class TagLoggerCallback extends BaseCallback {
    constructor(name) {
        super();
        this.name = name;
    }

    async onStart(runnable, input, config) {
        console.log(`[${this.name}] Starting ${runnable.constructor.name}`);
        console.log(`  Tags: [${config.tags.join(', ')}]`);
        console.log(`  Callback count: ${config.callbacks.length}`);
    }

    async onEnd(runnable, output, config) {
        console.log(`[${this.name}] Completed ${runnable.constructor.name}`);
    }
}

// Test Runnables that create child configs
class Step1Runnable extends Runnable {
    async _call(input, config) {
        console.log('\n--- Inside Step1 ---');

        const childConfig = config.child({ tags: ['step1'] });

        console.log(`Step1 child has ${childConfig.callbacks.length} callbacks`);

        // Simulate nested work
        return `Step1(${input})`;
    }
}

class Step2Runnable extends Runnable {
    async _call(input, config) {
        console.log('\n--- Inside Step2 ---');

        const childConfig = config.child({ tags: ['step2'] });

        console.log(`Step2 child has ${childConfig.callbacks.length} callbacks`);

        return `Step2(${input})`;
    }
}

class Step3Runnable extends Runnable {
    async _call(input, config) {
        console.log('\n--- Inside Step3 ---');

        const childConfig = config.child({
            tags: ['step3'],
            metadata: { nested: true }
        });

        console.log(`Step3 tags: [${childConfig.tags.join(', ')}]`);
        console.log(`Step3 metadata:`, childConfig.metadata);

        return `Step3(${input})`;
    }
}

async function exercise() {
    console.log('=== Exercise 15: Config Merging and Child Configs ===\n');

    // Part 1: Basic config inheritance
    console.log('--- Part 1: Basic Inheritance ---\n');

    const parentConfig = new RunnableConfig({
        callbacks: [new TagLoggerCallback('Parent')],
        tags: ['base']
    });

    const childConfig = parentConfig.child({
        callbacks: [new TagLoggerCallback('Child')],
        tags: ['child']
    });

    console.log('Parent callbacks:', parentConfig.callbacks.length);
    console.log('Child callbacks:', childConfig.callbacks.length);
    console.log('Parent tags:', parentConfig.tags);
    console.log('Child tags:', childConfig.tags);

    // Part 2: Config in pipelines
    console.log('\n--- Part 2: Config in Pipelines ---\n');

    const pipelineConfig = new RunnableConfig({
        callbacks: [new TagLoggerCallback('Pipeline')],
        tags: ['base']
    });

    const step1 = new Step1Runnable();
    const step2 = new Step2Runnable();
    const pipeline = step1.pipe(step2);

    await pipeline.invoke("test", pipelineConfig);

    // Part 3: Multiple levels of nesting
    console.log('\n--- Part 3: Multiple Nesting Levels ---\n');

    const level1Config = new RunnableConfig({
        callbacks: [new TagLoggerCallback('Level1')],
        tags: ['level1'],
        metadata: {level: 1}
    });

    const level2Config = level1Config.child({
        callbacks: [new TagLoggerCallback('Level2')],
        tags: ['level2'],
        metadata: {level: 2}
    });

    const level3Config = level2Config.child({
        callbacks: [new TagLoggerCallback('Level3')],
        tags: ['level3'],
        metadata: {level: 3}
    });

    console.log('Level 1 - Callbacks:', level1Config.callbacks.length, 'Tags:', level1Config.tags);
    console.log('Level 2 - Callbacks:', level2Config.callbacks.length, 'Tags:', level2Config.tags);
    console.log('Level 3 - Callbacks:', level3Config.callbacks.length, 'Tags:', level3Config.tags);

    // Part 4: merge() vs child()
    console.log('\n--- Part 4: merge() vs child() ---\n');

    const configA = new RunnableConfig({
        tags: ['a'],
        metadata: {source: 'A'}
    });

    const configB = new RunnableConfig({
        tags: ['b'],
        metadata: {source: 'B', extra: 'data'}
    });

    const merged = configA.merge(configB);

    const child = configA.child({
        tags: ['b'],
        metadata: { extra: 'data' }
    });

    console.log('Merged metadata:', merged.metadata);
    console.log('Child metadata:', child.metadata);

    console.log('\n✓ Exercise 15 complete!');
}

// Run the exercise
exercise().catch(console.error);

/**

 * Expected Output Snippets:

 *

 * --- Part 1: Basic Inheritance ---

 * Parent callbacks: 1

 * Child callbacks: 2

 * Parent tags: ['base']

 * Child tags: ['base', 'child']

 *

 * --- Part 2: Config in Pipelines ---

 * [Pipeline] Starting Step1Runnable

 *   Tags: [base]

 *   Callback count: 1

 *

 * --- Inside Step1 ---

 * Step1 child has 1 callbacks

 * [Pipeline] Completed Step1Runnable

 *

 * --- Part 3: Multiple Nesting Levels ---

 * Level 1 - Callbacks: 1, Tags: ['level1']

 * Level 2 - Callbacks: 2, Tags: ['level1', 'level2']

 * Level 3 - Callbacks: 3, Tags: ['level1', 'level2', 'level3']

 *

 * --- Part 4: merge() vs child() ---

 * Merged metadata: { source: 'B', extra: 'data' }

 * Child metadata: { source: 'A', extra: 'data' }

 *

 * Learning Points:

 * 1. child() creates a new config inheriting from parent

 * 2. Callbacks accumulate (child has parent's + its own)

 * 3. Tags accumulate (arrays concatenate)

 * 4. Metadata merges (child overrides parent keys)

 * 5. merge() treats both configs equally

 * 6. child() treats parent as base, child as override

 */