Skip to content
Snippets Groups Projects
Commit c7a34f47 authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

Merge branch 'master' of github.com:distantmagic/resonance

parents 12ad4edf f4d11fbe
No related branches found
No related tags found
No related merge requests found
......@@ -47,8 +47,8 @@ readonly class DialogueNode implements DialogueNodeInterface
{
foreach ($other->getPotentialResponses() as $response) {
$this->addPotentialResponse($response);
}
}
}
public function getMessageProducer(): DialogueMessageProducerInterface
{
......
......@@ -4,8 +4,7 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
use Ds\PriorityQueue;
use Ds\Stack;
use Ds\Set;
use Generator;
use IteratorAggregate;
......@@ -15,10 +14,10 @@ use IteratorAggregate;
readonly class DialogueResponseSortedIterator implements IteratorAggregate
{
/**
* @param iterable<DialogueResponseInterface> $responses
* @param Set<DialogueResponseInterface> $responses
*/
public function __construct(
private iterable $responses,
private Set $responses,
) {}
/**
......@@ -26,29 +25,17 @@ readonly class DialogueResponseSortedIterator implements IteratorAggregate
*/
public function getIterator(): Generator
{
/**
* @var PriorityQueue<DialogueResponseInterface> $responsesPriorityQueue
*/
$responsesPriorityQueue = new PriorityQueue();
foreach ($this->responses as $response) {
$responsesPriorityQueue->push(
$response,
$response->getCost(),
);
}
/**
* @var Stack<DialogueResponseInterface> $sortedResponses
*/
$sortedResponses = new Stack();
$responses = $this->responses->toArray();
foreach ($responsesPriorityQueue as $response) {
$sortedResponses->push($response);
}
usort($responses, $this->compareResponses(...));
foreach ($sortedResponses as $response) {
foreach ($responses as $response) {
yield $response;
}
}
private function compareResponses(DialogueResponseInterface $a, DialogueResponseInterface $b): int
{
return $a->getCost() <=> $b->getCost();
}
}
......@@ -6,6 +6,8 @@ namespace Distantmagic\Resonance;
use Distantmagic\Resonance\DialogueResponse\LiteralInputResponse;
use Distantmagic\Resonance\DialogueResponse\LlamaCppExtractSubjectResponse;
use Distantmagic\Resonance\DialogueResponse\LlamaCppExtractWhenResponse;
use Ds\Set;
use Mockery;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
......@@ -31,21 +33,34 @@ final class DialogueResponseSortedIteratorTest extends TestCase
},
);
$response2 = new LiteralInputResponse(
$response2 = new LlamaCppExtractWhenResponse(
llamaCppExtractWhen: Mockery::mock(LlamaCppExtractWhenInterface::class),
condition: "user's occupation",
whenProvided: static function (): DialogueResponseResolution {
return new DialogueResponseResolution(
followUp: null,
status: DialogueResponseResolutionStatus::CannotRespond,
);
},
);
$response3 = new LiteralInputResponse(
when: 'marketing',
followUp: $marketingNode,
);
$responses = [
$responses = new Set([
$response1,
$response2,
];
$response3,
]);
$sortedResponses = iterator_to_array(new DialogueResponseSortedIterator($responses));
self::assertEquals([
$response2,
$response3,
$response1,
$response2,
], $sortedResponses);
}
}
......@@ -56,8 +56,17 @@ readonly class LlamaCppExtractWhen implements LlamaCppExtractWhenInterface
$ret .= $token;
}
$yesNoMaybe = YesNoMaybe::tryFrom($ret);
if (!($yesNoMaybe instanceof YesNoMaybe)) {
return new LlamaCppExtractYesNoMaybeResult(
result: null,
isFailed: true,
);
}
return new LlamaCppExtractYesNoMaybeResult(
result: YesNoMaybe::from($ret),
result: $yesNoMaybe,
isFailed: false,
);
}
......
......@@ -22,11 +22,8 @@ final readonly class ObservableTaskFactory
iterableTask: static function () use ($iterableTask): Generator {
yield new ObservableTaskStatusUpdate(ObservableTaskStatus::Running, null);
try {
yield from $iterableTask();
} finally {
yield new ObservableTaskStatusUpdate(ObservableTaskStatus::Finished, null);
}
yield from $iterableTask();
yield new ObservableTaskStatusUpdate(ObservableTaskStatus::Finished, null);
},
inactivityTimeout: $inactivityTimeout,
),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment