-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuilder_pattern_example.php
More file actions
365 lines (294 loc) · 14.9 KB
/
Copy pathbuilder_pattern_example.php
File metadata and controls
365 lines (294 loc) · 14.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env php
<?php
/**
* Builder Pattern Example
*
* Demonstrates the Builder Pattern for type-safe configuration with:
* - Fluent API for readability
* - Type safety (compile-time checks)
* - IDE autocomplete support
* - Self-documenting code
*
* Run: php examples/builder_pattern_example.php
*/
require_once __DIR__ . '/../vendor/autoload.php';
use ClaudeAgents\Config\AgentConfigBuilder;
use ClaudeAgents\Factory\AgentFactory;
use ClaudeAgents\Tools\Tool;
use ClaudePhp\ClaudePhp;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Load environment
$dotenv = __DIR__ . '/../.env';
if (file_exists($dotenv)) {
$lines = file($dotenv, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
if (strpos($line, '=') === false) continue;
[$name, $value] = explode('=', $line, 2);
$_ENV[trim($name)] = trim($value);
}
}
$apiKey = $_ENV['ANTHROPIC_API_KEY'] ?? throw new RuntimeException('ANTHROPIC_API_KEY not set');
$client = new ClaudePhp(apiKey: $apiKey);
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Builder Pattern Example ║\n";
echo "║ Type-Safe Configuration with Fluent API ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
// ============================================================================
// Example 1: Array Configuration (Without Builder)
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "❌ Without Builder: Array Configuration\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Traditional array configuration:\n\n";
echo "```php\n";
echo "\$config = [\n";
echo " 'model' => 'claude-opus-4',\n";
echo " 'max_tokens' => 4096,\n";
echo " 'max_iterations' => 10,\n";
echo " 'system' => 'You are helpful',\n";
echo " 'thinking' => ['type' => 'enabled', 'budget_tokens' => 10000],\n";
echo " 'temperature' => 0.7,\n";
echo "];\n";
echo "```\n\n";
echo "Problems:\n";
echo " • No type safety - typos not caught (e.g., 'max_token' vs 'max_tokens')\n";
echo " • No IDE autocomplete\n";
echo " • Easy to forget required fields\n";
echo " • No validation until runtime\n";
echo " • Not self-documenting\n\n";
// ============================================================================
// Example 2: Basic Builder Usage
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "✅ Example 1: Basic Builder Usage\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$basicConfig = AgentConfigBuilder::create()
->withModel('claude-sonnet-4-20250514')
->withMaxTokens(2048)
->withMaxIterations(5)
->build();
echo "✅ Built basic configuration:\n\n";
echo "```php\n";
echo "\$config = AgentConfigBuilder::create()\n";
echo " ->withModel('claude-sonnet-4-20250514')\n";
echo " ->withMaxTokens(2048)\n";
echo " ->withMaxIterations(5)\n";
echo " ->build();\n";
echo "```\n\n";
echo "Benefits:\n";
echo " ✓ Type-safe: withMaxTokens() expects int\n";
echo " ✓ IDE autocomplete: Shows all available methods\n";
echo " ✓ Self-documenting: Clear what each parameter does\n";
echo " ✓ Chainable: Fluent interface\n\n";
// ============================================================================
// Example 3: Complete Configuration
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 2: Complete Configuration\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$calculator = Tool::create('calculate')
->description('Perform calculations')
->stringParam('expression', 'Math expression')
->handler(fn($input) => eval("return {$input['expression']};"));
$completeConfig = AgentConfigBuilder::create()
// Model settings
->withModel('claude-opus-4-20250514')
->withMaxTokens(4096)
->withTemperature(0.7)
->withTopP(0.9)
// Agent settings
->withName('complete_agent')
->withSystemPrompt('You are a comprehensive assistant with all features enabled')
->withMaxIterations(15)
->withTimeout(300)
// Extended thinking
->withThinking(10000)
// Tools
->addTool($calculator)
// Custom options
->withCustomOption('caching', true)
->withCustomOption('streaming', false)
->build();
echo "✅ Built complete configuration with all features:\n\n";
echo "Configuration includes:\n";
echo " • Model: claude-opus-4-20250514\n";
echo " • Max Tokens: 4096\n";
echo " • Temperature: 0.7\n";
echo " • Top P: 0.9\n";
echo " • Name: complete_agent\n";
echo " • System Prompt: Custom\n";
echo " • Max Iterations: 15\n";
echo " • Timeout: 300s\n";
echo " • Extended Thinking: 10000 tokens\n";
echo " • Tools: 1 tool added\n";
echo " • Custom Options: caching, streaming\n\n";
// ============================================================================
// Example 4: Builder Methods Reference
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 3: Available Builder Methods\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Model Configuration:\n";
echo " • withModel(string \$model)\n";
echo " • withMaxTokens(int \$maxTokens)\n";
echo " • withTemperature(float \$temperature)\n";
echo " • withTopP(float \$topP)\n";
echo " • withTopK(int \$topK)\n\n";
echo "Agent Configuration:\n";
echo " • withName(string \$name)\n";
echo " • withSystemPrompt(string \$prompt)\n";
echo " • withMaxIterations(int \$iterations)\n";
echo " • withTimeout(int \$seconds)\n\n";
echo "Extended Thinking:\n";
echo " • withThinking(int \$budgetTokens) // Shorthand\n";
echo " • withThinkingConfig(array \$config) // Full config\n\n";
echo "Tools:\n";
echo " • withTools(array \$tools) // Set all tools\n";
echo " • addTool(Tool \$tool) // Add single tool\n\n";
echo "Custom Options:\n";
echo " • withCustomOption(string \$key, mixed \$value)\n";
echo " • withCustomOptions(array \$options)\n\n";
echo "Build:\n";
echo " • build(): AgentConfig // Returns object\n";
echo " • toArray(): array // Returns array\n\n";
// ============================================================================
// Example 5: Reusable Configuration Templates
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 4: Reusable Configuration Templates\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
// Base configuration
$baseConfig = AgentConfigBuilder::create()
->withModel('claude-sonnet-4-20250514')
->withMaxTokens(2048)
->withTimeout(300);
echo "Created base configuration template\n\n";
// Production variant
$productionConfig = clone $baseConfig;
$productionConfig
->withThinking(10000)
->withMaxIterations(15)
->withCustomOption('caching', true);
echo "✅ Production config (base + thinking + caching)\n";
// Development variant
$devConfig = clone $baseConfig;
$devConfig
->withMaxIterations(3)
->withCustomOption('debug', true);
echo "✅ Development config (base + limited iterations + debug)\n";
// Testing variant
$testConfig = clone $baseConfig;
$testConfig
->withMaxTokens(512)
->withMaxIterations(2)
->withCustomOption('mock_mode', true);
echo "✅ Test config (base + reduced tokens + mock mode)\n\n";
echo "💡 Clone base config for different environments:\n";
echo " • Production: Full features, caching enabled\n";
echo " • Development: Debug mode, fewer iterations\n";
echo " • Testing: Mock mode, minimal tokens\n\n";
// ============================================================================
// Example 6: Integration with Factory
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 5: Builder + Factory Integration\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$logger = new Logger('builder_demo');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
$factory = new AgentFactory($client, $logger);
// Build config with Builder
$agentConfig = AgentConfigBuilder::create()
->withModel('claude-sonnet-4-20250514')
->withMaxTokens(2048)
->withMaxIterations(5)
->toArray(); // Convert to array for factory
// Create agent with Factory
$agent = $factory->create('react', $agentConfig);
echo "✅ Combined Builder + Factory:\n\n";
echo "```php\n";
echo "\$factory = new AgentFactory(\$client, \$logger);\n\n";
echo "\$config = AgentConfigBuilder::create()\n";
echo " ->withMaxTokens(2048)\n";
echo " ->withMaxIterations(5)\n";
echo " ->toArray();\n\n";
echo "\$agent = \$factory->create('react', \$config);\n";
echo "```\n\n";
echo "💡 Best of both patterns:\n";
echo " • Builder: Type-safe configuration\n";
echo " • Factory: Consistent agent creation\n";
echo " • Result: Production-ready code\n\n";
// ============================================================================
// Example 7: Type Safety Demonstration
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 6: Type Safety Benefits\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Type safety prevents common errors:\n\n";
echo "❌ Array config (runtime error):\n";
echo "```php\n";
echo "\$config = [\n";
echo " 'max_token' => '2048', // Typo! Runtime error\n";
echo " 'temperature' => 'high', // Wrong type! Runtime error\n";
echo "];\n";
echo "```\n\n";
echo "✅ Builder (compile-time safety):\n";
echo "```php\n";
echo "\$config = AgentConfigBuilder::create()\n";
echo " ->withMaxTokens(2048) // Correct method, int required\n";
echo " ->withTemperature(0.7); // float required\n";
echo "// Typos caught by IDE/PHP\n";
echo "// Type errors caught before running\n";
echo "```\n\n";
// ============================================================================
// Summary
// ============================================================================
echo str_repeat("═", 80) . "\n";
echo "📚 Builder Pattern Summary\n";
echo str_repeat("═", 80) . "\n\n";
echo "✅ Benefits:\n\n";
echo "1. Type Safety:\n";
echo " • Compile-time checking\n";
echo " • Method signatures enforce types\n";
echo " • IDE catches typos immediately\n\n";
echo "2. Readability:\n";
echo " • Self-documenting code\n";
echo " • Clear parameter names\n";
echo " • Fluent, chainable interface\n\n";
echo "3. IDE Support:\n";
echo " • Full autocomplete\n";
echo " • Inline documentation\n";
echo " • Type hints\n\n";
echo "4. Maintainability:\n";
echo " • Easy to add new options\n";
echo " • Backward compatible\n";
echo " • Clear deprecation path\n\n";
echo "5. Validation:\n";
echo " • Early error detection\n";
echo " • Type constraints\n";
echo " • Required parameters\n\n";
echo "🎯 When to Use:\n\n";
echo "• Complex configurations (>3 parameters)\n";
echo "• Type safety is important\n";
echo "• Multiple developers on team\n";
echo "• Production code\n";
echo "• When IDE support matters\n\n";
echo "📖 Comparison:\n\n";
echo "Use Builder when:\n";
echo " ✓ Configuration has many parameters\n";
echo " ✓ Type safety is critical\n";
echo " ✓ Team collaboration\n";
echo " ✓ Long-term maintenance\n\n";
echo "Use Arrays when:\n";
echo " • Simple configuration (<3 params)\n";
echo " • Rapid prototyping\n";
echo " • Dynamic configuration from external sources\n\n";
echo "📚 Learn More:\n";
echo "• Design Patterns Guide: docs/DesignPatterns.md\n";
echo "• Complete Demo: examples/design_patterns_demo.php\n";
echo "• Factory Pattern: examples/factory_pattern_example.php\n";
echo "• Best Practices: docs/BestPractices.md\n\n";
echo str_repeat("═", 80) . "\n";
echo "✨ Builder Pattern Example Complete!\n";
echo str_repeat("═", 80) . "\n";