feat(agentic): add workspace state aliases
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
c8a2d62d27
commit
6cb5a9f39a
2 changed files with 52 additions and 5 deletions
|
|
@ -92,18 +92,20 @@ class WorkspaceState extends Model
|
|||
}
|
||||
|
||||
public static function getOrCreate(
|
||||
AgentPlan $plan,
|
||||
AgentPlan|int $plan,
|
||||
string $key,
|
||||
mixed $default = null,
|
||||
string $type = self::TYPE_JSON
|
||||
): self {
|
||||
$planId = static::planId($plan);
|
||||
|
||||
return static::firstOrCreate(
|
||||
['agent_plan_id' => $plan->id, 'key' => $key],
|
||||
['agent_plan_id' => $planId, 'key' => $key],
|
||||
['value' => $default, 'type' => $type]
|
||||
);
|
||||
}
|
||||
|
||||
public static function getValue(AgentPlan $plan, string $key, mixed $default = null): mixed
|
||||
public static function getValue(AgentPlan|int $plan, string $key, mixed $default = null): mixed
|
||||
{
|
||||
$state = static::forPlan($plan)->where('key', $key)->first();
|
||||
|
||||
|
|
@ -111,17 +113,37 @@ class WorkspaceState extends Model
|
|||
}
|
||||
|
||||
public static function setValue(
|
||||
AgentPlan $plan,
|
||||
AgentPlan|int $plan,
|
||||
string $key,
|
||||
mixed $value,
|
||||
string $type = self::TYPE_JSON
|
||||
): self {
|
||||
$planId = static::planId($plan);
|
||||
|
||||
return static::updateOrCreate(
|
||||
['agent_plan_id' => $plan->id, 'key' => $key],
|
||||
['agent_plan_id' => $planId, 'key' => $key],
|
||||
['value' => $value, 'type' => $type]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* WorkspaceState::set($plan->id, 'discovered_pattern', 'observer');
|
||||
* WorkspaceState::get($plan->id, 'discovered_pattern');
|
||||
*/
|
||||
public static function set(
|
||||
AgentPlan|int $plan,
|
||||
string $key,
|
||||
mixed $value,
|
||||
string $type = self::TYPE_JSON
|
||||
): self {
|
||||
return static::setValue($plan, $key, $value, $type);
|
||||
}
|
||||
|
||||
public static function get(AgentPlan|int $plan, string $key, mixed $default = null): mixed
|
||||
{
|
||||
return static::getValue($plan, $key, $default);
|
||||
}
|
||||
|
||||
public function setTypedValue(mixed $value): void
|
||||
{
|
||||
$this->update(['value' => $value]);
|
||||
|
|
@ -143,4 +165,9 @@ class WorkspaceState extends Model
|
|||
'updated_at' => $this->updated_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function planId(AgentPlan|int $plan): int
|
||||
{
|
||||
return $plan instanceof AgentPlan ? $plan->id : $plan;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -236,6 +236,26 @@ class WorkspaceStateTest extends TestCase
|
|||
$this->assertEquals(['n' => 2], WorkspaceState::getValue($this->plan, 'counter'));
|
||||
}
|
||||
|
||||
public function test_set_alias_creates_new_state(): void
|
||||
{
|
||||
$state = WorkspaceState::set($this->plan->id, 'discovered_pattern', 'observer');
|
||||
|
||||
$this->assertDatabaseHas('agent_workspace_states', [
|
||||
'agent_plan_id' => $this->plan->id,
|
||||
'key' => 'discovered_pattern',
|
||||
]);
|
||||
$this->assertEquals('observer', $state->value);
|
||||
}
|
||||
|
||||
public function test_get_alias_returns_stored_value(): void
|
||||
{
|
||||
WorkspaceState::set($this->plan->id, 'discovered_pattern', 'observer');
|
||||
|
||||
$value = WorkspaceState::get($this->plan->id, 'discovered_pattern');
|
||||
|
||||
$this->assertEquals('observer', $value);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// MCP output
|
||||
// =========================================================================
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue