• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-solver / 14341185346

08 Apr 2025 06:42PM UTC coverage: 81.115% (-7.1%) from 88.172%
14341185346

Pull #323

github

eliashaeussler
[FEATURE] Add Claude as additional solution provider
Pull Request #323: [FEATURE] Add Claude as additional solution provider

3 of 92 new or added lines in 5 files covered. (3.26%)

902 of 1112 relevant lines covered (81.12%)

2.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/Classes/ProblemSolving/Solution/Model/CompletionResponse.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "solver".
7
 *
8
 * Copyright (C) 2023-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3Solver\ProblemSolving\Solution\Model;
25

26
use Anthropic\Responses as AnthropicResponses;
27
use OpenAI\Responses as OpenAIResponses;
28

29
/**
30
 * CompletionResponse
31
 *
32
 * @author Elias Häußler <elias@haeussler.dev>
33
 * @license GPL-2.0-or-later
34
 *
35
 * @phpstan-import-type MessageArray from Message
36
 * @phpstan-type CompletionResponseArray array{index: int, message: MessageArray, finishReason: string|null}
37
 */
38
final class CompletionResponse implements \JsonSerializable
39
{
40
    public function __construct(
7✔
41
        public readonly int $index,
42
        public readonly Message $message,
43
        public readonly ?string $finishReason = null,
44
    ) {}
7✔
45

46
    public static function fromOpenAIChoice(
2✔
47
        OpenAIResponses\Chat\CreateResponseChoice|OpenAIResponses\Chat\CreateStreamedResponseChoice $choice,
48
    ): self {
49
        if ($choice instanceof OpenAIResponses\Chat\CreateStreamedResponseChoice) {
2✔
50
            $role = (string)$choice->delta->role;
1✔
51
            $content = $choice->delta->content;
1✔
52
        } else {
53
            $role = $choice->message->role;
1✔
54
            $content = $choice->message->content;
1✔
55
        }
56

57
        return new self($choice->index, new Message($role, $content), $choice->finishReason);
2✔
58
    }
59

NEW
60
    public static function fromAnthropicContent(
×
61
        int $index,
62
        AnthropicResponses\Messages\CreateResponseContent $content,
63
        ?string $stopReason,
64
    ): self {
NEW
65
        return new self($index, new Message($content->type, $content->text), $stopReason);
×
66
    }
67

68
    /**
69
     * @param array{index?: int, message?: array{role?: string, content?: string|null}, finishReason?: string|null} $response
70
     */
71
    public static function fromArray(array $response): self
1✔
72
    {
73
        $message = $response['message'] ?? [];
1✔
74
        $message['role'] ??= '';
1✔
75
        $message['content'] ??= null;
1✔
76

77
        return new self(
1✔
78
            $response['index'] ?? 0,
1✔
79
            new Message($message['role'], $message['content']),
1✔
80
            $response['finishReason'] ?? null,
1✔
81
        );
1✔
82
    }
83

84
    public function merge(self $other): self
1✔
85
    {
86
        return new self(
1✔
87
            $this->index,
1✔
88
            new Message(
1✔
89
                $other->message->role,
1✔
90
                $this->message->content . $other->message->content,
1✔
91
            ),
1✔
92
            $other->finishReason,
1✔
93
        );
1✔
94
    }
95

96
    public function isFinished(): bool
1✔
97
    {
98
        return $this->finishReason !== null;
1✔
99
    }
100

101
    /**
102
     * @return CompletionResponseArray
103
     */
104
    public function toArray(): array
2✔
105
    {
106
        return [
2✔
107
            'index' => $this->index,
2✔
108
            'message' => $this->message->toArray(),
2✔
109
            'finishReason' => $this->finishReason,
2✔
110
        ];
2✔
111
    }
112

113
    /**
114
     * @return CompletionResponseArray
115
     */
116
    public function jsonSerialize(): array
1✔
117
    {
118
        return $this->toArray();
1✔
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc