|
4 | 4 |
|
5 | 5 | namespace Cortex\Pipeline; |
6 | 6 |
|
| 7 | +use Cortex\LLM\Data\Usage; |
7 | 8 | use Cortex\Agents\Data\Step; |
8 | 9 | use Illuminate\Support\Fluent; |
9 | 10 | use Illuminate\Support\Collection; |
| 11 | +use Cortex\LLM\Data\Messages\MessageCollection; |
10 | 12 |
|
11 | 13 | /** |
12 | 14 | * @extends Fluent<string, mixed> |
13 | 15 | */ |
14 | 16 | class Context extends Fluent |
15 | 17 | { |
| 18 | + public const string STEPS_KEY = 'steps'; |
| 19 | + public const string CURRENT_STEP_KEY = 'current_step'; |
| 20 | + public const string USAGE_SO_FAR_KEY = 'usage_so_far'; |
| 21 | + public const string MESSAGE_HISTORY_KEY = 'message_history'; |
| 22 | + |
16 | 23 | /** |
| 24 | + * Get the steps from the context. |
| 25 | + * |
17 | 26 | * @return \Illuminate\Support\Collection<int, \Cortex\Agents\Data\Step> |
18 | 27 | */ |
19 | 28 | public function getSteps(): Collection |
20 | 29 | { |
21 | | - return $this->get('steps', new Collection()); |
| 30 | + return $this->get(self::STEPS_KEY, new Collection()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Determines if the context has any steps. |
| 35 | + */ |
| 36 | + public function hasSteps(): bool |
| 37 | + { |
| 38 | + return $this->getSteps()->isNotEmpty(); |
22 | 39 | } |
23 | 40 |
|
24 | 41 | /** |
| 42 | + * Set the steps in the context. |
| 43 | + * |
25 | 44 | * @param \Illuminate\Support\Collection<int, \Cortex\Agents\Data\Step> $steps |
26 | 45 | */ |
27 | 46 | public function setSteps(Collection $steps): void |
28 | 47 | { |
29 | | - $this->set('steps', $steps); |
| 48 | + $this->set(self::STEPS_KEY, $steps); |
30 | 49 | } |
31 | 50 |
|
| 51 | + /** |
| 52 | + * Get the current step from the context. |
| 53 | + */ |
32 | 54 | public function getCurrentStep(): Step |
33 | 55 | { |
34 | | - return $this->get('current_step', new Step(number: 1)); |
| 56 | + return $this->get(self::CURRENT_STEP_KEY, new Step(number: 1)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Get the current step number from the context. |
| 61 | + */ |
| 62 | + public function getCurrentStepNumber(): int |
| 63 | + { |
| 64 | + return $this->getCurrentStep()->number; |
35 | 65 | } |
36 | 66 |
|
| 67 | + /** |
| 68 | + * Set the current step in the context. |
| 69 | + */ |
37 | 70 | public function setCurrentStep(Step $step): void |
38 | 71 | { |
39 | | - $this->set('current_step', $step); |
| 72 | + $this->set(self::CURRENT_STEP_KEY, $step); |
40 | 73 | } |
41 | 74 |
|
| 75 | + /** |
| 76 | + * Add a step to the context. |
| 77 | + */ |
42 | 78 | public function addStep(Step $step): void |
43 | 79 | { |
44 | 80 | $steps = $this->getSteps()->push($step); |
45 | 81 | $this->setCurrentStep($step); |
46 | 82 | $this->setSteps($steps); |
47 | 83 | } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the usage so far from the context. |
| 87 | + */ |
| 88 | + public function getUsageSoFar(): Usage |
| 89 | + { |
| 90 | + return $this->get(self::USAGE_SO_FAR_KEY, Usage::empty()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set the usage so far in the context. |
| 95 | + */ |
| 96 | + public function setUsageSoFar(Usage $usage): void |
| 97 | + { |
| 98 | + $this->set(self::USAGE_SO_FAR_KEY, $usage); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Append usage to the context. |
| 103 | + */ |
| 104 | + public function appendUsage(Usage $usage): static |
| 105 | + { |
| 106 | + $usageSoFar = $this->getUsageSoFar()->add($usage); |
| 107 | + $this->setUsageSoFar($usageSoFar); |
| 108 | + |
| 109 | + return $this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the message history from the context. |
| 114 | + */ |
| 115 | + public function getMessageHistory(): MessageCollection |
| 116 | + { |
| 117 | + return $this->get(self::MESSAGE_HISTORY_KEY, new MessageCollection()); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Set the message history in the context. |
| 122 | + */ |
| 123 | + public function setMessageHistory(MessageCollection $messages): void |
| 124 | + { |
| 125 | + $this->set(self::MESSAGE_HISTORY_KEY, $messages); |
| 126 | + } |
48 | 127 | } |
0 commit comments