Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(php-sdk): send changeTracking as a format and tag crawl, batch scrape and map with the SDK origin (#5235)
Erik Engervalld88afcd8bd18
diff --git a/apps/php-sdk/src/Client/FirecrawlClient.php b/apps/php-sdk/src/Client/FirecrawlClient.phpindex 9ec9dec7a..9f62c891b 100644--- a/apps/php-sdk/src/Client/FirecrawlClient.php+++ b/apps/php-sdk/src/Client/FirecrawlClient.php@@ -299,6 +299,8 @@ final class FirecrawlClient$body = array_merge($body, $options->toArray());}+ $body['origin'] ??= 'php-sdk@' . Version::SDK_VERSION;+return CrawlResponse::fromArray($this->http->post('/v2/crawl', $body, $extraHeaders, $requestTimeoutSeconds),);@@ -369,6 +371,8 @@ final class FirecrawlClient$body = array_merge($body, $options->toArray());}+ $body['origin'] ??= 'php-sdk@' . Version::SDK_VERSION;+return BatchScrapeResponse::fromArray($this->http->post('/v2/batch/scrape', $body, $extraHeaders),);@@ -422,6 +426,8 @@ final class FirecrawlClient$body = array_merge($body, $options->toArray());}+ $body['origin'] ??= 'php-sdk@' . Version::SDK_VERSION;+$response = $this->assertSuccess($this->http->post('/v2/map', $body));return MapData::fromArray($response['data'] ?? $response);diff --git a/apps/php-sdk/src/Models/ScrapeOptions.php b/apps/php-sdk/src/Models/ScrapeOptions.phpindex 1c9fc3b46..bd154702d 100644--- a/apps/php-sdk/src/Models/ScrapeOptions.php+++ b/apps/php-sdk/src/Models/ScrapeOptions.php@@ -112,6 +112,21 @@ final class ScrapeOptions);}+ if ($this->changeTracking === true) {+ $formats = $data['formats'] ?? [];+ $hasFormat = static fn (string $type): bool => array_filter(+ $formats,+ static fn (string|array $f): bool => (is_array($f) ? ($f['type'] ?? null) : $f) === $type,+ ) !== [];+ if (!$hasFormat('markdown')) {+ $formats[] = 'markdown';+ }+ if (!$hasFormat('changeTracking')) {+ $formats[] = 'changeTracking';+ }+ $data['formats'] = $formats;+ }+$fields = ['headers' => $this->headers,'includeTags' => $this->includeTags,@@ -137,7 +152,6 @@ final class ScrapeOptions'checkPromptInjection' => $this->checkPromptInjection,'integration' => $this->integration,'profile' => $this->profile,- 'changeTracking' => $this->changeTracking,'redactPII' => $this->redactPII,'auditMetadata' => $this->auditMetadata?->toArray(),];diff --git a/apps/php-sdk/tests/Unit/ChangeTrackingOriginTest.php b/apps/php-sdk/tests/Unit/ChangeTrackingOriginTest.phpnew file mode 100644index 000000000..80932c3a4--- /dev/null+++ b/apps/php-sdk/tests/Unit/ChangeTrackingOriginTest.php@@ -0,0 +1,59 @@+<?php++declare(strict_types=1);++use Firecrawl\Models\BatchScrapeOptions;+use Firecrawl\Models\JsonFormat;+use Firecrawl\Models\ScrapeOptions;+use Firecrawl\Version;+use GuzzleHttp\Psr7\Response;++it('sends changeTracking as a format alongside markdown', function (): void {+ expect(ScrapeOptions::with(changeTracking: true)->toArray())+ ->toBe(['formats' => ['markdown', 'changeTracking']]);++ expect(ScrapeOptions::with(formats: ['html'], changeTracking: true)->toArray())+ ->toBe(['formats' => ['html', 'markdown', 'changeTracking']]);+});++it('keeps existing markdown and changeTracking formats', function (): void {+ $options = ScrapeOptions::with(+ formats: ['changeTracking', JsonFormat::with(prompt: 'title'), 'markdown'],+ changeTracking: true,+ );++ expect($options->toArray()['formats'])+ ->toBe(['changeTracking', ['type' => 'json', 'prompt' => 'title'], 'markdown']);+});++it('never sends a top-level changeTracking key', function (): void {+ expect(ScrapeOptions::with(formats: [JsonFormat::with(prompt: 'title')], changeTracking: true)->toArray())+ ->not->toHaveKey('changeTracking');+ expect(ScrapeOptions::with(formats: ['html'], changeTracking: false)->toArray())+ ->toBe(['formats' => ['html']]);+});++it('sends the SDK origin on crawl, batch scrape and map', function (): void {+ $history = new ArrayObject();+ $client = fakeFirecrawlClient([+ new Response(200, [], json_encode(['success' => true, 'id' => 'crawl-1', 'url' => 'https://api.firecrawl.dev/v2/crawl/crawl-1'])),+ new Response(200, [], json_encode(['success' => true, 'id' => 'batch-1', 'url' => 'https://api.firecrawl.dev/v2/batch/scrape/batch-1'])),+ new Response(200, [], json_encode(['success' => true, 'links' => []])),+ ], $history);++ $client->startCrawl('https://example.com');+ $client->startBatchScrape(['https://example.com'], BatchScrapeOptions::with(+ options: ScrapeOptions::with(changeTracking: true),+ ));+ $client->map('https://example.com');++ $bodies = array_map(+ fn (array $entry): array => json_decode((string) $entry['request']->getBody(), true),+ $history->getArrayCopy(),+ );+ foreach ($bodies as $body) {+ expect($body['origin'])->toBe('php-sdk@' . Version::SDK_VERSION);+ }+ expect($bodies[1]['formats'])->toBe(['markdown', 'changeTracking']);+ expect($bodies[1])->not->toHaveKey('changeTracking');+});