Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/Contracts/ChatWorkspacePromptsSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Contracts;

class ChatWorkspacePromptsSettings extends Data
{
public string $system;
public string $searchDescription;
public string $searchQParam;
public string $searchIndexUidParam;
Comment on lines +9 to +12
Copy link
Collaborator

@norkunas norkunas Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would pass empty strings, will it still work? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!


/**
* @param array{
* system: string,
* searchDescription: string,
* searchQParam: string,
* searchIndexUidParam: string
* } $params
*/
public function __construct(array $params)
{
parent::__construct($params);

$this->system = $params['system'];
$this->searchDescription = $params['searchDescription'];
$this->searchQParam = $params['searchQParam'];
$this->searchIndexUidParam = $params['searchIndexUidParam'];
}

public function getSystem(): string
{
return $this->system;
}

public function getSearchDescription(): string
{
return $this->searchDescription;
}

public function getSearchQParam(): string
{
return $this->searchQParam;
}

public function getSearchIndexUidParam(): string
{
return $this->searchIndexUidParam;
}

/**
* @return array{
* system: string,
* searchDescription: string,
* searchQParam: string,
* searchIndexUidParam: string
* }
*/
public function toArray(): array
{
return [
'system' => $this->system,
'searchDescription' => $this->searchDescription,
'searchQParam' => $this->searchQParam,
'searchIndexUidParam' => $this->searchIndexUidParam,
];
}
}
53 changes: 43 additions & 10 deletions src/Contracts/ChatWorkspaceSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,51 @@

namespace Meilisearch\Contracts;

use Meilisearch\Contracts\ChatWorkspacePromptsSettings;

class ChatWorkspaceSettings extends Data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this misses many typehints

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated 🙌

{
private ?string $source;
private string $source;
private ?string $orgId;
private ?string $projectId;
private ?string $apiVersion;
private ?string $deploymentId;
private ?string $baseUrl;
private ?string $apiKey;
private array $prompts;
private ?ChatWorkspacePromptsSettings $prompts;

Check failure on line 18 in src/Contracts/ChatWorkspaceSettings.php

View workflow job for this annotation

GitHub Actions / phpstan-tests

Property Meilisearch\Contracts\ChatWorkspaceSettings::$prompts (Meilisearch\Contracts\ChatWorkspacePromptsSettings|null) is never assigned null so it can be removed from the property type.

/**
* @param array{
* source: string,
* orgId?: string,
* projectId?: string,
* apiVersion?: string,
* deploymentId?: string,
* baseUrl?: string,
* apiKey?: string,
* prompts?: array{
* system: string,
* searchDescription: string,
* searchQParam: string,
* searchIndexUidParam: string
* }
* } $params
*/
public function __construct(array $params)
{
parent::__construct($params);

$this->source = $params['source'] ?? null;
$this->source = $params['source'];
$this->orgId = $params['orgId'] ?? null;
$this->projectId = $params['projectId'] ?? null;
$this->apiVersion = $params['apiVersion'] ?? null;
$this->deploymentId = $params['deploymentId'] ?? null;
$this->baseUrl = $params['baseUrl'] ?? null;
$this->apiKey = $params['apiKey'] ?? null;
$this->prompts = $params['prompts'] ?? [];
$this->prompts = $params['prompts'] === null ? null : new ChatWorkspacePromptsSettings($params['prompts']);

Check failure on line 48 in src/Contracts/ChatWorkspaceSettings.php

View workflow job for this annotation

GitHub Actions / phpstan-tests

Strict comparison using === between array{system: string, searchDescription: string, searchQParam: string, searchIndexUidParam: string} and null will always evaluate to false.
}

public function getSource(): ?string
public function getSource(): string
{
return $this->source;
}
Expand Down Expand Up @@ -64,14 +83,28 @@
return $this->apiKey;
}

/**
* @return array{system?: string, searchDescription?: string, searchQParam?: string, searchIndexUidParam?: string}
*/
public function getPrompts(): array
public function getPrompts(): ?ChatWorkspacePromptsSettings
{
return $this->prompts;
}

