Follow Discord
Sweep 22 Oct 2026 · 21:04Z Build v2.1.309 528 read Stable v2.1.308 Latest v2.1.309 Next v2.1.309 Feeds RSS JSON llms.txt llms-full.txt Unofficial
One change · api

event-deltas changedmanaged-agents/event-deltas

Recorded here
Lines+127added
Lines−8removed
From line 491 where the diff opens
First seen 8 Oct 2026 this site's first read of the page
Recorded edits15to this page, all time

The whole hunk

from line 491, old and new numbered
/
lines
from line 491
491491 ```
492492 
493493 ```php PHP
494 // In PHP, set eventDeltas on EventStreamParams and accumulate with Anthropic\Lib\Sessions\EventAccumulator.
494 use Anthropic\Beta\Sessions\BetaManagedAgentsDeltaEvent;
495 use Anthropic\Beta\Sessions\BetaManagedAgentsDeltaType;
496 use Anthropic\Beta\Sessions\BetaManagedAgentsStartEvent;
497 use Anthropic\Beta\Sessions\Events\ManagedAgentsAgentMessageEvent;
498 use Anthropic\Beta\Sessions\Events\ManagedAgentsSessionStatusIdleEvent;
499 use Anthropic\Beta\Sessions\Events\ManagedAgentsSpanModelRequestEndEvent;
500 use Anthropic\Beta\Sessions\Events\ManagedAgentsTextBlock;
501 use Anthropic\Lib\Sessions\EventAccumulator;
502 
503 // Preview snapshots, keyed by event id. EventAccumulator::accumulate() adds each
504 // event_start / event_delta to an agent.message snapshot; the buffered
505 // agent.message replaces it.
506 $previews = [];
507 
508 // The text of an agent.message snapshot or event
509 $textOf = fn (ManagedAgentsAgentMessageEvent $message): string => implode(
510 '',
511 array_map(
512 fn ($block) => $block->text,
513 array_filter(
514 $message->content,
515 fn ($block) => $block instanceof ManagedAgentsTextBlock,
516 ),
517 ),
518 );
519 
520 // Opt in to agent.message previews on this connection
521 $stream = $client->beta->sessions->events->streamStream(
522 $session->id,
523 eventDeltas: [BetaManagedAgentsDeltaType::AGENT_MESSAGE],
524 );
525 
526 $client->beta->sessions->events->send(
527 $session->id,
528 events: [
529 [
530 'type' => 'user.message',
531 'content' => [['type' => 'text', 'text' => 'Describe the repo in one sentence.']],
532 ],
533 ],
534 );
535 
536 foreach ($stream as $event) {
537 switch (true) {
538 case $event instanceof BetaManagedAgentsStartEvent:
539 $snapshot = EventAccumulator::accumulate(null, $event);
540 if ($snapshot !== null) {
541 $previews[$event->event->id] = $snapshot;
542 }
543 printf("event_start %s %s\n", $event->event->type, $event->event->id);
544 break;
545 case $event instanceof BetaManagedAgentsDeltaEvent:
546 // Skip a late delta for a preview that was already replaced or closed
547 if (!isset($previews[$event->eventID])) {
548 break; // Break switch statement
549 }
550 $previews[$event->eventID] = EventAccumulator::accumulate($previews[$event->eventID], $event);
551 printf("event_delta preview: %s\n", json_encode($textOf($previews[$event->eventID])));
552 break;
553 case $event instanceof ManagedAgentsAgentMessageEvent:
554 // The buffered event is the record: it replaces and closes the preview
555 unset($previews[$event->id]);
556 printf("agent.message %s %s\n", $event->id, json_encode($textOf($event)));
557 break;
558 case $event instanceof ManagedAgentsSpanModelRequestEndEvent:
559 // No more deltas are coming. Close any preview whose
560 // buffered event never arrived.
561 foreach (array_keys($previews) as $eventID) {
562 printf("span.model_request_end closing preview for %s\n", $eventID);
563 }
564 $previews = [];
565 break;
566 case $event instanceof ManagedAgentsSessionStatusIdleEvent:
567 break 2; // Break foreach loop
568 }
569 }
570 $stream->close();
495571 ```
496572 
497573 ```ruby Ruby
from line 712
636712 ```
637713 
638714 ```python Python
639 # List the session's threads and pick a child: child threads carry a non-null
715 # List the session's threads and pick a child: child threads have a non-null
640716 # parent_thread_id, and the primary thread's parent_thread_id is null.
641717 child_thread = next(
642718 thread
from line 743
667743 ```
668744 
669745 ```typescript TypeScript
670 // List the session's threads and pick a child: child threads carry a non-null
746 // List the session's threads and pick a child: child threads have a non-null
671747 // parent_thread_id, and the primary thread's parent_thread_id is null.
672748 let childThreadId: string | undefined;
673749 for await (const thread of client.beta.sessions.threads.list(session.id)) {
from line 783
707783 ```
708784 
709785 ```csharp C#
710 // List the session's threads and pick a child: child threads carry a non-null
786 // List the session's threads and pick a child: child threads have a non-null
711787 // parent_thread_id, and the primary thread's parent_thread_id is null.
712788 var threads = await client.Beta.Sessions.Threads.List(session.ID);
713789 var childThread = threads.Items.First(thread => thread.ParentThreadID is not null);
from line 817
741817 ```
742818 
743819 ```go Go
744 // List the session's threads and pick a child: child threads carry a non-null
820 // List the session's threads and pick a child: child threads have a non-null
745821 // parent_thread_id, and the primary thread's parent_thread_id is null.
746822 var childThreadID string
747823 threads := client.Beta.Sessions.Threads.ListAutoPaging(ctx, session.ID, anthropic.BetaSessionThreadListParams{})
from line 864
788864 ```
789865 
790866 ```java Java
791 // List the session's threads and pick a child: child threads carry a non-null
867 // List the session's threads and pick a child: child threads have a non-null
792868 // parent_thread_id, and the primary thread's parent_thread_id is null.
793869 var childThread = client.beta().sessions().threads().list(session.id()).autoPager().stream()
794870 .filter(thread -> thread.parentThreadId().isPresent())
from line 900
824900 ```
825901 
826902 ```php PHP
827 // In PHP, set eventDeltas on the thread EventStreamParams and accumulate with Anthropic\Lib\Sessions\EventAccumulator.
903 use Anthropic\Beta\Sessions\BetaManagedAgentsDeltaEvent;
904 use Anthropic\Beta\Sessions\BetaManagedAgentsDeltaType;
905 use Anthropic\Beta\Sessions\Events\ManagedAgentsAgentMessageEvent;
906 use Anthropic\Beta\Sessions\Events\ManagedAgentsSessionThreadStatusIdleEvent;
907 use Anthropic\Beta\Sessions\Events\ManagedAgentsTextBlock;
908 
909 // List the session's threads and pick a child: child threads have a non-null
910 // parentThreadID, and the primary thread's parentThreadID is null.
911 $childThread = null;
912 foreach ($client->beta->sessions->threads->list($session->id)->pagingEachItem() as $thread) {
913 if ($thread->parentThreadID !== null) {
914 $childThread = $thread;
915 break;
916 }
917 }
918 
919 // The child thread's stream takes the same eventDeltas parameter as the
920 // session stream.
921 $stream = $client->beta->sessions->threads->events->streamStream(
922 $childThread->id,
923 sessionID: $session->id,
924 eventDeltas: [BetaManagedAgentsDeltaType::AGENT_MESSAGE],
925 );
926 
927 foreach ($stream as $event) {
928 switch (true) {
929 case $event instanceof BetaManagedAgentsDeltaEvent:
930 echo $event->delta->content->text;
931 break;
932 case $event instanceof ManagedAgentsAgentMessageEvent:
933 // The buffered event is the authoritative record; render its content
934 echo "\n";
935 foreach ($event->content as $block) {
936 if ($block instanceof ManagedAgentsTextBlock) {
937 echo $block->text;
938 }
939 }
940 echo "\n";
941 break;
942 case $event instanceof ManagedAgentsSessionThreadStatusIdleEvent:
943 break 2; // Break foreach loop
944 }
945 }
946 $stream->close();
828947 ```
829948 
830949 ```ruby Ruby
831 # List the session's threads and pick a child: child threads carry a non-null
950 # List the session's threads and pick a child: child threads have a non-null
832951 # parent_thread_id, and the primary thread's parent_thread_id is null.
833952 child_thread = client.beta.sessions.threads.list(session.id).to_enum.find { it.parent_thread_id }
834953 
Feedback