Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(php-sdk): use last_id for conversation pagination (#44659)

Winoren2d3a1b94560d
diff --git a/sdks/php-client/README.md b/sdks/php-client/README.md
index 6c5c7ca3a32..7f3880cf959 100644
--- a/sdks/php-client/README.md
+++ b/sdks/php-client/README.md
@@ -136,6 +136,22 @@ The SDK sends the top-level `inputs` as `{}`, as required by the API, for chat,
completion and workflow requests. Nonempty input maps and nested arrays retain
their values and JSON types.
+## Conversation pagination
+
+Pass the final conversation ID from the previous page as the second argument:
+
+```php
+$page = json_decode((string) $chatClient->get_conversations("user_id", null, 20)->getBody(), true);
+if ($page['has_more'] && $page['data']) {
+ $lastConversation = end($page['data']);
+ $nextPage = $chatClient->get_conversations("user_id", $lastConversation['id'], 20);
+}
+```
+
+For compatibility, this argument retains its legacy PHP name `$first_id`, but the
+conversation-list request sends the API's `last_id` field. Message history through
+`get_conversation_messages()` continues to send `first_id`.
+
## License
This SDK is released under the MIT License.
@@ -146,6 +162,7 @@ After `composer install`, run these commands from this directory:
```sh
php tests/base_url_test.php
+php tests/conversation_pagination_test.php
php tests/input_object_test.php
php tests/streaming_test.php
```
diff --git a/sdks/php-client/dify-client.php b/sdks/php-client/dify-client.php
index 8b2c4d07d0c..ce5b7fdc4d1 100644
--- a/sdks/php-client/dify-client.php
+++ b/sdks/php-client/dify-client.php
@@ -136,7 +136,7 @@ class ChatClient extends DifyClient {
public function get_conversations($user, $first_id = null, $limit = null, $pinned = null) {
$params = [
'user' => $user,
- 'first_id' => $first_id,
+ 'last_id' => $first_id,
'limit' => $limit,
'pinned'=> $pinned,
];
diff --git a/sdks/php-client/tests/conversation_pagination_test.php b/sdks/php-client/tests/conversation_pagination_test.php
new file mode 100644
index 00000000000..d5c9c1a81b2
--- /dev/null
+++ b/sdks/php-client/tests/conversation_pagination_test.php
@@ -0,0 +1,55 @@
+<?php
+
+require dirname(__DIR__) . '/vendor/autoload.php';
+
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Middleware;
+use GuzzleHttp\Psr7\Response;
+
+$cursor = '11111111-1111-4111-8111-111111111111';
+$conversationId = '22222222-2222-4222-8222-222222222222';
+$user = '[email protected]';
+$history = [];
+$stack = HandlerStack::create(new MockHandler([
+ new Response(200, [], '{"data":[{"id":"' . $cursor . '"}],"has_more":true}'),
+ new Response(200, [], '{"data":[],"has_more":false}'),
+ new Response(200, [], '{"data":[],"has_more":false}'),
+]));
+$stack->push(Middleware::history($history));
+$client = new ChatClient('test-api-key');
+$property = new ReflectionProperty(DifyClient::class, 'client');
+if (PHP_VERSION_ID < 80100) {
+ $property->setAccessible(true);
+}
+// Keep the real SDK and Guzzle URI/query serialization; replace only HTTP transport.
+$property->getValue($client)->getConfig('handler')->setHandler($stack);
+
+$firstPage = json_decode((string) $client->get_conversations($user, null, 1)->getBody(), true);
+$client->get_conversations($user, $firstPage['data'][0]['id'], 1);
+$client->get_conversation_messages($user, $conversationId, $cursor, 1);
+
+foreach ($history as $transaction) {
+ $request = $transaction['request'];
+ if ($request->getMethod() !== 'GET'
+ || $request->getHeaderLine('Authorization') !== 'Bearer test-api-key') {
+ throw new RuntimeException('Request method or authentication changed');
+ }
+}
+
+parse_str($history[0]['request']->getUri()->getQuery(), $firstQuery);
+parse_str($history[1]['request']->getUri()->getQuery(), $nextQuery);
+parse_str($history[2]['request']->getUri()->getQuery(), $messageQuery);
+if ($firstQuery !== ['user' => $user, 'limit' => '1']) {
+ throw new RuntimeException('First page must not send a cursor');
+}
+if ($history[1]['request']->getUri()->getPath() !== '/v1/conversations'
+ || $nextQuery !== ['user' => $user, 'last_id' => $cursor, 'limit' => '1']) {
+ throw new RuntimeException('Conversation pagination must send last_id; got ' . json_encode($nextQuery));
+}
+if ($history[2]['request']->getUri()->getPath() !== '/v1/messages'
+ || $messageQuery !== ['user' => $user, 'conversation_id' => $conversationId, 'first_id' => $cursor, 'limit' => '1']) {
+ throw new RuntimeException('Message pagination must retain first_id');
+}
+
+echo "Conversation pagination regression: 3 requests passed\n";