Skip to content

Commit e1ffaba

Browse files
committed
psalm fixes
Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent 254b4ea commit e1ffaba

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

lib/Service/OpenAiAPIService.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function getAdminQuotaInfo(): array {
332332
* @param string $model
333333
* @param int|null $maxTokens
334334
* @param array|null $extraParams
335-
* @return array|string[]
335+
* @return string[]
336336
* @throws Exception
337337
*/
338338
public function createCompletion(
@@ -389,6 +389,10 @@ public function createCompletion(
389389
$completions = [];
390390

391391
foreach ($response['choices'] as $choice) {
392+
if (!isset($choice['text']) || !is_string($choice['text'])) {
393+
$this->logger->debug('Text generation yielded empty or invalid response: ' . json_encode($choice));
394+
continue;
395+
}
392396
$completions[] = $choice['text'];
393397
}
394398

@@ -408,7 +412,7 @@ public function createCompletion(
408412
* @param array|null $extraParams
409413
* @param string|null $toolMessage JSON string with role, content, tool_call_id
410414
* @param array|null $tools
411-
* @return array<string, array<string>>
415+
* @return array{messages: array<string>, tool_calls: array<string>}
412416
* @throws Exception
413417
*/
414418
public function createChatCompletion(
@@ -537,12 +541,19 @@ public function createChatCompletion(
537541
// fix the tool_calls format, make it like expected by the context_agent app
538542
$choice['message']['tool_calls'] = array_map(static function ($toolCall) {
539543
$toolCall['function']['id'] = $toolCall['id'];
540-
$toolCall['function']['args'] = json_decode($toolCall['function']['arguments']);
544+
$toolCall['function']['args'] = json_decode($toolCall['function']['arguments']) ?: (object)[];
541545
unset($toolCall['function']['arguments']);
542546
return $toolCall['function'];
543547
}, $choice['message']['tool_calls']);
544-
$completions['tool_calls'][] = json_encode($choice['message']['tool_calls']);
548+
549+
$toolCalls = json_encode($choice['message']['tool_calls']);
550+
if ($toolCalls === false) {
551+
$this->logger->debug('Tool calls JSON encoding error: ' . json_last_error_msg());
552+
} else {
553+
$completions['tool_calls'][] = $toolCalls;
554+
}
545555
}
556+
546557
// always try to get a message
547558
if (isset($choice['message']['content']) && is_string($choice['message']['content'])) {
548559
$completions['messages'][] = $choice['message']['content'];
@@ -745,8 +756,8 @@ public function getExpTextProcessingTime(): int {
745756
* @return void
746757
*/
747758
public function updateExpTextProcessingTime(int $runtime): void {
748-
$oldTime = $this->getExpTextProcessingTime();
749-
$newTime = (1 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR) * $oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * $runtime;
759+
$oldTime = floatval($this->getExpImgProcessingTime());
760+
$newTime = (1.0 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR) * $oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * floatval($runtime);
750761

751762
if ($this->isUsingOpenAi()) {
752763
$this->appConfig->setValueString(Application::APP_ID, 'openai_text_generation_time', strval(intval($newTime)));
@@ -769,8 +780,8 @@ public function getExpImgProcessingTime(): int {
769780
* @return void
770781
*/
771782
public function updateExpImgProcessingTime(int $runtime): void {
772-
$oldTime = $this->getExpImgProcessingTime();
773-
$newTime = (1 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR) * $oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * $runtime;
783+
$oldTime = floatval($this->getExpImgProcessingTime());
784+
$newTime = (1.0 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR) * $oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * floatval($runtime);
774785

775786
if ($this->isUsingOpenAi()) {
776787
$this->appConfig->setValueString(Application::APP_ID, 'openai_image_generation_time', strval(intval($newTime)));

lib/TaskProcessing/SummaryProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ public function process(?string $userId, array $input, callable $reportProgress)
152152
}
153153

154154
// Take only one completion for each chunk and combine them into a single summary (which may be used as the next prompt)
155-
$completionStrings = array_map(fn (array $val): string => end($val), $completions);
155+
$completionStrings = array_values(array_filter(
156+
array_map(fn (array $val): false|string => end($val), $completions),
157+
fn (false|string $val): bool => $val !== false,
158+
));
156159
$summary = implode(' ', $completionStrings);
157160

158161
$prompts = self::chunkSplitPrompt($summary);

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
findUnusedBaselineEntry="true"
1010
findUnusedCode="false"
1111
resolveFromConfigFile="true"
12+
ensureOverrideAttribute="false"
1213
phpVersion="8.1"
1314
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1415
xmlns="https://getpsalm.org/schema/config"

0 commit comments

Comments
 (0)