Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions inc/Cli/Commands/AgentBundleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,10 @@ private function bundle_artifacts_for_agent( array $bundle, ?array $agent ): arr
);
}

foreach ( self::bundle_file_artifacts( $bundle ) as $artifact ) {
$artifacts[] = $artifact;
}

foreach ( AgentBundleArtifactExtensions::normalize_artifacts( is_array( $bundle['extension_artifacts'] ?? null ) ? $bundle['extension_artifacts'] : array() ) as $artifact ) {
$artifacts[] = $artifact;
}
Expand Down Expand Up @@ -868,6 +872,7 @@ private function current_artifacts( array $agent, array $installed ): array {

$artifacts = array_merge(
$artifacts,
\DataMachine\Engine\AI\System\SystemTaskPromptRegistry::current_artifacts(),
AgentBundleArtifactExtensions::current_artifacts(
$agent,
$installed,
Expand All @@ -878,6 +883,45 @@ private function current_artifacts( array $agent, array $installed ): array {
return $artifacts;
}

/** @return array<int,array<string,mixed>> */
private static function bundle_file_artifacts( array $bundle ): array {
$artifacts = array();
$files = is_array( $bundle['artifact_files'] ?? null ) ? $bundle['artifact_files'] : array();

foreach ( self::bundle_file_artifact_directories() as $directory => $type ) {
foreach ( is_array( $files[ $directory ] ?? null ) ? $files[ $directory ] : array() as $relative_path => $payload ) {
$artifact_id = is_array( $payload ) && is_string( $payload['artifact_id'] ?? null )
? (string) $payload['artifact_id']
: self::artifact_id_from_relative_path( (string) $relative_path );

$artifacts[] = array(
'artifact_type' => $type,
'artifact_id' => $artifact_id,
'source_path' => $directory . '/' . ltrim( (string) $relative_path, '/' ),
'payload' => $payload,
);
}
}

return $artifacts;
}

/** @return array<string,string> */
private static function bundle_file_artifact_directories(): array {
return array(
\DataMachine\Engine\Bundle\BundleSchema::PROMPTS_DIR => 'prompt',
\DataMachine\Engine\Bundle\BundleSchema::RUBRICS_DIR => 'rubric',
\DataMachine\Engine\Bundle\BundleSchema::TOOL_POLICIES_DIR => 'tool_policy',
\DataMachine\Engine\Bundle\BundleSchema::AUTH_REFS_DIR => 'auth_ref',
\DataMachine\Engine\Bundle\BundleSchema::SEED_QUEUES_DIR => 'seed_queue',
);
}

private static function artifact_id_from_relative_path( string $relative_path ): string {
$relative_path = preg_replace( '/\.(json|md|txt)$/i', '', $relative_path );
return null === $relative_path ? '' : $relative_path;
}

private function pipeline_payload( array $pipeline, string $portable_slug ): array {
return array(
'portable_slug' => $portable_slug,
Expand Down
83 changes: 81 additions & 2 deletions inc/Core/Agents/AgentBundler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
use DataMachine\Engine\Bundle\AgentPackageProjection;
use DataMachine\Engine\Bundle\BundleValidationException;
use DataMachine\Engine\Bundle\PortableSlug;
use DataMachine\Engine\Bundle\PromptArtifact;
use DataMachine\Core\Steps\FlowStepConfig;
use DataMachine\Engine\AI\System\SystemTaskPromptRegistry;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -221,6 +223,9 @@ public function export_directory_object( string $slug, array $context = array()

$pipeline_slugs = array_map( fn( AgentBundlePipelineFile $pipeline ) => $pipeline->slug(), $pipeline_documents );
$flow_slugs = array_map( fn( AgentBundleFlowFile $flow ) => $flow->slug(), $flow_documents );
$artifact_files = array(
\DataMachine\Engine\Bundle\BundleSchema::PROMPTS_DIR => SystemTaskPromptRegistry::bundle_prompt_files(),
);
$extension_paths = array_map( static fn( array $artifact ) => (string) ( $artifact['source_path'] ?? '' ), $extension_artifacts );
$manifest = new AgentBundleManifest(
gmdate( 'c' ),
Expand All @@ -239,13 +244,14 @@ public function export_directory_object( string $slug, array $context = array()
'memory' => array_keys( $memory_files ),
'pipelines' => $pipeline_slugs,
'flows' => $flow_slugs,
'prompts' => array_keys( $artifact_files[ \DataMachine\Engine\Bundle\BundleSchema::PROMPTS_DIR ] ),
'extensions' => array_values( array_filter( $extension_paths ) ),
'handler_auth' => $handler_auth,
)
);

$extras = self::collect_export_extras( $agent_id, $agent );
$directory = new AgentBundleDirectory( $manifest, $memory_files, $pipeline_documents, $flow_documents, array(), $extension_artifacts, $extras );
$directory = new AgentBundleDirectory( $manifest, $memory_files, $pipeline_documents, $flow_documents, $artifact_files, $extension_artifacts, $extras );

return array(
'success' => true,
Expand Down Expand Up @@ -933,7 +939,41 @@ public function import( array $bundle, ?string $new_slug = null, int $owner_id =
);
}

// 6. Apply plugin-owned artifacts through their owning plugin.
// 6. Apply bundle-owned prompt artifacts without touching local overrides.
foreach ( self::bundle_file_artifacts( $bundle ) as $artifact ) {
if ( PromptArtifact::TYPE_PROMPT !== (string) $artifact['artifact_type'] ) {
continue;
}

$artifact_key = self::artifact_key( (string) $artifact['artifact_type'], (string) $artifact['artifact_id'] );
if ( SystemTaskPromptRegistry::has_local_override_for_artifact( $artifact ) ) {
$conflicts[] = array(
'artifact_type' => $artifact['artifact_type'],
'artifact_id' => $artifact['artifact_id'],
'reason' => 'local_modified',
);
continue;
}

if ( ! SystemTaskPromptRegistry::apply_bundle_artifact( $artifact ) ) {
$conflicts[] = array(
'artifact_type' => $artifact['artifact_type'],
'artifact_id' => $artifact['artifact_id'],
'reason' => 'missing_apply_handler',
);
continue;
}

$artifact_records[ $artifact_key ] = $this->bundle_artifact_record(
$bundle_metadata,
(string) $artifact['artifact_type'],
(string) $artifact['artifact_id'],
(string) $artifact['source_path'],
$artifact['payload'] ?? null
);
}

// 7. Apply plugin-owned artifacts through their owning plugin.
$agent_context = array_merge(
$agent_data,
array(
Expand Down Expand Up @@ -1320,6 +1360,45 @@ private function bundle_artifact_record( array $bundle_metadata, string $type, s
);
}

/** @return array<int,array<string,mixed>> */
private static function bundle_file_artifacts( array $bundle ): array {
$artifacts = array();
$files = is_array( $bundle['artifact_files'] ?? null ) ? $bundle['artifact_files'] : array();

foreach ( self::bundle_file_artifact_directories() as $directory => $type ) {
foreach ( is_array( $files[ $directory ] ?? null ) ? $files[ $directory ] : array() as $relative_path => $payload ) {
$artifact_id = is_array( $payload ) && is_string( $payload['artifact_id'] ?? null )
? (string) $payload['artifact_id']
: self::artifact_id_from_relative_path( (string) $relative_path );

$artifacts[] = array(
'artifact_type' => $type,
'artifact_id' => $artifact_id,
'source_path' => $directory . '/' . ltrim( (string) $relative_path, '/' ),
'payload' => $payload,
);
}
}

return $artifacts;
}

/** @return array<string,string> */
private static function bundle_file_artifact_directories(): array {
return array(
\DataMachine\Engine\Bundle\BundleSchema::PROMPTS_DIR => 'prompt',
\DataMachine\Engine\Bundle\BundleSchema::RUBRICS_DIR => 'rubric',
\DataMachine\Engine\Bundle\BundleSchema::TOOL_POLICIES_DIR => 'tool_policy',
\DataMachine\Engine\Bundle\BundleSchema::AUTH_REFS_DIR => 'auth_ref',
\DataMachine\Engine\Bundle\BundleSchema::SEED_QUEUES_DIR => 'seed_queue',
);
}

private static function artifact_id_from_relative_path( string $relative_path ): string {
$relative_path = preg_replace( '/\.(json|md|txt)$/i', '', $relative_path );
return null === $relative_path ? '' : $relative_path;
}

/** @param array<int,array<string,mixed>> $artifacts */
private static function index_artifacts( array $artifacts ): array {
$indexed = array();
Expand Down
Loading
Loading