44
55namespace LLM \Agents \PromptGenerator \Interceptors ;
66
7- use LLM \Agents \Agent \AgentInterface ;
87use LLM \Agents \Agent \AgentRepositoryInterface ;
9- use LLM \Agents \LLM \AgentPromptGeneratorInterface ;
108use LLM \Agents \LLM \Prompt \Chat \MessagePrompt ;
119use LLM \Agents \LLM \Prompt \Chat \Prompt ;
1210use LLM \Agents \LLM \Prompt \Chat \PromptInterface ;
13- use LLM \Agents \LLM \PromptContextInterface ;
11+ use LLM \Agents \PromptGenerator \InterceptorHandler ;
12+ use LLM \Agents \PromptGenerator \PromptGeneratorInput ;
1413use LLM \Agents \PromptGenerator \PromptInterceptorInterface ;
1514use LLM \Agents \Solution \AgentLink ;
1615use 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'
5949There are agents {linked_agents} associated with you. You can ask them for help if you need it.
6050Use the `ask_agent` tool and provide the agent key.
6151Always follow rules:
6252- Don't make up the agent key. Use only the ones from the provided list.
6353PROMPT,
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}
0 commit comments