Skip to content

Commit 67445ae

Browse files
committed
Docs updates
1 parent 14680ae commit 67445ae

File tree

346 files changed

+2854
-1917
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

346 files changed

+2854
-1917
lines changed

docs-build/cookbook/agents/agent_builder/agent_basic.mdx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: 'Basic Agent Usage'
33
docname: 'agent_basic'
44
order: 1
5+
id: '67ca'
56
---
6-
77
## Overview
88

99
The simplest use of an Agent - a straightforward Q&A without tools. The agent uses
@@ -13,7 +13,8 @@ a message, processing it through the LLM, and returning a response.
1313
Key concepts:
1414
- `AgentBuilder`: Constructs configured agent instances
1515
- `AgentState`: Immutable state container for messages and metadata
16-
- `Agent::execute()`: Executes the agent loop until completion
16+
- `AgentLoop::execute()`: Executes the agent loop until completion
17+
- `UseGuards`: Adds step/token/time safety limits
1718
- `AgentConsoleLogger`: Provides visibility into agent execution stages
1819

1920

@@ -23,7 +24,12 @@ Key concepts:
2324
<?php
2425
require 'examples/boot.php';
2526

26-
use Cognesy\Agents\Builder\AgentBuilder;use Cognesy\Agents\Data\AgentState;use Cognesy\Agents\Events\Support\AgentConsoleLogger;use Cognesy\Messages\Messages;
27+
use Cognesy\Agents\Builder\AgentBuilder;
28+
use Cognesy\Agents\Capability\Core\UseGuards;
29+
use Cognesy\Agents\Capability\Core\UseLLMConfig;
30+
use Cognesy\Agents\Data\AgentState;
31+
use Cognesy\Agents\Events\Support\AgentConsoleLogger;
32+
use Cognesy\Messages\Messages;
2733

