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