Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
AssistantClient
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 7
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 createAssistant
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 listAssistants
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 describeAssistant
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 deleteAssistant
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 chat
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 handleResponse
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace Mbvb1223\Pinecone\Assistant;
6
7use GuzzleHttp\Client;
8use GuzzleHttp\Exception\GuzzleException;
9use Mbvb1223\Pinecone\Utils\Configuration;
10use Mbvb1223\Pinecone\Errors\PineconeApiException;
11use Mbvb1223\Pinecone\Errors\PineconeException;
12use Psr\Http\Message\ResponseInterface;
13
14class AssistantClient
15{
16    private Client $httpClient;
17    private Configuration $config;
18
19    public function __construct(Configuration $config)
20    {
21        $this->config = $config;
22        $this->httpClient = new Client([
23            'base_uri' => 'https://api.pinecone.io',
24            'timeout' => $config->getTimeout(),
25            'headers' => $config->getDefaultHeaders(),
26        ]);
27    }
28
29    public function createAssistant(string $name, array $instructions = []): array
30    {
31        try {
32            $payload = [
33                'name' => $name,
34                'instructions' => $instructions,
35            ];
36
37            $response = $this->httpClient->post('/assistants', [
38                'json' => $payload,
39            ]);
40
41            return $this->handleResponse($response);
42        } catch (GuzzleException $e) {
43            throw new PineconeException('Failed to create assistant: ' . $e->getMessage(), 0, $e);
44        }
45    }
46
47    public function listAssistants(): array
48    {
49        try {
50            $response = $this->httpClient->get('/assistants');
51
52            return $this->handleResponse($response);
53        } catch (GuzzleException $e) {
54            throw new PineconeException('Failed to list assistants: ' . $e->getMessage(), 0, $e);
55        }
56    }
57
58    public function describeAssistant(string $assistantName): array
59    {
60        try {
61            $response = $this->httpClient->get("/assistants/{$assistantName}");
62
63            return $this->handleResponse($response);
64        } catch (GuzzleException $e) {
65            throw new PineconeException('Failed to describe assistant: ' . $e->getMessage(), 0, $e);
66        }
67    }
68
69    public function deleteAssistant(string $assistantName): void
70    {
71        try {
72            $response = $this->httpClient->delete("/assistants/{$assistantName}");
73            $this->handleResponse($response);
74        } catch (GuzzleException $e) {
75            throw new PineconeException('Failed to delete assistant: ' . $e->getMessage(), 0, $e);
76        }
77    }
78
79    public function chat(string $assistantName, array $messages, array $options = []): array
80    {
81        try {
82            $payload = array_merge([
83                'messages' => $messages,
84            ], $options);
85
86            $response = $this->httpClient->post("/assistants/{$assistantName}/chat", [
87                'json' => $payload,
88            ]);
89
90            return $this->handleResponse($response);
91        } catch (GuzzleException $e) {
92            throw new PineconeException('Failed to chat with assistant: ' . $e->getMessage(), 0, $e);
93        }
94    }
95
96    private function handleResponse(ResponseInterface $response): array
97    {
98        $statusCode = $response->getStatusCode();
99        $body = $response->getBody()->getContents();
100
101        if ($statusCode >= 400) {
102            $data = json_decode($body, true) ?? [];
103            $message = $data['message'] ?? 'API request failed';
104            throw new PineconeApiException($message, $statusCode, $data);
105        }
106
107        if (empty($body)) {
108            return [];
109        }
110
111        $decoded = json_decode($body, true);
112        if (json_last_error() !== JSON_ERROR_NONE) {
113            throw new PineconeException('Failed to decode JSON response: ' . json_last_error_msg());
114        }
115
116        return $decoded;
117    }
118}