Skip to content

Commit e107e93

Browse files
Add PHPDoc to all classes and public methods
Document all contracts, value objects, middleware, exceptions, agent builder with @param, @return, and @throws annotations.
1 parent 6abd3a5 commit e107e93

43 files changed

Lines changed: 777 additions & 60 deletions

Some content is hidden

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

src/Agent.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
use PapiAI\Core\Contracts\ToolInterface;
2323
use PapiAI\Core\Schema\Schema;
2424

25+
/**
26+
* Core agent implementation that orchestrates LLM interactions and tool execution.
27+
*
28+
* Manages the agentic loop: sending prompts to the provider, executing tool calls,
29+
* feeding results back, and repeating until the model produces a final response
30+
* or the maximum number of turns is reached. Supports middleware pipelines,
31+
* event hooks, structured output, and streaming.
32+
*
33+
* Create instances via the fluent builder: Agent::build()->provider($p)->model('...')->create()
34+
*/
2535
final class Agent implements AgentInterface
2636
{
2737
/** @var array<string, ToolInterface> */
@@ -64,26 +74,34 @@ public function __construct(
6474

6575
/**
6676
* Create a fluent builder for constructing an Agent.
77+
*
78+
* @return AgentBuilder A new builder instance
6779
*/
6880
public static function build(): AgentBuilder
6981
{
7082
return new AgentBuilder();
7183
}
7284

85+
/** {@inheritDoc} */
7386
public function addTool(ToolInterface $tool): self
7487
{
7588
$this->tools[$tool->getName()] = $tool;
7689

7790
return $this;
7891
}
7992

93+
/** {@inheritDoc} */
8094
public function getProvider(): ProviderInterface
8195
{
8296
return $this->provider;
8397
}
8498

8599
/**
86-
* Add middleware to the pipeline.
100+
* Add middleware to the agent's request/response pipeline.
101+
*
102+
* @param MiddlewareInterface $middleware The middleware to add
103+
*
104+
* @return self For method chaining
87105
*/
88106
public function addMiddleware(MiddlewareInterface $middleware): self
89107
{
@@ -93,14 +111,21 @@ public function addMiddleware(MiddlewareInterface $middleware): self
93111
}
94112

95113
/**
96-
* Run the agent with a prompt.
114+
* Run the agent with a prompt, executing the full agentic loop.
115+
*
116+
* Sends the prompt through any middleware, then iterates: call the LLM,
117+
* execute tool calls, feed results back, until a final response or max turns.
97118
*
98119
* @param string $prompt The user prompt
99120
* @param array{
100121
* outputSchema?: Schema,
101122
* context?: mixed,
102123
* maxTurns?: int,
103124
* } $options Run options
125+
*
126+
* @return Response The final agent response
127+
*
128+
* @throws InvalidArgumentException If max turns is exceeded or structured output parsing fails
104129
*/
105130
public function run(string $prompt, array $options = []): Response
106131
{
@@ -171,7 +196,9 @@ private function executeRun(string $prompt, array $options = []): Response
171196
}
172197

173198
/**
174-
* Stream the agent response as text chunks.
199+
* {@inheritDoc}
200+
*
201+
* Streams text chunks from the provider without executing the agentic tool loop.
175202
*/
176203
public function stream(string $prompt, array $options = []): iterable
177204
{
@@ -188,7 +215,9 @@ public function stream(string $prompt, array $options = []): iterable
188215
}
189216

190217
/**
191-
* Stream the agent response with detailed events.
218+
* {@inheritDoc}
219+
*
220+
* Streams detailed events including text, tool calls, tool results, and completion.
192221
*/
193222
public function streamEvents(string $prompt, array $options = []): iterable
194223
{

src/AgentBuilder.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,55 @@ final class AgentBuilder
4848
/** @var array<MiddlewareInterface> */
4949
private array $middleware = [];
5050

51+
/**
52+
* Set the LLM provider for the agent.
53+
*
54+
* @param ProviderInterface $provider The provider to use
55+
*
56+
* @return self For method chaining
57+
*/
5158
public function provider(ProviderInterface $provider): self
5259
{
5360
$this->provider = $provider;
5461

5562
return $this;
5663
}
5764

65+
/**
66+
* Set the model identifier (e.g., 'gpt-4o', 'claude-sonnet-4-20250514').
67+
*
68+
* @param string $model The model name
69+
*
70+
* @return self For method chaining
71+
*/
5872
public function model(string $model): self
5973
{
6074
$this->model = $model;
6175

6276
return $this;
6377
}
6478

79+
/**
80+
* Set the system instructions (system prompt) for the agent.
81+
*
82+
* @param string $instructions The system prompt text
83+
*
84+
* @return self For method chaining
85+
*/
6586
public function instructions(string $instructions): self
6687
{
6788
$this->instructions = $instructions;
6889

6990
return $this;
7091
}
7192

93+
/**
94+
* Add a single tool to the agent.
95+
*
96+
* @param ToolInterface $tool The tool to register
97+
*
98+
* @return self For method chaining
99+
*/
72100
public function tool(ToolInterface $tool): self
73101
{
74102
$this->tools[] = $tool;
@@ -77,7 +105,11 @@ public function tool(ToolInterface $tool): self
77105
}
78106

79107
/**
80-
* @param array<ToolInterface> $tools
108+
* Add multiple tools to the agent at once.
109+
*
110+
* @param array<ToolInterface> $tools The tools to register
111+
*
112+
* @return self For method chaining
81113
*/
82114
public function tools(array $tools): self
83115
{
@@ -86,34 +118,70 @@ public function tools(array $tools): self
86118
return $this;
87119
}
88120

121+
/**
122+
* Register an event hook (e.g., 'beforeToolCall', 'afterToolCall', 'onError').
123+
*
124+
* @param string $name The hook name
125+
* @param Closure $callback The callback to invoke when the hook fires
126+
*
127+
* @return self For method chaining
128+
*/
89129
public function hook(string $name, Closure $callback): self
90130
{
91131
$this->hooks[$name] = $callback;
92132

93133
return $this;
94134
}
95135

136+
/**
137+
* Set the maximum number of tokens the model can generate.
138+
*
139+
* @param int $maxTokens Maximum output tokens
140+
*
141+
* @return self For method chaining
142+
*/
96143
public function maxTokens(int $maxTokens): self
97144
{
98145
$this->maxTokens = $maxTokens;
99146

100147
return $this;
101148
}
102149

150+
/**
151+
* Set the sampling temperature (higher = more creative, lower = more deterministic).
152+
*
153+
* @param float $temperature Temperature value (typically 0.0 to 1.0)
154+
*
155+
* @return self For method chaining
156+
*/
103157
public function temperature(float $temperature): self
104158
{
105159
$this->temperature = $temperature;
106160

107161
return $this;
108162
}
109163

164+
/**
165+
* Set the maximum number of agentic turns (tool call loops) before stopping.
166+
*
167+
* @param int $maxTurns Maximum iterations of the agentic loop
168+
*
169+
* @return self For method chaining
170+
*/
110171
public function maxTurns(int $maxTurns): self
111172
{
112173
$this->maxTurns = $maxTurns;
113174

114175
return $this;
115176
}
116177

178+
/**
179+
* Add a single middleware to the pipeline.
180+
*
181+
* @param MiddlewareInterface $middleware The middleware to add
182+
*
183+
* @return self For method chaining
184+
*/
117185
public function addMiddleware(MiddlewareInterface $middleware): self
118186
{
119187
$this->middleware[] = $middleware;
@@ -122,7 +190,11 @@ public function addMiddleware(MiddlewareInterface $middleware): self
122190
}
123191

124192
/**
125-
* @param array<MiddlewareInterface> $middleware
193+
* Add multiple middleware to the pipeline at once.
194+
*
195+
* @param array<MiddlewareInterface> $middleware The middleware to add
196+
*
197+
* @return self For method chaining
126198
*/
127199
public function middleware(array $middleware): self
128200
{
@@ -131,6 +203,13 @@ public function middleware(array $middleware): self
131203
return $this;
132204
}
133205

206+
/**
207+
* Build and return the configured Agent instance.
208+
*
209+
* @return Agent The fully configured agent
210+
*
211+
* @throws InvalidArgumentException If provider or model is not set
212+
*/
134213
public function create(): Agent
135214
{
136215
if ($this->provider === null) {

src/AgentJob.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
*/
2020
final class AgentJob
2121
{
22+
/**
23+
* @param string $agentClass Fully-qualified class name of the agent to run
24+
* @param string $prompt The user prompt for the agent
25+
* @param array<string, mixed> $options Agent run options
26+
* @param string|null $callbackUrl Optional URL to POST results to when the job completes
27+
*/
2228
public function __construct(
2329
public readonly string $agentClass,
2430
public readonly string $prompt,
@@ -27,6 +33,11 @@ public function __construct(
2733
) {
2834
}
2935

36+
/**
37+
* Serialize the job to an array for queue transport.
38+
*
39+
* @return array{agentClass: string, prompt: string, options: array, callbackUrl: string|null}
40+
*/
3041
public function toArray(): array
3142
{
3243
return [
@@ -37,6 +48,13 @@ public function toArray(): array
3748
];
3849
}
3950

51+
/**
52+
* Deserialize a job from a queue-transported array.
53+
*
54+
* @param array{agentClass: string, prompt: string, options?: array, callbackUrl?: string|null} $data Serialised job data
55+
*
56+
* @return self The restored job
57+
*/
4058
public static function fromArray(array $data): self
4159
{
4260
return new self(

src/AgentRequest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
*/
2020
final class AgentRequest
2121
{
22+
/**
23+
* @param string $prompt The user prompt
24+
* @param array<string, mixed> $options Agent run options (e.g., outputSchema, maxTurns)
25+
* @param array<Message> $messages Pre-existing conversation messages
26+
* @param array<string, mixed> $metadata Arbitrary metadata for middleware inspection
27+
*/
2228
public function __construct(
2329
public readonly string $prompt,
2430
public readonly array $options = [],
@@ -27,6 +33,14 @@ public function __construct(
2733
) {
2834
}
2935

36+
/**
37+
* Return a new request with an additional option set.
38+
*
39+
* @param string $key Option key
40+
* @param mixed $value Option value
41+
*
42+
* @return self A new immutable request with the option merged
43+
*/
3044
public function withOption(string $key, mixed $value): self
3145
{
3246
return new self(
@@ -37,6 +51,13 @@ public function withOption(string $key, mixed $value): self
3751
);
3852
}
3953

54+
/**
55+
* Return a new request with the message history replaced.
56+
*
57+
* @param array<Message> $messages The new message list
58+
*
59+
* @return self A new immutable request with the updated messages
60+
*/
4061
public function withMessages(array $messages): self
4162
{
4263
return new self(
@@ -47,6 +68,14 @@ public function withMessages(array $messages): self
4768
);
4869
}
4970

71+
/**
72+
* Return a new request with an additional metadata entry.
73+
*
74+
* @param string $key Metadata key
75+
* @param mixed $value Metadata value
76+
*
77+
* @return self A new immutable request with the metadata merged
78+
*/
5079
public function withMetadata(string $key, mixed $value): self
5180
{
5281
return new self(

src/Attributes/Description.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@
1616

1717
use Attribute;
1818

19+
/**
20+
* Provides a description for a tool parameter or property in the generated JSON schema.
21+
*
22+
* Applied to method parameters alongside the #[Tool] attribute so the LLM
23+
* understands what each parameter represents.
24+
*/
1925
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
2026
class Description
2127
{
28+
/**
29+
* @param string $text Human-readable description of the parameter
30+
*/
2231
public function __construct(
2332
public readonly string $text,
2433
) {

0 commit comments

Comments
 (0)