2834
// Create a console logger for visibility into agent execution
2935
$logger = new AgentConsoleLogger(
@@ -34,7 +40,8 @@ $logger = new AgentConsoleLogger(
3440

3541
// Build a basic agent
3642
$agent = AgentBuilder::base()
37-
->withLlmPreset('anthropic')
43+
->withCapability(new UseLLMConfig(preset: 'anthropic'))
44+
->withCapability(new UseGuards(maxSteps: 3, maxTokens: 4096, maxExecutionTime: 30))
3845
->build()
3946
->wiretap($logger->wiretap());
4047

@@ -49,10 +56,16 @@ echo "=== Agent Execution Log ===\n\n";
4956
$finalState = $agent->execute($state);
5057

5158
echo "\n=== Result ===\n";
52-
$response = $finalState->currentStep()?->outputMessages()->toString() ?? 'No response';
59+
$response = $finalState->finalResponse()->toString() ?: 'No response';
5360
echo "Answer: {$response}\n";
5461
echo "Steps: {$finalState->stepCount()}\n";
5562
echo "Tokens: {$finalState->usage()->total()}\n";
5663
echo "Status: {$finalState->status()->value}\n";
64+
65+
// Assertions
66+
assert($finalState->status() === \Cognesy\Agents\Enums\ExecutionStatus::Completed);
67+
assert(!empty($finalState->finalResponse()->toString()), 'Expected non-empty response');
68+
assert($finalState->stepCount() >= 1, 'Expected at least 1 step');
69+
assert($finalState->usage()->total() > 0, 'Expected token usage > 0');
5770
?>
5871
```

docs-build/cookbook/agents/agent_builder/agent_file_system.mdx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: 'Agent with File System Tools'
33
docname: 'agent_file_system'
44
order: 2
5+
id: '2b2f'
56
---
6-
77
## Overview
88

99
Agents can be equipped with file system capabilities to read, write, search, and edit files
@@ -12,7 +12,8 @@ refactoring assistance, and other file-based operations. The agent determines wh
1212
operations to perform based on the task.
1313

1414
Key concepts:
15-
- `UseFileTools`: Capability that adds file system tools to the agent
15+
- `UseFileTools`: Capability that adds core file tools (`read_file`, `write_file`, `edit_file`)
16+
- `UseTools`: Adds extra tools explicitly when needed (`list_dir`, `search_files`)
1617
- Working directory: Root path for all file operations (security boundary)
1718
- Available tools: `read_file`, `write_file`, `edit_file`, `list_dir`, `search_files`
1819
- `AgentConsoleLogger`: Provides visibility into agent execution stages
@@ -23,7 +24,15 @@ Key concepts:
2324
<?php
2425
require 'examples/boot.php';
2526

26-
use Cognesy\Agents\Builder\AgentBuilder;use Cognesy\Agents\Capabilities\File\UseFileTools;use Cognesy\Agents\Data\AgentState;use Cognesy\Agents\Events\Support\AgentConsoleLogger;use Cognesy\Messages\Messages;
27+
use Cognesy\Agents\Builder\AgentBuilder;
28+
use Cognesy\Agents\Capability\Core\UseGuards;
29+
use Cognesy\Agents\Capability\Core\UseTools;
30+
use Cognesy\Agents\Capability\File\ListDirTool;
31+
use Cognesy\Agents\Capability\File\SearchFilesTool;
32+
use Cognesy\Agents\Capability\File\UseFileTools;
33+
use Cognesy\Agents\Data\AgentState;
34+
use Cognesy\Agents\Events\Support\AgentConsoleLogger;
35+
use Cognesy\Messages\Messages;
2736

2837
// Create console logger for execution visibility
2938
$logger = new AgentConsoleLogger(
@@ -39,6 +48,11 @@ $workDir = dirname(__DIR__, 3); // Project root
3948
// Build agent with file system capabilities
4049
$agent = AgentBuilder::base()
4150
->withCapability(new UseFileTools($workDir))
51+
->withCapability(new UseTools(
52+
ListDirTool::inDirectory($workDir),
53+
SearchFilesTool::inDirectory($workDir),
54+
))
55+
->withCapability(new UseGuards(maxSteps: 8, maxTokens: 8192, maxExecutionTime: 45))
4256
->build()
4357
->wiretap($logger->wiretap());
4458

@@ -62,10 +76,15 @@ echo "=== Agent Execution Log ===\n\n";
6276
$finalState = $agent->execute($state);
6377

6478
echo "\n=== Result ===\n";
65-
$response = $finalState->currentStep()?->outputMessages()->toString() ?? 'No response';
79+
$response = $finalState->finalResponse()->toString() ?: 'No response';
6680
echo "Answer: {$response}\n";
6781
echo "Steps: {$finalState->stepCount()}\n";
6882
echo "Tokens: {$finalState->usage()->total()}\n";
6983
echo "Status: {$finalState->status()->value}\n";
84+
85+
// Assertions
86+
assert(!empty($finalState->finalResponse()->toString()), 'Expected non-empty response');
87+
assert($finalState->stepCount() >= 1, 'Expected at least 1 step');
88+
assert($finalState->usage()->total() > 0, 'Expected token usage > 0');
7089
?>
7190
```

docs-build/cookbook/agents/agent_builder/agent_hooks.mdx

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: 'Agent Hooks - Tool Interception'
33
docname: 'agent_hooks'
44
order: 4
5+
id: '9185'
56
---
6-
77
## Overview
88

99
Hooks allow you to intercept tool calls before and after execution. This example
@@ -14,7 +14,7 @@ Key concepts:
1414
- `CallableHook`: Wraps a closure as a hook
1515
- `HookContext`: Provides access to tool call and agent state
1616
- `HookTriggers`: Defines when the hook fires (e.g., `beforeToolUse()`)
17-
- `addHook()`: Registers a hook on the builder with priority
17+
- `UseHook`: Registers a hook capability with explicit trigger/priority
1818
- `AgentConsoleLogger`: Provides visibility into agent execution stages
1919

2020
## Example
@@ -23,7 +23,15 @@ Key concepts:
2323
<?php
2424
require 'examples/boot.php';
2525

26-
use Cognesy\Agents\Builder\AgentBuilder;use Cognesy\Agents\Capabilities\Bash\UseBash;use Cognesy\Agents\Data\AgentState;use Cognesy\Agents\Events\Support\AgentConsoleLogger;use Cognesy\Agents\Hook\Collections\HookTriggers;use Cognesy\Agents\Hook\Data\HookContext;use Cognesy\Agents\Hook\Hooks\CallableHook;
26+
use Cognesy\Agents\Builder\AgentBuilder;
27+
use Cognesy\Agents\Capability\Bash\UseBash;
28+
use Cognesy\Agents\Capability\Core\UseGuards;
29+
use Cognesy\Agents\Capability\Core\UseHook;
30+
use Cognesy\Agents\Data\AgentState;
31+
use Cognesy\Agents\Events\Support\AgentConsoleLogger;
32+
use Cognesy\Agents\Hook\Collections\HookTriggers;
33+
use Cognesy\Agents\Hook\Data\HookContext;
34+
use Cognesy\Agents\Hook\Hooks\CallableHook;
2735

2836
// Create console logger for execution visibility
2937
$logger = new AgentConsoleLogger(
@@ -43,18 +51,30 @@ $blockedPatterns = [
4351
'dd if=',
4452
':(){:|:&};:', // Fork bomb
4553
];
54+
$blockedPatterns = array_map(
55+
static fn(string $pattern): string => strtolower(trim($pattern)),
56+
$blockedPatterns,
57+
);
4658

4759
// Build agent with bash capability and security hook
4860
$agent = AgentBuilder::base()
4961
->withCapability(new UseBash())
50-
->addHook(
62+
->withCapability(new UseHook(
5163
hook: new CallableHook(function (HookContext $ctx) use ($blockedPatterns): HookContext {
5264
$toolCall = $ctx->toolCall();
5365
if ($toolCall === null) {
5466
return $ctx;
5567
}
5668

57-
$command = $toolCall->args()['command'] ?? '';
69+
$args = $toolCall->args();
70+
$rawCommand = match (true) {
71+
is_array($args) && isset($args['command']) && is_string($args['command']) => $args['command'],
72+
default => '',
73+
};
74+
$command = strtolower(trim((string) preg_replace('/\s+/', ' ', $rawCommand)));
75+
if ($command === '') {
76+
return $ctx;
77+
}
5878

5979
// Check for dangerous patterns
6080
foreach ($blockedPatterns as $pattern) {
@@ -64,12 +84,13 @@ $agent = AgentBuilder::base()
6484
}
6585
}
6686

67-
echo " [HOOK] ALLOWED - {$command}\n";
87+
echo " [HOOK] ALLOWED - {$rawCommand}\n";
6888
return $ctx;
6989
}),
7090
triggers: HookTriggers::beforeToolUse(),
7191
priority: 100, // High priority = runs first
72-
)
92+
))
93+
->withCapability(new UseGuards(maxSteps: 8, maxTokens: 4096, maxExecutionTime: 30))
7394
->build()
7495
->wiretap($logger->wiretap());
7596

@@ -82,7 +103,7 @@ echo "=== Test 1: Safe Commands ===\n\n";
82103
$finalState = $agent->execute($state);
83104

84105
echo "\n=== Result ===\n";
85-
$response = $finalState->currentStep()?->outputMessages()->toString() ?? 'No response';
106+
$response = $finalState->finalResponse()->toString() ?: 'No response';
86107
echo "Answer: {$response}\n";
87108
echo "Steps: {$finalState->stepCount()}\n";
88109
echo "Status: {$finalState->status()->value}\n";
@@ -100,12 +121,17 @@ $hasErrors = $finalState2->currentStep()?->hasErrors() ?? false;
100121
echo "Command was " . ($hasErrors ? "BLOCKED (security hook worked!)" : "executed") . "\n";
101122
echo "Steps: {$finalState2->stepCount()}\n";
102123
echo "Status: {$finalState2->status()->value}\n";
124+
125+
// Assertions
126+
assert(!empty($finalState->finalResponse()->toString()), 'Expected non-empty response from safe commands');
127+
assert($finalState->stepCount() >= 1, 'Expected at least 1 step for safe commands');
128+
assert($finalState2->stepCount() >= 1, 'Expected at least 1 step for dangerous command test');
103129
?>
104130
```
105131

106132
## How It Works
107133

108-
1. **Hook Registration**: `addHook()` registers a `CallableHook` with `HookTriggers::beforeToolUse()`
134+
1. **Hook Registration**: `UseHook` registers a `CallableHook` with `HookTriggers::beforeToolUse()`
109135
2. **Context Access**: `HookContext` provides `toolCall()` and `state()` accessors
110136
3. **Priority**: Higher priority (100) ensures this security check runs before other hooks
111137
4. **Blocking**: `$ctx->withToolExecutionBlocked($reason)` blocks the tool call with a reason
@@ -115,7 +141,7 @@ echo "Status: {$finalState2->status()->value}\n";
115141

116142
```php
117143
// After tool execution - for logging/metrics
118-
->addHook(
144+
->withCapability(new UseHook(
119145
hook: new CallableHook(function (HookContext $ctx): HookContext {
120146
$exec = $ctx->toolExecution();
121147
if ($exec !== null) {
@@ -124,19 +150,19 @@ echo "Status: {$finalState2->status()->value}\n";
124150
return $ctx;
125151
}),
126152
triggers: HookTriggers::afterToolUse(),
127-
)
153+
))
128154

129155
// Before each step - modify state
130-
->addHook(
156+
->withCapability(new UseHook(
131157
hook: new CallableHook(function (HookContext $ctx): HookContext {
132158
$state = $ctx->state()->withMetadata('step_started', microtime(true));
133159
return $ctx->withState($state);
134160
}),
135161
triggers: HookTriggers::beforeStep(),
136-
)
162+
))
137163

138164
// After each step
139-
->addHook(
165+
->withCapability(new UseHook(
140166
hook: new CallableHook(function (HookContext $ctx): HookContext {
141167
$started = $ctx->state()->metadata()->get('step_started');
142168
if ($started !== null) {
@@ -146,5 +172,5 @@ echo "Status: {$finalState2->status()->value}\n";
146172
return $ctx;
147173
}),
148174
triggers: HookTriggers::afterStep(),
149-
)
175+
))
150176
```

docs-build/cookbook/agents/agent_builder/agent_search.mdx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: 'Agent-Driven Codebase Search'
33
docname: 'agent_search'
44
order: 6
5+
id: '4e0c'
56
---
6-
77
## Overview
88

99
Demonstrates how agents can autonomously search codebases by:
@@ -17,7 +17,7 @@ analyzing results without predefined workflows. The agent decides which files to
1717
based on search results.
1818

1919
Key concepts:
20-
- `SearchFilesTool`: Search for files by pattern or content
20+
- `SearchFilesTool`: Search for files by filename/path pattern
2121
- `ReadFileTool`: Read file contents
2222
- `UseSubagents`: Spawn specialized subagents for subtasks
2323
- `AgentConsoleLogger`: Provides visibility into agent execution stages
@@ -28,7 +28,19 @@ Key concepts:
2828
<?php
2929
require 'examples/boot.php';
3030

31-
use Cognesy\Agents\AgentTemplate\Definitions\AgentDefinition;use Cognesy\Agents\AgentTemplate\Definitions\AgentDefinitionRegistry;use Cognesy\Agents\Builder\AgentBuilder;use Cognesy\Agents\Capabilities\File\UseFileTools;use Cognesy\Agents\Capabilities\Subagent\UseSubagents;use Cognesy\Agents\Collections\NameList;use Cognesy\Agents\Data\AgentState;use Cognesy\Agents\Events\Support\AgentConsoleLogger;use Cognesy\Messages\Messages;
31+
use Cognesy\Agents\Builder\AgentBuilder;
32+
use Cognesy\Agents\Capability\Core\UseGuards;
33+
use Cognesy\Agents\Capability\Core\UseTools;
34+
use Cognesy\Agents\Capability\File\ListDirTool;
35+
use Cognesy\Agents\Capability\File\SearchFilesTool;
36+
use Cognesy\Agents\Capability\File\UseFileTools;
37+
use Cognesy\Agents\Capability\Subagent\UseSubagents;
38+
use Cognesy\Agents\Collections\NameList;
39+
use Cognesy\Agents\Data\AgentState;
40+
use Cognesy\Agents\Events\Support\AgentConsoleLogger;
41+
use Cognesy\Agents\Template\AgentDefinitionRegistry;
42+
use Cognesy\Agents\Template\Data\AgentDefinition;
43+
use Cognesy\Messages\Messages;
3244

3345
// Create console logger for execution visibility
3446
$logger = new AgentConsoleLogger(
@@ -53,20 +65,25 @@ $registry->register(new AgentDefinition(
5365

5466
$registry->register(new AgentDefinition(
5567
name: 'searcher',
56-
description: 'Searches for files matching patterns',
57-
systemPrompt: 'You search for files matching patterns. Use glob patterns effectively.',
68+
description: 'Searches for files by filename/path patterns',
69+
systemPrompt: 'You search for files by filename/path patterns. Use glob patterns effectively.',
5870
tools: NameList::fromArray(['search_files']),
5971
));
6072

6173
// Build main orchestration agent
6274
$agent = AgentBuilder::base()
6375
->withCapability(new UseFileTools($workDir))
76+
->withCapability(new UseTools(
77+
ListDirTool::inDirectory($workDir),
78+
SearchFilesTool::inDirectory($workDir),
79+
))
6480
->withCapability(new UseSubagents(provider: $registry))
81+
->withCapability(new UseGuards(maxSteps: 12, maxTokens: 12288, maxExecutionTime: 90))
6582
->build()
6683
->wiretap($logger->wiretap());
6784

68-
// Ask a question that requires search
69-
$question = "Find all test files related to Agent capabilities and tell me what they test";
85+
// Ask a question that requires search + file reading
86+
$question = "Find all tool classes (files matching *Tool.php) under packages/agents/src/Capability/File/ and briefly describe what each tool does based on its code.";
7087

7188
$state = AgentState::empty()->withMessages(
7289
Messages::fromString($question)
@@ -78,10 +95,15 @@ echo "=== Agent Execution Log ===\n\n";
7895
$finalState = $agent->execute($state);
7996

8097
echo "\n=== Result ===\n";
81-
$answer = $finalState->currentStep()?->outputMessages()->toString() ?? 'No answer';
98+
$answer = $finalState->finalResponse()->toString() ?: 'No answer';
8299
echo "Answer: {$answer}\n";
83100
echo "Steps: {$finalState->stepCount()}\n";
84101
echo "Tokens: {$finalState->usage()->total()}\n";
85102
echo "Status: {$finalState->status()->value}\n";
103+
104+
// Assertions
105+
assert(!empty($finalState->finalResponse()->toString()), 'Expected non-empty response');
106+
assert($finalState->stepCount() >= 1, 'Expected at least 1 step');
107+
assert($finalState->usage()->total() > 0, 'Expected token usage > 0');
86108
?>
87109
```

0 commit comments

Comments
 (0)