Skip to content

Commit 35c719f

Browse files
authored
Merge pull request #2239 from Extra-Chill/refactor/agents-api-adoption-orchestrator
refactor: use Agents API package adoption orchestration
2 parents 3cb7979 + 0c44530 commit 35c719f

8 files changed

Lines changed: 494 additions & 103 deletions

composer.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Agents API package adoption state store for Data Machine bundles.
4+
*
5+
* @package DataMachine\Engine\Bundle
6+
*/
7+
8+
namespace DataMachine\Engine\Bundle;
9+
10+
use DataMachine\Core\Database\Agents\Agents;
11+
12+
defined( 'ABSPATH' ) || exit;
13+
14+
/**
15+
* Adapts Data Machine bundle artifact state to the storage-neutral Agents API contract.
16+
*/
17+
final class AgentBundleAdoptionStateStore implements \WP_Agent_Package_Artifact_State_Store {
18+
19+
/** @var array<string,mixed> */
20+
private array $agent;
21+
22+
/** @var array<int,array<string,mixed>> */
23+
private array $installed;
24+
25+
/** @var array<int,array<string,mixed>> */
26+
private array $current;
27+
28+
/** @var array<int,array<string,mixed>> */
29+
private array $target;
30+
31+
/** @var array<string,mixed> */
32+
private array $bundle;
33+
34+
/**
35+
* Constructor.
36+
*
37+
* @param array<string,mixed> $agent Agent row or context.
38+
* @param array<int,array<string,mixed>> $installed Installed bundle artifact rows.
39+
* @param array<int,array<string,mixed>> $current Current artifact rows.
40+
* @param array<int,array<string,mixed>> $target Target artifact rows.
41+
* @param array<string,mixed> $bundle Bundle metadata.
42+
*/
43+
public function __construct( array $agent, array $installed, array $current, array $target, array $bundle ) {
44+
$this->agent = $agent;
45+
$this->installed = $installed;
46+
$this->current = $current;
47+
$this->target = $target;
48+
$this->bundle = $bundle;
49+
}
50+
51+
public function get_installed_artifacts( \WP_Agent_Package $package, array $context = array() ): array {
52+
unset( $package, $context );
53+
return self::package_artifact_rows( $this->installed );
54+
}
55+
56+
public function get_current_artifacts( \WP_Agent_Package $package, array $context = array() ): array {
57+
unset( $package, $context );
58+
return self::package_artifact_rows( $this->current );
59+
}
60+
61+
public function get_target_artifacts( \WP_Agent_Package $package, array $context = array() ): array {
62+
unset( $package, $context );
63+
return self::package_artifact_rows( $this->target );
64+
}
65+
66+
public function record_installed_artifacts( \WP_Agent_Package $package, array $artifacts, array $context = array() ): bool {
67+
unset( $package, $context );
68+
global $wpdb;
69+
70+
$agent_id = (int) ( $this->agent['agent_id'] ?? 0 );
71+
if ( $agent_id <= 0 || ! is_object( $wpdb ) ) {
72+
return false;
73+
}
74+
75+
$agents = new Agents();
76+
$agent = $agents->get_agent( $agent_id );
77+
if ( ! $agent ) {
78+
return false;
79+
}
80+
81+
$config = is_array( $agent['agent_config'] ?? null ) ? $agent['agent_config'] : array();
82+
if ( ! isset( $config['datamachine_bundle'] ) || ! is_array( $config['datamachine_bundle'] ) ) {
83+
$config['datamachine_bundle'] = array();
84+
}
85+
86+
$config['datamachine_bundle']['bundle_slug'] = (string) ( $this->bundle['bundle_slug'] ?? $config['datamachine_bundle']['bundle_slug'] ?? '' );
87+
$config['datamachine_bundle']['bundle_version'] = (string) ( $this->bundle['bundle_version'] ?? $config['datamachine_bundle']['bundle_version'] ?? '' );
88+
$config['datamachine_bundle']['template_slug'] = (string) ( $this->bundle['template_slug'] ?? $config['datamachine_bundle']['template_slug'] ?? $config['datamachine_bundle']['bundle_slug'] ?? '' );
89+
$config['datamachine_bundle']['template_version'] = (string) ( $this->bundle['template_version'] ?? $config['datamachine_bundle']['template_version'] ?? $config['datamachine_bundle']['bundle_version'] ?? '' );
90+
$config['datamachine_bundle']['source_ref'] = (string) ( $this->bundle['source_ref'] ?? $config['datamachine_bundle']['source_ref'] ?? '' );
91+
$config['datamachine_bundle']['source_revision'] = (string) ( $this->bundle['source_revision'] ?? $config['datamachine_bundle']['source_revision'] ?? '' );
92+
93+
if ( ! isset( $config['datamachine_bundle']['artifacts'] ) || ! is_array( $config['datamachine_bundle']['artifacts'] ) ) {
94+
$config['datamachine_bundle']['artifacts'] = array();
95+
}
96+
97+
foreach ( $artifacts as $artifact ) {
98+
if ( ! $artifact instanceof \WP_Agent_Package_Installed_Artifact ) {
99+
continue;
100+
}
101+
102+
$row = $artifact->to_array();
103+
$type = AgentBundleUpgradePlanner::bundle_artifact_type( (string) $row['artifact_type'] );
104+
$id = (string) $row['artifact_id'];
105+
$key = AgentBundleArtifactExtensions::artifact_key( $type, $id );
106+
107+
$config['datamachine_bundle']['artifacts'][ $key ] = array(
108+
'bundle_slug' => (string) $config['datamachine_bundle']['bundle_slug'],
109+
'bundle_version' => (string) $config['datamachine_bundle']['bundle_version'],
110+
'artifact_type' => $type,
111+
'artifact_id' => $id,
112+
'source_path' => (string) ( $row['source'] ?? '' ),
113+
'installed_hash' => $row['installed_hash'] ?? null,
114+
'current_hash' => $row['current_hash'] ?? null,
115+
'installed_payload' => $row['installed_payload'] ?? null,
116+
'status' => AgentBundleArtifactStatus::classify( $row['installed_hash'] ?? null, $row['current_hash'] ?? null ),
117+
'installed_at' => (string) ( $row['installed_at'] ?? '' ),
118+
'updated_at' => (string) ( $row['updated_at'] ?? '' ),
119+
);
120+
}
121+
122+
return (bool) $agents->update_agent( $agent_id, array( 'agent_config' => $config ) );
123+
}
124+
125+
/**
126+
* @param array<int,array<string,mixed>> $artifacts Bundle artifact rows.
127+
* @return array<int,array<string,mixed>> Package artifact rows.
128+
*/
129+
public static function package_artifact_rows( array $artifacts ): array {
130+
$rows = array();
131+
foreach ( $artifacts as $artifact ) {
132+
if ( ! is_array( $artifact ) ) {
133+
continue;
134+
}
135+
136+
$type = (string) ( $artifact['artifact_type'] ?? '' );
137+
$id = (string) ( $artifact['artifact_id'] ?? '' );
138+
if ( '' === $type || '' === $id ) {
139+
continue;
140+
}
141+
142+
$rows[] = array_merge(
143+
$artifact,
144+
array(
145+
'artifact_type' => AgentBundleUpgradePlanner::package_artifact_type( $type ),
146+
'artifact_id' => $id,
147+
'source' => (string) ( $artifact['source_path'] ?? ( $artifact['source'] ?? '' ) ),
148+
)
149+
);
150+
}
151+
152+
return $rows;
153+
}
154+
}

0 commit comments

Comments
 (0)