Skip to content

Commit 3ac1ac9

Browse files
committed
feature #509 [Agent] Add getName() method to AgentInterface (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- [Agent] Add `getName()` method to `AgentInterface` | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | Follows #19 (comment) and #19 (comment) | License | MIT shall we make it non-nullable an return a uuid if not provided or do you want to make it real mandatory? `@chr`-hertel Commits ------- bb1ef23 [Agent] Add `getName()` method to `AgentInterface`
2 parents 4aeca02 + bb1ef23 commit 3ac1ac9

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/agent/src/Agent.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
private Model $model,
4949
iterable $inputProcessors = [],
5050
iterable $outputProcessors = [],
51+
private string $name = 'agent',
5152
private LoggerInterface $logger = new NullLogger(),
5253
) {
5354
$this->inputProcessors = $this->initializeProcessors($inputProcessors, InputProcessorInterface::class);
@@ -59,6 +60,11 @@ public function getModel(): Model
5960
return $this->model;
6061
}
6162

63+
public function getName(): string
64+
{
65+
return $this->name;
66+
}
67+
6268
/**
6369
* @param array<string, mixed> $options
6470
*

src/agent/src/AgentInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ interface AgentInterface
2626
* @throws ExceptionInterface When the agent encounters an error (e.g., unsupported model capabilities, invalid arguments, network failures, or processor errors)
2727
*/
2828
public function call(MessageBag $messages, array $options = []): ResultInterface;
29+
30+
/**
31+
* Get the agent's name, which can be used for debugging or multi-agent configuration.
32+
*/
33+
public function getName(): string;
2934
}

src/agent/src/MockAgent.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ final class MockAgent implements AgentInterface
4646
/**
4747
* @param array<string, string|MockResponse|\Closure> $responses Predefined responses for specific inputs
4848
*/
49-
public function __construct(array $responses = [])
50-
{
49+
public function __construct(
50+
array $responses = [],
51+
private string $name = 'mock',
52+
) {
5153
$this->responses = $responses;
5254
}
5355

@@ -241,4 +243,9 @@ public function reset(): self
241243

242244
return $this;
243245
}
246+
247+
public function getName(): string
248+
{
249+
return $this->name;
250+
}
244251
}

src/agent/tests/AgentTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function getResponse(): HttpResponseInterface
307307
$this->expectException(InvalidArgumentException::class);
308308
$this->expectExceptionMessage('Client error');
309309

310-
$agent = new Agent($platform, $model, [], [], $logger);
310+
$agent = new Agent($platform, $model, logger: $logger);
311311
$agent->call($messages);
312312
}
313313

@@ -405,4 +405,25 @@ public function testConstructorAcceptsTraversableProcessors()
405405

406406
$this->assertInstanceOf(AgentInterface::class, $agent);
407407
}
408+
409+
public function testGetNameReturnsDefaultName()
410+
{
411+
$platform = $this->createMock(PlatformInterface::class);
412+
$model = $this->createMock(Model::class);
413+
414+
$agent = new Agent($platform, $model);
415+
416+
$this->assertSame('agent', $agent->getName());
417+
}
418+
419+
public function testGetNameReturnsProvidedName()
420+
{
421+
$platform = $this->createMock(PlatformInterface::class);
422+
$model = $this->createMock(Model::class);
423+
$name = 'test';
424+
425+
$agent = new Agent($platform, $model, [], [], $name);
426+
427+
$this->assertSame($name, $agent->getName());
428+
}
408429
}

src/agent/tests/MockAgentTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,18 @@ public function testCallableTrackingInCalls()
410410
$this->assertSame('tracked', $calls[0]['input']);
411411
$this->assertSame('Tracked: tracked', $calls[0]['response']);
412412
}
413+
414+
public function testGetNameReturnsDefaultName()
415+
{
416+
$agent = new MockAgent();
417+
418+
$this->assertSame('mock', $agent->getName());
419+
}
420+
421+
public function testGetNameReturnsProvidedName()
422+
{
423+
$agent = new MockAgent(name: 'custom');
424+
425+
$this->assertSame('custom', $agent->getName());
426+
}
413427
}

0 commit comments

Comments
 (0)