-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelpers.php
More file actions
456 lines (402 loc) · 13.7 KB
/
helpers.php
File metadata and controls
456 lines (402 loc) · 13.7 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* Shared Helper Functions for Agentic AI Tutorials
*
* This file provides common utilities used across all tutorials to avoid code duplication
* and provide consistent patterns for building AI agents.
*/
/**
* Run an agent loop with configurable iteration limits
*
* This is the core ReAct loop implementation that powers autonomous agents.
* It continues calling Claude and executing tools until:
* - Claude provides a final answer (stop_reason = 'end_turn')
* - Maximum iterations reached
* - An error occurs
*
* @param ClaudePhp\ClaudePhp $client The Claude client instance
* @param array $messages Current conversation history
* @param array $tools Available tools for the agent
* @param array $config Configuration options:
* - max_iterations: Maximum loop iterations (default: 10)
* - model: Model to use (default: claude-sonnet-4-5)
* - max_tokens: Max tokens per response (default: 4096)
* - debug: Show debug output (default: false)
* - system: System prompt (optional)
* - thinking: Enable extended thinking (optional)
*
* @return array Final response and updated messages
*/
function runAgentLoop($client, array $messages, array $tools, array $config = []): array
{
$maxIterations = $config['max_iterations'] ?? 10;
$model = $config['model'] ?? 'claude-sonnet-4-5';
$maxTokens = $config['max_tokens'] ?? 4096;
$debug = $config['debug'] ?? false;
$system = $config['system'] ?? null;
$thinking = $config['thinking'] ?? null;
$iteration = 0;
$finalResponse = null;
while ($iteration < $maxIterations) {
$iteration++;
if ($debug) {
echo "\n" . str_repeat("=", 80) . "\n";
echo "🔄 Agent Iteration {$iteration}/{$maxIterations}\n";
echo str_repeat("=", 80) . "\n";
}
// Build request parameters
$params = [
'model' => $model,
'max_tokens' => $maxTokens,
'messages' => $messages,
'tools' => $tools,
];
if ($system) {
$params['system'] = $system;
}
if ($thinking) {
$params['thinking'] = $thinking;
}
// Call Claude
try {
$response = $client->messages()->create($params);
} catch (Exception $e) {
if ($debug) {
echo "❌ Error: {$e->getMessage()}\n";
}
return [
'success' => false,
'error' => $e->getMessage(),
'messages' => $messages,
'iterations' => $iteration
];
}
if ($debug) {
debugAgentStep($response, $iteration);
}
// Add assistant response to conversation
$messages[] = [
'role' => 'assistant',
'content' => $response->content
];
// Check if we're done
if ($response->stop_reason === 'end_turn') {
$finalResponse = $response;
if ($debug) {
echo "\n✅ Agent completed successfully!\n";
}
break;
}
// Extract and execute tool calls
if ($response->stop_reason === 'tool_use') {
$toolUses = extractToolUses($response);
if (empty($toolUses)) {
if ($debug) {
echo "⚠️ stop_reason='tool_use' but no tool uses found\n";
}
break;
}
// Execute each tool and collect results
$toolResults = [];
foreach ($toolUses as $toolUse) {
if ($debug) {
echo "\n🔧 Executing tool: {$toolUse['name']}\n";
echo " Input: " . json_encode($toolUse['input']) . "\n";
}
// This is where you'd execute the actual tool
// In tutorials, we provide the tool executor function
$toolResults[] = [
'type' => 'tool_result',
'tool_use_id' => $toolUse['id'],
'content' => '[Tool executor not configured in helper]'
];
}
// Add tool results to conversation
$messages[] = [
'role' => 'user',
'content' => $toolResults
];
} else {
// Unexpected stop reason
if ($debug) {
echo "⚠️ Unexpected stop_reason: {$response->stop_reason}\n";
}
$finalResponse = $response;
break;
}
}
if ($iteration >= $maxIterations && !$finalResponse) {
if ($debug) {
echo "\n⚠️ Max iterations reached without completion\n";
}
}
return [
'success' => $finalResponse !== null,
'response' => $finalResponse,
'messages' => $messages,
'iterations' => $iteration
];
}
/**
* Extract tool use blocks from a Claude response
*
* @param mixed $response Claude API response
* @return array Array of tool use blocks
*/
function extractToolUses($response): array
{
$toolUses = [];
foreach ($response->content as $block) {
if (is_array($block) && ($block['type'] ?? null) === 'tool_use') {
$toolUses[] = $block;
} elseif (is_object($block) && isset($block->type) && $block->type === 'tool_use') {
// Handle object format
$toolUses[] = [
'type' => 'tool_use',
'id' => $block->id,
'name' => $block->name,
'input' => (array)$block->input
];
}
}
return $toolUses;
}
/**
* Format a tool execution result for returning to Claude
*
* @param string $toolUseId The tool_use_id from Claude's request
* @param mixed $result The result from executing the tool
* @param bool $isError Whether this is an error result
* @return array Formatted tool_result block
*/
function formatToolResult(string $toolUseId, $result, bool $isError = false): array
{
$toolResult = [
'type' => 'tool_result',
'tool_use_id' => $toolUseId,
'content' => is_string($result) ? $result : json_encode($result)
];
if ($isError) {
$toolResult['is_error'] = true;
}
return $toolResult;
}
/**
* Debug output for agent reasoning steps
*
* Shows what Claude is thinking and planning to do
*
* @param mixed $response Claude API response
* @param int $iteration Current iteration number
*/
function debugAgentStep($response, int $iteration): void
{
echo "\n📊 Step {$iteration} Analysis:\n";
echo str_repeat("-", 80) . "\n";
// Show thinking if present
foreach ($response->content as $block) {
if (is_array($block)) {
if (($block['type'] ?? null) === 'thinking') {
$thinking = $block['thinking'] ?? '';
echo "💭 Thinking: " . substr($thinking, 0, 200);
if (strlen($thinking) > 200) {
echo "... (truncated)\n";
} else {
echo "\n";
}
} elseif (($block['type'] ?? null) === 'text') {
echo "💬 Response: {$block['text']}\n";
} elseif (($block['type'] ?? null) === 'tool_use') {
echo "🔧 Tool Call: {$block['name']}\n";
echo " Parameters: " . json_encode($block['input']) . "\n";
}
}
}
echo "🛑 Stop Reason: {$response->stop_reason}\n";
echo "📈 Tokens: Input={$response->usage->input_tokens}, Output={$response->usage->output_tokens}\n";
}
/**
* Manage conversation history to stay within token limits
*
* This function keeps the most recent messages and removes older ones
* to prevent hitting context window limits.
*
* @param array $messages Current conversation history
* @param int $maxMessages Maximum number of message pairs to keep
* @return array Trimmed message history
*/
function manageConversationHistory(array $messages, int $maxMessages = 10): array
{
if (count($messages) <= $maxMessages) {
return $messages;
}
// Keep the most recent messages
// Always keep user-assistant pairs
$keep = [];
$pairs = [];
for ($i = 0; $i < count($messages); $i++) {
$role = $messages[$i]['role'];
if ($role === 'user') {
// Start a new pair
$pairs[] = [$messages[$i]];
} elseif ($role === 'assistant' && !empty($pairs)) {
// Complete the current pair
$pairs[count($pairs) - 1][] = $messages[$i];
}
}
// Keep the last N pairs
$keepPairs = array_slice($pairs, -$maxMessages);
// Flatten back to messages
foreach ($keepPairs as $pair) {
foreach ($pair as $msg) {
$keep[] = $msg;
}
}
return $keep;
}
/**
* Calculate approximate token count for messages
*
* This is a rough estimation: ~4 characters = 1 token
* For accurate counts, use the Claude API's countTokens endpoint
*
* @param array $messages Message array
* @return int Approximate token count
*/
function estimateTokens(array $messages): int
{
$text = json_encode($messages);
return (int)ceil(strlen($text) / 4);
}
/**
* Pretty print a message array for debugging
*
* @param array $messages Messages to print
* @param int $maxContentLength Maximum length of content to show
*/
function printMessages(array $messages, int $maxContentLength = 100): void
{
echo "\n📝 Conversation History:\n";
echo str_repeat("=", 80) . "\n";
foreach ($messages as $i => $message) {
$role = $message['role'];
$content = $message['content'];
echo "\n[{$i}] {$role}:\n";
if (is_string($content)) {
$display = strlen($content) > $maxContentLength
? substr($content, 0, $maxContentLength) . '...'
: $content;
echo " {$display}\n";
} elseif (is_array($content)) {
foreach ($content as $block) {
if (is_array($block)) {
$type = $block['type'] ?? 'unknown';
if ($type === 'text') {
$text = $block['text'] ?? '';
$display = strlen($text) > $maxContentLength
? substr($text, 0, $maxContentLength) . '...'
: $text;
echo " [text] {$display}\n";
} elseif ($type === 'tool_use') {
echo " [tool_use] {$block['name']}\n";
} elseif ($type === 'tool_result') {
$result = $block['content'] ?? '';
$display = strlen($result) > $maxContentLength
? substr($result, 0, $maxContentLength) . '...'
: $result;
echo " [tool_result] {$display}\n";
}
}
}
}
}
echo "\n" . str_repeat("=", 80) . "\n";
}
/**
* Extract text content from a Claude response
*
* @param mixed $response Claude API response
* @return string Combined text from all text blocks
*/
function extractTextContent($response): string
{
$text = [];
foreach ($response->content as $block) {
if (is_array($block) && ($block['type'] ?? null) === 'text') {
$text[] = $block['text'];
} elseif (is_object($block) && isset($block->type) && $block->type === 'text') {
$text[] = $block->text;
}
}
return implode("\n", $text);
}
/**
* Create a simple tool definition
*
* @param string $name Tool name
* @param string $description What the tool does
* @param array $parameters Parameter definitions
* @param array $required Required parameter names
* @return array Tool definition for Claude
*/
function createTool(string $name, string $description, array $parameters, array $required = []): array
{
return [
'name' => $name,
'description' => $description,
'input_schema' => [
'type' => 'object',
'properties' => $parameters,
'required' => $required
]
];
}
/**
* Simple retry wrapper with exponential backoff
*
* @param callable $fn Function to retry
* @param int $maxAttempts Maximum retry attempts
* @param int $initialDelayMs Initial delay in milliseconds
* @return mixed Result from function
* @throws Exception if all retries fail
*/
function retryWithBackoff(callable $fn, int $maxAttempts = 3, int $initialDelayMs = 1000)
{
$attempt = 0;
$delay = $initialDelayMs;
while ($attempt < $maxAttempts) {
try {
return $fn();
} catch (Exception $e) {
$attempt++;
if ($attempt >= $maxAttempts) {
throw $e;
}
// Exponential backoff
usleep($delay * 1000);
$delay *= 2;
}
}
}
/**
* Colorize console output (for terminals that support it)
*
* @param string $text Text to colorize
* @param string $color Color name (red, green, yellow, blue, magenta, cyan)
* @return string Colorized text
*/
function colorize(string $text, string $color): string
{
$colors = [
'red' => "\033[31m",
'green' => "\033[32m",
'yellow' => "\033[33m",
'blue' => "\033[34m",
'magenta' => "\033[35m",
'cyan' => "\033[36m",
'reset' => "\033[0m"
];
$colorCode = $colors[$color] ?? '';
$resetCode = $colors['reset'];
return $colorCode . $text . $resetCode;
}