Skip to content

Commit a9777b9

Browse files
feat: Add explicit SSE event names for local v3 streaming
1 parent 9187d2e commit a9777b9

3 files changed

Lines changed: 18 additions & 31 deletions

File tree

.stats.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 8
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-975ca868b31b1e45fb00b31a53d9df1ceec8663f6c2851bf40fdaa84171afadc.yml
3-
openapi_spec_hash: 37891379e0f47e5c69769fbaa1064dab
4-
config_hash: 0209737a4ab2a71afececb0ff7459998
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-a4a5ea048bb50d06460d81d6828b53b12b19e9224121ee6338dcd1f0781e22a1.yml
3+
openapi_spec_hash: 9b81c0ae04576318d13d7a80d4ab7b5a
4+
config_hash: d6c6f623d03971bdba921650e5eb7e5f

src/SSEStream.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,9 @@ private function parsedGenerator(): \Generator
2929
return;
3030
}
3131

32-
$done = false;
3332
foreach ($this->stream as $row) {
34-
// @phpstan-ignore if.alwaysFalse
35-
if ($done) {
36-
// Iterate through the whole stream
37-
continue;
38-
}
39-
4033
switch ($row['event'] ?? null) {
41-
case null:
42-
if ($data = $row['data'] ?? '') {
43-
$decoded = Util::decodeJson($data);
44-
45-
yield Conversion::coerce($this->convert, value: $decoded);
46-
}
47-
48-
break;
49-
}
50-
51-
if ($data = $row['data'] ?? '') {
52-
if (str_starts_with($data, needle: '{"data":{"status":"finished"')) {
53-
$done = true;
54-
55-
continue;
56-
}
57-
58-
if (str_starts_with($data, needle: 'error')) {
34+
case 'error':
5935
if ($data = $row['data'] ?? '') {
6036
$json = Util::decodeJson($data);
6137
$message = Util::prettyEncodeJson($json);
@@ -69,8 +45,19 @@ private function parsedGenerator(): \Generator
6945
throw $exn;
7046
}
7147

72-
continue;
73-
}
48+
break;
49+
50+
case 'starting':
51+
case 'connected':
52+
case 'running':
53+
case 'finished':
54+
if ($data = $row['data'] ?? '') {
55+
$decoded = Util::decodeJson($data);
56+
57+
yield Conversion::coerce($this->convert, value: $decoded);
58+
}
59+
60+
break;
7461
}
7562
}
7663
}

src/Sessions/StreamEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Stagehand\Sessions\StreamEvent\Type;
1313

1414
/**
15-
* Server-Sent Event emitted during streaming responses. Events are sent as `data: <JSON>\n\n`. Key order: data (with status first), type, id.
15+
* Server-Sent Event emitted during streaming responses. Events are sent as `event: <status>\ndata: <JSON>\n\n`, where the JSON payload has the shape `{ data, type, id }`.
1616
*
1717
* @phpstan-import-type DataVariants from \Stagehand\Sessions\StreamEvent\Data
1818
* @phpstan-import-type DataShape from \Stagehand\Sessions\StreamEvent\Data

0 commit comments

Comments
 (0)