/**
* @return array{
* source: string,
* orgId?: string,
* projectId?: string,
* apiVersion?: string,
* deploymentId?: string,
* baseUrl?: string,
* apiKey?: string,
* prompts?: array{
* system: string,
* searchDescription: string,
* searchQParam: string,
* searchIndexUidParam: string
* }
* }
*/
public function toArray(): array
{
return [
Expand All @@ -82,7 +115,7 @@
'deploymentId' => $this->deploymentId,
'baseUrl' => $this->baseUrl,
'apiKey' => $this->apiKey,
'prompts' => $this->prompts,
'prompts' => $this->prompts === null ? null : $this->prompts->toArray(),
];
}
}
4 changes: 2 additions & 2 deletions src/Contracts/ChatWorkspacesResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class ChatWorkspacesResults extends Data

public function __construct(array $params)
{
parent::__construct($params['results'] ?? []);
parent::__construct($params['results']);

$this->offset = $params['offset'];
$this->limit = $params['limit'];
$this->total = $params['total'] ?? 0;
$this->total = $params['total'];
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

class Client implements Http
{
Expand Down
26 changes: 22 additions & 4 deletions tests/Endpoints/ChatWorkspaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Tests\Endpoints;

use Meilisearch\Exceptions\ApiException;
use Meilisearch\Http\Client;
use Tests\TestCase;

final class ChatWorkspaceTest extends TestCase
{
private array $workspaceSettings = [
'source' => 'openAi',
'source' => 'mistral',
'orgId' => 'some-org-id',
'projectId' => 'some-project-id',
'apiVersion' => 'some-api-version',
Expand Down Expand Up @@ -44,6 +45,13 @@ public function testUpdateWorkspacesSettings(): void
self::assertSame('XXX...', $response->getApiKey());
}

public function testExceptionWhenWorkspaceDoesNotExist(): void
{
self::expectException(ApiException::class);
self::expectExceptionCode(404);
$this->client->chats->workspace('non-existent-workspace')->getSettings();
}

public function testGetWorkspaceSettings(): void
{
$this->client->chats->workspace('myWorkspace')->updateSettings($this->workspaceSettings);
Expand All @@ -55,9 +63,13 @@ public function testGetWorkspaceSettings(): void
self::assertSame($this->workspaceSettings['apiVersion'], $response->getApiVersion());
self::assertSame($this->workspaceSettings['deploymentId'], $response->getDeploymentId());
self::assertSame($this->workspaceSettings['baseUrl'], $response->getBaseUrl());
self::assertSame($this->workspaceSettings['prompts']['system'], $response->getPrompts()['system']);
// Meilisearch will mask the API key in the response
self::assertSame('XXX...', $response->getApiKey());

self::assertSame($this->workspaceSettings['prompts']['system'], $response->getPrompts()->getSystem());
self::assertNotEmpty($response->getPrompts()->getSearchDescription());
self::assertNotEmpty($response->getPrompts()->getSearchQParam());
self::assertNotEmpty($response->getPrompts()->getSearchIndexUidParam());
}

public function testListWorkspaces(): void
Expand All @@ -69,19 +81,25 @@ public function testListWorkspaces(): void
], $response->getResults());
}

public function testDeleteWorkspaceSettings(): void
public function testResetWorkspaceSettings(): void
{
$this->client->chats->workspace('myWorkspace')->updateSettings($this->workspaceSettings);
$this->client->chats->workspace('myWorkspace')->resetSettings();
$settingsResponse = $this->client->chats->workspace('myWorkspace')->getSettings();
self::assertSame('openAi', $settingsResponse->getSource());
self::assertSame('openAi', $settingsResponse->getSource()); // Source is reset to openAi for some reason
self::assertNull($settingsResponse->getOrgId());
self::assertNull($settingsResponse->getProjectId());
self::assertNull($settingsResponse->getApiVersion());
self::assertNull($settingsResponse->getDeploymentId());
self::assertNull($settingsResponse->getBaseUrl());
self::assertNull($settingsResponse->getApiKey());
// Prompts are reset to their original values
self::assertNotEmpty($settingsResponse->getPrompts()->getSystem());
self::assertNotEmpty($settingsResponse->getPrompts()->getSearchDescription());
self::assertNotEmpty($settingsResponse->getPrompts()->getSearchQParam());
self::assertNotEmpty($settingsResponse->getPrompts()->getSearchIndexUidParam());

// Workspace still appears when listing workspaces
$listResponse = $this->client->chats->listWorkspaces();
self::assertSame([
['uid' => 'myWorkspace'],
Expand Down
Loading