Skip to content

Commit b8067a1

Browse files
authored
Merge pull request #1 from llm-agents-php/feature/interceptors-handler
Adds interceptors handler
2 parents 118a9fb + 8b3f734 commit b8067a1

10 files changed

+181
-178
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"require": {
66
"php": "^8.3",
7-
"llm-agents/agents": "^1.0",
7+
"llm-agents/agents": "^1.2",
88
"llm-agents/json-schema-mapper": "^1.0"
99
},
1010
"require-dev": {

src/InterceptorHandler.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\PromptGenerator;
6+
7+
use LLM\Agents\LLM\AgentPromptGeneratorInterface;
8+
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
9+
10+
final readonly class InterceptorHandler
11+
{
12+
public function __construct(
13+
private AgentPromptGeneratorInterface $generator,
14+
) {}
15+
16+
public function __invoke(PromptGeneratorInput $input): PromptInterface
17+
{
18+
return $this->generator->generate(
19+
agent: $input->agent,
20+
userPrompt: $input->userPrompt,
21+
context: $input->context,
22+
prompt: $input->prompt,
23+
);
24+
}
25+
}

src/Interceptors/AgentMemoryInjector.php

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,43 @@
44

55
namespace LLM\Agents\PromptGenerator\Interceptors;
66

7-
use LLM\Agents\Agent\AgentInterface;
8-
use LLM\Agents\LLM\AgentPromptGeneratorInterface;
97
use LLM\Agents\LLM\Prompt\Chat\MessagePrompt;
108
use LLM\Agents\LLM\Prompt\Chat\Prompt;
119
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
12-
use LLM\Agents\LLM\PromptContextInterface;
10+
use LLM\Agents\PromptGenerator\InterceptorHandler;
11+
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
1312
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
1413
use LLM\Agents\Solution\SolutionMetadata;
1514

1615
final class AgentMemoryInjector implements PromptInterceptorInterface
1716
{
1817
public function generate(
19-
AgentInterface $agent,
20-
\Stringable|string $userPrompt,
21-
PromptInterface $prompt,
22-
PromptContextInterface $context,
23-
AgentPromptGeneratorInterface $next,
18+
PromptGeneratorInput $input,
19+
InterceptorHandler $next,
2420
): PromptInterface {
25-
\assert($prompt instanceof Prompt);
21+
\assert($input->prompt instanceof Prompt);
2622

27-
return $next->generate(
28-
agent: $agent,
29-
userPrompt: $userPrompt,
30-
context: $context,
31-
prompt: $prompt
32-
->withAddedMessage(
33-
MessagePrompt::system(
34-
prompt: 'Instructions about your experiences, follow them: {memory}. And also {dynamic_memory}',
35-
),
36-
)
37-
->withValues(
38-
values: [
39-
'memory' => \implode(
40-
PHP_EOL,
41-
\array_map(
42-
static fn(SolutionMetadata $metadata) => $metadata->content,
43-
$agent->getMemory(),
44-
),
23+
return $next(
24+
input: $input->withPrompt(
25+
$input->prompt
26+
->withAddedMessage(
27+
MessagePrompt::system(
28+
prompt: 'Instructions about your experiences, follow them: {memory}. And also {dynamic_memory}',
4529
),
46-
'dynamic_memory' => '',
47-
],
48-
),
30+
)
31+
->withValues(
32+
values: [
33+
'memory' => \implode(
34+
PHP_EOL,
35+
\array_map(
36+
static fn(SolutionMetadata $metadata) => $metadata->content,
37+
$input->agent->getMemory(),
38+
),
39+
),
40+
'dynamic_memory' => '',
41+
],
42+
),
43+
),
4944
);
5045
}
5146
}

src/Interceptors/InstructionGenerator.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,40 @@
44

55
namespace LLM\Agents\PromptGenerator\Interceptors;
66

7-
use LLM\Agents\Agent\AgentInterface;
8-
use LLM\Agents\LLM\AgentPromptGeneratorInterface;
97
use LLM\Agents\LLM\Prompt\Chat\MessagePrompt;
108
use LLM\Agents\LLM\Prompt\Chat\Prompt;
119
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
12-
use LLM\Agents\LLM\PromptContextInterface;
10+
use LLM\Agents\PromptGenerator\InterceptorHandler;
11+
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
1312
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
1413

1514
final class InstructionGenerator implements PromptInterceptorInterface
1615
{
1716
public function generate(
18-
AgentInterface $agent,
19-
\Stringable|string $userPrompt,
20-
PromptInterface $prompt,
21-
PromptContextInterface $context,
22-
AgentPromptGeneratorInterface $next,
17+
PromptGeneratorInput $input,
18+
InterceptorHandler $next,
2319
): PromptInterface {
24-
\assert($prompt instanceof Prompt);
20+
\assert($input->prompt instanceof Prompt);
2521

26-
return $next->generate(
27-
agent: $agent,
28-
userPrompt: $userPrompt,
29-
context: $context,
30-
prompt: $prompt
31-
->withAddedMessage(
32-
MessagePrompt::system(
33-
prompt: <<<'PROMPT'
22+
return $next(
23+
input: $input->withPrompt(
24+
$input->prompt
25+
->withAddedMessage(
26+
MessagePrompt::system(
27+
prompt: <<<'PROMPT'
3428
{prompt}
3529
Important rules:
3630
- always response in markdown format
3731
- think before responding to user
3832
PROMPT,
33+
),
34+
)
35+
->withValues(
36+
values: [
37+
'prompt' => $input->agent->getInstruction(),
38+
],
3939
),
40-
)
41-
->withValues(
42-
values: [
43-
'prompt' => $agent->getInstruction(),
44-
],
45-
),
40+
),
4641
);
4742
}
4843
}

src/Interceptors/LinkedAgentsInjector.php

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
namespace LLM\Agents\PromptGenerator\Interceptors;
66

7-
use LLM\Agents\Agent\AgentInterface;
87
use LLM\Agents\Agent\AgentRepositoryInterface;
9-
use LLM\Agents\LLM\AgentPromptGeneratorInterface;
108
use LLM\Agents\LLM\Prompt\Chat\MessagePrompt;
119
use LLM\Agents\LLM\Prompt\Chat\Prompt;
1210
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
13-
use LLM\Agents\LLM\PromptContextInterface;
11+
use LLM\Agents\PromptGenerator\InterceptorHandler;
12+
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
1413
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
1514
use LLM\Agents\Solution\AgentLink;
1615
use LLM\Agents\Tool\SchemaMapperInterface;
@@ -23,61 +22,53 @@ public function __construct(
2322
) {}
2423

2524
public function generate(
26-
AgentInterface $agent,
27-
\Stringable|string $userPrompt,
28-
PromptInterface $prompt,
29-
PromptContextInterface $context,
30-
AgentPromptGeneratorInterface $next,
25+
PromptGeneratorInput $input,
26+
InterceptorHandler $next,
3127
): PromptInterface {
32-
\assert($prompt instanceof Prompt);
28+
\assert($input->prompt instanceof Prompt);
3329

34-
if (\count($agent->getAgents()) === 0) {
35-
return $next->generate(
36-
agent: $agent,
37-
userPrompt: $userPrompt,
38-
context: $context,
39-
prompt: $prompt,
40-
);
30+
if (\count($input->agent->getAgents()) === 0) {
31+
return $next($input);
4132
}
4233

4334
$associatedAgents = \array_map(
4435
fn(AgentLink $agent): array => [
4536
'agent' => $this->agents->get($agent->getName()),
4637
'output_schema' => \json_encode($this->schemaMapper->toJsonSchema($agent->outputSchema)),
4738
],
48-
$agent->getAgents(),
39+
$input->agent->getAgents(),
4940
);
5041

51-
return $next->generate(
52-
agent: $agent,
53-
userPrompt: $userPrompt,
54-
context: $context,
55-
prompt: $prompt
56-
->withAddedMessage(
57-
MessagePrompt::system(
58-
prompt: <<<'PROMPT'
42+
return $next(
43+
input: $input->withPrompt(
44+
$input
45+
->prompt
46+
->withAddedMessage(
47+
MessagePrompt::system(
48+
prompt: <<<'PROMPT'
5949
There are agents {linked_agents} associated with you. You can ask them for help if you need it.
6050
Use the `ask_agent` tool and provide the agent key.
6151
Always follow rules:
6252
- Don't make up the agent key. Use only the ones from the provided list.
6353
PROMPT,
64-
),
65-
)
66-
->withValues(
67-
values: [
68-
'linked_agents' => \implode(
69-
PHP_EOL,
70-
\array_map(
71-
static fn(array $agent): string => \json_encode([
72-
'key' => $agent['agent']->getKey(),
73-
'description' => $agent['agent']->getDescription(),
74-
'output_schema' => $agent['output_schema'],
75-
]),
76-
$associatedAgents,
77-
),
7854
),
79-
],
80-
),
55+
)
56+
->withValues(
57+
values: [
58+
'linked_agents' => \implode(
59+
PHP_EOL,
60+
\array_map(
61+
static fn(array $agent): string => \json_encode([
62+
'key' => $agent['agent']->getKey(),
63+
'description' => $agent['agent']->getDescription(),
64+
'output_schema' => $agent['output_schema'],
65+
]),
66+
$associatedAgents,
67+
),
68+
),
69+
],
70+
),
71+
),
8172
);
8273
}
8374
}

src/Interceptors/SessionContextInjector.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/Interceptors/UserPromptInjector.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,29 @@
44

55
namespace LLM\Agents\PromptGenerator\Interceptors;
66

7-
use LLM\Agents\Agent\AgentInterface;
8-
use LLM\Agents\LLM\AgentPromptGeneratorInterface;
97
use LLM\Agents\LLM\Prompt\Chat\ChatMessage;
108
use LLM\Agents\LLM\Prompt\Chat\Prompt;
119
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
1210
use LLM\Agents\LLM\Prompt\Chat\Role;
13-
use LLM\Agents\LLM\PromptContextInterface;
11+
use LLM\Agents\PromptGenerator\InterceptorHandler;
12+
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
1413
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
1514

1615
final class UserPromptInjector implements PromptInterceptorInterface
1716
{
1817
public function generate(
19-
AgentInterface $agent,
20-
\Stringable|string $userPrompt,
21-
PromptInterface $prompt,
22-
PromptContextInterface $context,
23-
AgentPromptGeneratorInterface $next,
18+
PromptGeneratorInput $input,
19+
InterceptorHandler $next,
2420
): PromptInterface {
25-
\assert($prompt instanceof Prompt);
21+
\assert($input->prompt instanceof Prompt);
2622

27-
return $next->generate(
28-
agent: $agent,
29-
userPrompt: $userPrompt,
30-
context: $context,
31-
prompt: $prompt->withAddedMessage(
32-
new ChatMessage(
33-
content: (string) $userPrompt,
34-
role: Role::User,
23+
return $next(
24+
input: $input->withPrompt(
25+
$input->prompt->withAddedMessage(
26+
new ChatMessage(
27+
content: (string) $input->userPrompt,
28+
role: Role::User,
29+
),
3530
),
3631
),
3732
);

0 commit comments

Comments
 (0)