Skip to content

Commit 79b604c

Browse files
a2a-php - change: fix all remaining a2a-tck and compser tests
1 parent 3ab42e6 commit 79b604c

File tree

5 files changed

+66
-26
lines changed

5 files changed

+66
-26
lines changed

phpunit.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
bootstrap="vendor/autoload.php"
55
colors="true"
66
testdox="true">
7+
<php>
8+
<env name="XDEBUG_MODE" value="coverage"/>
9+
</php>
710
<testsuites>
811
<testsuite name="A2A PHP SDK Test Suite">
912
<directory>tests</directory>
@@ -14,9 +17,4 @@
1417
<directory>src</directory>
1518
</include>
1619
</source>
17-
<coverage>
18-
<report>
19-
<html outputDirectory="coverage"/>
20-
</report>
21-
</coverage>
2220
</phpunit>

src/A2AProtocol_v030.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ public function handleRequest(array $request): array
168168
case 'ping':
169169
return $jsonRpc->createResponse($parsedRequest['id'], ['status' => 'pong']);
170170
default:
171-
$this->logger->warning('Method not found', ['method' => $parsedRequest['method']]);
171+
$this->logger->warning('Unknown method requested', ['method' => $parsedRequest['method']]);
172172
return $jsonRpc->createError(
173173
$parsedRequest['id'],
174-
A2AErrorCodes::getErrorMessage(A2AErrorCodes::METHOD_NOT_FOUND),
174+
'Unknown method: ' . $parsedRequest['method'],
175175
A2AErrorCodes::METHOD_NOT_FOUND
176176
);
177177
}
@@ -559,37 +559,53 @@ private function handleCancelTask(array $params, $requestId): array
559559

560560
private function handleResubscribeTask(array $params, $requestId): array
561561
{
562+
$jsonRpc = new JsonRpc();
562563
$taskId = $params['id'] ?? null;
563564

564565
if (!$taskId) {
565566
$this->streamingServer->streamResubscribeError(
566-
$requestId,
567+
(string) ($requestId ?? ''),
567568
A2AErrorCodes::INVALID_PARAMS,
568569
'Task ID is required for resubscription'
569570
);
570571

571-
return [];
572+
return $jsonRpc->createError(
573+
$requestId,
574+
'Task ID is required for resubscription',
575+
A2AErrorCodes::INVALID_PARAMS
576+
);
572577
}
573578

574579
$task = $this->taskManager->getTask($taskId);
575580
if (!$task) {
576581
$this->streamingServer->streamResubscribeError(
577-
$requestId,
582+
(string) ($requestId ?? ''),
578583
A2AErrorCodes::TASK_NOT_FOUND,
579584
'Task not found',
580585
$taskId
581586
);
582587

583-
return [];
588+
return $jsonRpc->createError(
589+
$requestId,
590+
'Task not found',
591+
A2AErrorCodes::TASK_NOT_FOUND
592+
);
584593
}
585594

586595
$this->streamingServer->streamResubscribeSnapshot(
587-
$requestId,
596+
(string) ($requestId ?? ''),
588597
$taskId,
589598
$task->toArray()
590599
);
591600

592-
return [];
601+
return $jsonRpc->createResponse(
602+
$requestId,
603+
[
604+
'status' => 'resubscribed',
605+
'taskId' => $taskId,
606+
'task' => $task->toArray()
607+
]
608+
);
593609
}
594610

595611
private function handleMessageStream(array $parsedRequest, array $rawRequest): array

src/Models/Part.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ class Part
1111
{
1212
public static function fromArray(array $data): PartInterface
1313
{
14-
if (!isset($data['kind'])) {
15-
if (isset($data['type'])) {
16-
$data['kind'] = $data['type'];
17-
} else {
18-
// For backward compatibility, if 'kind' is missing, assume it's a TextPart
19-
// and the content is in a 'content' or 'text' field.
20-
$content = $data['content'] ?? $data['text'] ?? '';
21-
return new TextPart($content, $data['metadata'] ?? null);
14+
$kind = $data['kind'] ?? $data['type'] ?? null;
15+
16+
if ($kind === null) {
17+
// For backward compatibility, if neither 'kind' nor 'type' is provided,
18+
// assume a text part and attempt to map common content keys.
19+
$content = $data['content'] ?? $data['text'] ?? '';
20+
if (!is_string($content)) {
21+
$content = is_scalar($content) ? (string) $content : '';
2222
}
23+
return new TextPart($content, $data['metadata'] ?? null);
2324
}
2425

25-
switch ($data['kind']) {
26+
switch ($kind) {
2627
case 'text':
27-
return new TextPart($data['text'], $data['metadata'] ?? null);
28+
$text = $data['text'] ?? $data['content'] ?? '';
29+
if (!is_string($text)) {
30+
$text = is_scalar($text) ? (string) $text : '';
31+
}
32+
return new TextPart($text, $data['metadata'] ?? null);
2833
case 'file':
2934
$fileData = $data['file'];
3035
$uri = $fileData['uri'] ?? $fileData['url'] ?? $fileData['href'] ?? null;
@@ -40,7 +45,7 @@ public static function fromArray(array $data): PartInterface
4045
case 'data':
4146
return new DataPart($data['data'], $data['metadata'] ?? null);
4247
default:
43-
throw new \InvalidArgumentException("Unknown part kind: {$data['kind']}");
48+
throw new \InvalidArgumentException("Unknown part kind: {$kind}");
4449
}
4550
}
4651
}

src/Streaming/SSEStreamer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66

77
class SSEStreamer
88
{
9+
private function isCliEnvironment(): bool
10+
{
11+
return PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg';
12+
}
13+
914
public function sendEvent(string $data, ?string $event = null, ?string $id = null): void
1015
{
16+
if ($this->isCliEnvironment()) {
17+
return;
18+
}
19+
1120
if ($id !== null) {
1221
echo "id: $id\n";
1322
}
@@ -26,6 +35,10 @@ public function sendEvent(string $data, ?string $event = null, ?string $id = nul
2635

2736
public function startStream(): void
2837
{
38+
if ($this->isCliEnvironment()) {
39+
return;
40+
}
41+
2942
header('Content-Type: text/event-stream');
3043
header('Cache-Control: no-cache');
3144
header('Connection: keep-alive');
@@ -37,6 +50,10 @@ public function startStream(): void
3750

3851
public function endStream(): void
3952
{
53+
if ($this->isCliEnvironment()) {
54+
return;
55+
}
56+
4057
echo "event: close\ndata: \n\n";
4158
if (ob_get_level()) {
4259
ob_flush();

src/Utils/JsonRpc.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public function parseRequest(array $request): array
5252
throw new InvalidRequestException('Invalid JSON-RPC version');
5353
}
5454

55-
if (!isset($request['method']) || !is_string($request['method']) || $request['method'] === '') {
56-
throw new InvalidRequestException('Missing or invalid method');
55+
if (!array_key_exists('method', $request)) {
56+
throw new InvalidRequestException('Missing method');
57+
}
58+
59+
if (!is_string($request['method']) || trim($request['method']) === '') {
60+
throw new InvalidRequestException('Invalid method');
5761
}
5862

5963
if (array_key_exists('id', $request) && $request['id'] !== null && !is_string($request['id']) && !is_int($request['id'])) {

0 commit comments

Comments
 (0)