Skip to content

Commit fc3a18f

Browse files
authored
Merge pull request #10 from llm-agents-php/feature/agent-execution-interceptors
Adds agent execution interceptors
2 parents ca0b910 + a346e5d commit fc3a18f

File tree

6 files changed

+100
-6
lines changed

6 files changed

+100
-6
lines changed

app/src/Agents/AgentsCaller/AskAgentTool.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace App\Agents\AgentsCaller;
66

77
use App\Domain\Tool\PhpTool;
8-
use LLM\Agents\Agent\AgentExecutor;
8+
use LLM\Agents\AgentExecutor\ExecutorInterface;
99
use LLM\Agents\LLM\Prompt\Chat\ToolCallResultMessage;
1010
use LLM\Agents\LLM\Response\ToolCalledResponse;
11+
use LLM\Agents\PromptGenerator\Context;
1112
use LLM\Agents\Tool\ToolExecutor;
1213

1314
/**
@@ -18,7 +19,7 @@ final class AskAgentTool extends PhpTool
1819
public const NAME = 'ask_agent';
1920

2021
public function __construct(
21-
private readonly AgentExecutor $executor,
22+
private readonly ExecutorInterface $executor,
2223
private readonly ToolExecutor $toolExecutor,
2324
) {
2425
parent::__construct(
@@ -47,7 +48,7 @@ public function execute(object $input): string|\Stringable
4748

4849
// TODO: make async
4950
while (true) {
50-
$execution = $this->executor->execute($input->name, $prompt);
51+
$execution = $this->executor->execute(agent: $input->name, prompt: $prompt, promptContext: new Context());
5152
$result = $execution->result;
5253
$prompt = $execution->prompt;
5354

app/src/Application/Bootloader/AgentsBootloader.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
use LLM\Agents\Agent\AgentRegistry;
1010
use LLM\Agents\Agent\AgentRegistryInterface;
1111
use LLM\Agents\Agent\AgentRepositoryInterface;
12+
use LLM\Agents\AgentExecutor\ExecutorInterface;
13+
use LLM\Agents\AgentExecutor\ExecutorPipeline;
14+
use LLM\Agents\AgentExecutor\Interceptor\GeneratePromptInterceptor;
15+
use LLM\Agents\AgentExecutor\Interceptor\InjectModelInterceptor;
16+
use LLM\Agents\AgentExecutor\Interceptor\InjectOptionsInterceptor;
17+
use LLM\Agents\AgentExecutor\Interceptor\InjectResponseIntoPromptInterceptor;
18+
use LLM\Agents\AgentExecutor\Interceptor\InjectToolsInterceptor;
1219
use LLM\Agents\JsonSchema\Mapper\SchemaMapper;
1320
use LLM\Agents\LLM\ContextFactoryInterface;
1421
use LLM\Agents\LLM\OptionsFactoryInterface;
@@ -38,6 +45,25 @@ public function defineSingletons(): array
3845
ContextFactoryInterface::class => ContextFactory::class,
3946

4047
SchemaMapperInterface::class => SchemaMapper::class,
48+
49+
ExecutorInterface::class => static function (
50+
ExecutorPipeline $pipeline,
51+
52+
// Interceptors
53+
GeneratePromptInterceptor $generatePrompt,
54+
InjectModelInterceptor $injectModel,
55+
InjectToolsInterceptor $injectTools,
56+
InjectOptionsInterceptor $injectOptions,
57+
InjectResponseIntoPromptInterceptor $injectResponseIntoPrompt,
58+
) {
59+
return $pipeline->withInterceptor(
60+
$generatePrompt,
61+
$injectModel,
62+
$injectTools,
63+
$injectOptions,
64+
$injectResponseIntoPrompt,
65+
);
66+
},
4167
];
4268
}
4369

app/src/Application/Bootloader/AgentsChatBootloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace App\Application\Bootloader;
66

7+
use App\Domain\Chat\PromptGenerator\SessionContextInjector;
78
use App\Domain\Chat\SimpleChatService;
89
use App\Infrastructure\RoadRunner\Chat\ChatHistoryRepository;
910
use LLM\Agents\Chat\ChatHistoryRepositoryInterface;
1011
use LLM\Agents\Chat\ChatServiceInterface;
1112
use LLM\Agents\PromptGenerator\Interceptors\AgentMemoryInjector;
1213
use LLM\Agents\PromptGenerator\Interceptors\InstructionGenerator;
1314
use LLM\Agents\PromptGenerator\Interceptors\LinkedAgentsInjector;
14-
use LLM\Agents\PromptGenerator\Interceptors\SessionContextInjector;
1515
use LLM\Agents\PromptGenerator\Interceptors\UserPromptInjector;
1616
use LLM\Agents\PromptGenerator\PromptGeneratorPipeline;
1717
use Spiral\Boot\Bootloader\Bootloader;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Chat\PromptGenerator;
6+
7+
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
8+
use LLM\Agents\PromptGenerator\InterceptorHandler;
9+
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
10+
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
11+
12+
final class Dump implements PromptInterceptorInterface
13+
{
14+
public function generate(
15+
PromptGeneratorInput $input,
16+
InterceptorHandler $next,
17+
): PromptInterface {
18+
dump($input->prompt->format());
19+
20+
return $next($input);
21+
}
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Chat\PromptGenerator;
6+
7+
use LLM\Agents\LLM\Prompt\Chat\MessagePrompt;
8+
use LLM\Agents\LLM\Prompt\Chat\Prompt;
9+
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
10+
use LLM\Agents\PromptGenerator\Context;
11+
use LLM\Agents\PromptGenerator\InterceptorHandler;
12+
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
13+
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
14+
15+
final class SessionContextInjector implements PromptInterceptorInterface
16+
{
17+
public function generate(
18+
PromptGeneratorInput $input,
19+
InterceptorHandler $next,
20+
): PromptInterface {
21+
\assert($input->prompt instanceof Prompt);
22+
23+
if (
24+
(!$input->context instanceof Context)
25+
|| $input->context->getAuthContext() === null
26+
) {
27+
return $next($input);
28+
}
29+
30+
return $next(
31+
input: $input->withPrompt(
32+
$input->prompt
33+
->withAddedMessage(
34+
MessagePrompt::system(
35+
prompt: 'Session context: {active_context}',
36+
),
37+
)->withValues(
38+
values: [
39+
'active_context' => \json_encode($input->context->getAuthContext()),
40+
],
41+
),
42+
),
43+
);
44+
}
45+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"llm-agents/agent-site-status-checker": "^1.0",
1414
"llm-agents/agent-smart-home-control": "^1.0",
1515
"llm-agents/agent-symfony-console": "^1.0",
16-
"llm-agents/agents": "^1.2",
16+
"llm-agents/agents": "^1.3",
1717
"llm-agents/cli-chat": "^1.2",
1818
"llm-agents/json-schema-mapper": "^1.0",
1919
"llm-agents/openai-client": "^1.0",
20-
"llm-agents/prompt-generator": "^1.0",
20+
"llm-agents/prompt-generator": "^1.1",
2121
"nesbot/carbon": "^3.4",
2222
"nyholm/psr7": "^1.8",
2323
"openai-php/client": "^0.10.1",

0 commit comments

Comments
 (0)