Skip to content

Commit 9705432

Browse files
committed
wip
1 parent a29b75a commit 9705432

File tree

10 files changed

+29
-20
lines changed

10 files changed

+29
-20
lines changed

src/AGUI/Contracts/Event.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function withRawEvent(mixed $rawEvent): static;
3636

3737
/**
3838
* Convert the event to an array.
39+
*
40+
* @return array<string, mixed>
3941
*/
4042
public function toArray(): array;
4143
}

src/AGUI/Events/AbstractEvent.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class AbstractEvent implements Event
1313
{
1414
public protected(set) EventType $type;
1515

16-
public protected(set) ?DateTimeImmutable $timestamp;
16+
public protected(set) ?DateTimeImmutable $timestamp = null;
1717

1818
public protected(set) mixed $rawEvent;
1919

@@ -36,6 +36,11 @@ protected function formattedTimestamp(): ?string
3636
return $this->timestamp?->format(DateTimeInterface::ATOM);
3737
}
3838

39+
/**
40+
* @param array<string, mixed> $additional
41+
*
42+
* @return array<string, mixed>
43+
*/
3944
protected function buildArray(array $additional = []): array
4045
{
4146
$payload = [
@@ -47,6 +52,6 @@ protected function buildArray(array $additional = []): array
4752
$payload['timestamp'] = $this->formattedTimestamp();
4853
}
4954

50-
return $payload;
55+
return array_filter($payload, fn(mixed $value): bool => $value !== null);
5156
}
5257
}

src/AGUI/Events/Custom.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Cortex\AGUI\Events;
66

7-
use DateTimeImmutable;
87
use Cortex\AGUI\Enums\EventType;
98
use Illuminate\Contracts\Support\Arrayable;
109

src/AGUI/Events/ReasoningMessageStart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @implements Arrayable<string, mixed>
1212
*/
13-
final class ReasoningMessageStart extends AbstractEvent
13+
final class ReasoningMessageStart extends AbstractEvent implements Arrayable
1414
{
1515
public EventType $type = EventType::ReasoningMessageStart;
1616

src/Http/Controllers/AgentsController.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Cortex\Events\AgentStepStart;
1515
use Illuminate\Http\JsonResponse;
1616
use Illuminate\Routing\Controller;
17+
use Cortex\LLM\Enums\StreamingProtocol;
1718
use Cortex\LLM\Data\Messages\UserMessage;
19+
use Symfony\Component\HttpFoundation\StreamedResponse;
1820

1921
class AgentsController extends Controller
2022
{
@@ -77,7 +79,7 @@ public function invoke(string $agent, Request $request): JsonResponse
7779
]);
7880
}
7981

80-
public function stream(string $agent, Request $request)//: void// : StreamedResponse
82+
public function stream(string $agent, Request $request): StreamedResponse
8183
{
8284
$agent = Cortex::agent($agent);
8385

@@ -120,16 +122,16 @@ public function stream(string $agent, Request $request)//: void// : StreamedResp
120122
// // dump($chunk->toArray());
121123
// }
122124

123-
return $result->agUiStreamResponse();
125+
return $result->streamResponse(StreamingProtocol::AGUI);
124126
} catch (Throwable $e) {
125127
dd($e);
126128
}
127129

128-
dd([
129-
'total_usage' => $agent->getTotalUsage()->toArray(),
130-
'steps' => $agent->getSteps()->toArray(),
131-
'parsed_output' => $agent->getParsedOutput(),
132-
'memory' => $agent->getMemory()->getMessages()->toArray(),
133-
]);
130+
// dd([
131+
// 'total_usage' => $agent->getTotalUsage()->toArray(),
132+
// 'steps' => $agent->getSteps()->toArray(),
133+
// 'parsed_output' => $agent->getParsedOutput(),
134+
// 'memory' => $agent->getMemory()->getMessages()->toArray(),
135+
// ]);
134136
}
135137
}

src/LLM/AbstractLLM.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Cortex\LLM\Contracts\Tool;
1717
use Cortex\Events\ChatModelEnd;
1818
use Cortex\LLM\Data\ToolConfig;
19-
use Cortex\LLM\Enums\ChunkType;
2019
use Cortex\LLM\Enums\ToolChoice;
2120
use Cortex\Events\ChatModelError;
2221
use Cortex\Events\ChatModelStart;

src/LLM/Drivers/Anthropic/Concerns/MapStreamResponse.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Cortex\SDK\Anthropic\Data\Messages\Streaming\ThinkingDelta;
2727
use Cortex\SDK\Anthropic\Data\Messages\Streaming\InputJsonDelta;
2828
use Cortex\SDK\Anthropic\Data\Messages\Streaming\SignatureDelta;
29-
use Cortex\SDK\Anthropic\Data\Messages\Streaming\Events\MessageStop;
3029
use Cortex\SDK\Anthropic\Data\Messages\Streaming\Events\MessageDelta;
3130
use Cortex\SDK\Anthropic\Data\Messages\Streaming\Events\MessageStart;
3231
use Cortex\SDK\Anthropic\Data\Messages\ContentBlocks\TextContentBlock;

src/LLM/Enums/ChunkType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ enum ChunkType: string
7272

7373
case ChatModelEnd = 'chat_model_end';
7474

75+
case OutputParserStart = 'output_parser_start';
76+
77+
case OutputParserEnd = 'output_parser_end';
78+
79+
case OutputParserError = 'output_parser_error';
80+
7581
public function isText(): bool
7682
{
7783
return match ($this) {

src/LLM/Streaming/AgUiDataStream.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
use Cortex\AGUI\Events\TextMessageStart;
2727
use Cortex\LLM\Data\ChatGenerationChunk;
2828
use Cortex\AGUI\Events\TextMessageContent;
29-
use Cortex\LLM\Contracts\StreamingProtocolDriver;
3029
use Cortex\AGUI\Contracts\Event as AgUiEvent;
3130
use Cortex\AGUI\Events\ReasoningMessageContent;
31+
use Cortex\LLM\Contracts\StreamingProtocolDriver;
3232

3333
/**
3434
* AG-UI (Agent User Interaction Protocol) streaming implementation.
@@ -76,13 +76,13 @@ protected function mapChunkToEvent(ChatGenerationChunk $chunk): AgUiEvent
7676

7777
return match ($chunk->type) {
7878
ChunkType::RunStart => new RunStarted(
79-
runId: $this->runId = $chunk->metadata['run_id'],
8079
threadId: $this->threadId = $chunk->metadata['thread_id'],
80+
runId: $this->runId = $chunk->metadata['run_id'],
8181
),
8282

8383
ChunkType::RunEnd => new RunFinished(
84-
runId: $this->runId,
8584
threadId: $this->threadId,
85+
runId: $this->runId,
8686
),
8787

8888
ChunkType::TextStart => new TextMessageStart(
@@ -162,8 +162,6 @@ protected function mapChunkToEvent(ChatGenerationChunk $chunk): AgUiEvent
162162

163163
/**
164164
* Send a single event to the output stream.
165-
*
166-
* @param \Cortex\AGUI\Contracts\Event $event
167165
*/
168166
protected function sendEvent(AgUiEvent $event): void
169167
{

tests/Unit/LLM/Streaming/AgUiDataStreamTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
declare(strict_types=1);
44

55
namespace Tests\Unit\LLM\Streaming;
6-

0 commit comments

Comments
 (0)