harden runner errors

This commit is contained in:
team2
2026-02-16 22:39:07 +01:00
parent 825a63c530
commit 34e3d8139e

View File

@@ -26,7 +26,9 @@ final readonly class AgentRunner
private bool $debug, private bool $debug,
private bool $logPrompt, private bool $logPrompt,
private bool $logContext, private bool $logContext,
) {} )
{
}
public function run(string $prompt, string $userId): Generator public function run(string $prompt, string $userId): Generator
{ {
@@ -88,7 +90,12 @@ final readonly class AgentRunner
$chunker = new StreamChunker(); $chunker = new StreamChunker();
foreach ($this->ollamaClient->stream($finalPrompt) as $token) { foreach ($this->ollamaClient->stream($finalPrompt) as $token) {
$cleanToken = $this->thinkSuppressor->filter($token);
if (!is_string($token)) {
continue;
}
$cleanToken = $this->thinkSuppressor->filter((string)$token);
if ($cleanToken === '') { if ($cleanToken === '') {
continue; continue;
@@ -108,6 +115,8 @@ final readonly class AgentRunner
$finalChunk = $chunker->flush(); $finalChunk = $chunker->flush();
if ($finalChunk !== null) { if ($finalChunk !== null) {
yield $finalChunk; yield $finalChunk;
} else {
yield '... no data received from llm';
} }
// --------------------------------------------------------- // ---------------------------------------------------------
@@ -120,17 +129,17 @@ final readonly class AgentRunner
); );
$this->agentLogger->info('Agent run finished', [ $this->agentLogger->info('Agent run finished', [
'userId' => $userId, 'userId' => $userId,
'outputLength' => mb_strlen($fullOutput), 'outputLength' => mb_strlen($fullOutput),
'contextMode' => 'recent', 'contextMode' => 'recent',
]); ]);
} catch (Throwable $e) { } catch (Throwable $e) {
$this->agentLogger->error('Agent run failed', [ $this->agentLogger->error('Agent run failed', [
'userId' => $userId, 'userId' => $userId,
'exception' => $e, 'exception' => $e,
]); ]);
yield "\n❌ An internal error occurred while processing the request."; yield "\n❌ An internal error occurred while processing the request. \nError: " . $e->getMessage();
} }
} }
} }