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
12 changes: 11 additions & 1 deletion data-machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,15 @@ function datamachine_allow_json_upload( $mimes ) {

add_action( 'update_option_datamachine_settings', array( \DataMachine\Core\PluginSettings::class, 'clearCache' ) );

add_action(
'plugins_loaded',
function () {
\DataMachine\Core\Database\Chat\Chat::ensure_context_column();
\DataMachine\Core\Database\Chat\Chat::ensure_agent_id_column();
},
6
);

register_activation_hook( __FILE__, 'datamachine_activate_plugin' );
register_deactivation_hook( __FILE__, 'datamachine_deactivate_plugin' );

Expand Down Expand Up @@ -454,6 +463,7 @@ function datamachine_activate_for_site() {
$db_processed_items->create_table();

\DataMachine\Core\Database\Chat\Chat::create_table();
\DataMachine\Core\Database\Chat\Chat::ensure_context_column();
\DataMachine\Core\Database\Chat\Chat::ensure_agent_id_column();

// Ensure default agent memory files exist.
Expand Down Expand Up @@ -509,7 +519,7 @@ function datamachine_resolve_or_create_agent_id( int $user_id ): int {

$agent_slug = sanitize_title( (string) $user->user_login );
$agent_name = (string) $user->display_name;
$agent_model = \DataMachine\Core\PluginSettings::getAgentModel( 'chat' );
$agent_model = \DataMachine\Core\PluginSettings::getContextModel( 'chat' );

return $agents_repo->create_if_missing(
$agent_slug,
Expand Down
16 changes: 8 additions & 8 deletions inc/Abilities/Chat/CreateChatSessionAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ private function registerAbility(): void {
'type' => 'integer',
'description' => __( 'First-class agent ID for this session.', 'data-machine' ),
),
'agent_type' => array(
'context' => array(
'type' => 'string',
'default' => 'chat',
'description' => __( 'Agent type (chat, pipeline, system).', 'data-machine' ),
'description' => __( 'Execution context (chat, pipeline, system, standalone).', 'data-machine' ),
),
'source' => array(
'type' => 'string',
Expand Down Expand Up @@ -90,7 +90,7 @@ private function registerAbility(): void {
/**
* Execute create-chat-session ability.
*
* @param array $input Input parameters with user_id, optional agent_type, source, metadata.
* @param array $input Input parameters with user_id, optional context, source, metadata.
* @return array Result with session_id on success.
*/
public function execute( array $input ): array {
Expand All @@ -101,10 +101,10 @@ public function execute( array $input ): array {
);
}

$user_id = (int) $input['user_id'];
$agent_id = (int) ( $input['agent_id'] ?? 0 );
$agent_type = ! empty( $input['agent_type'] ) ? sanitize_text_field( $input['agent_type'] ) : 'chat';
$source = ! empty( $input['source'] ) ? sanitize_text_field( $input['source'] ) : null;
$user_id = (int) $input['user_id'];
$agent_id = (int) ( $input['agent_id'] ?? 0 );
$context = ! empty( $input['context'] ) ? sanitize_text_field( $input['context'] ) : 'chat';
$source = ! empty( $input['source'] ) ? sanitize_text_field( $input['source'] ) : null;

if ( ! $this->can_access_user_sessions( $user_id ) ) {
return array(
Expand All @@ -127,7 +127,7 @@ public function execute( array $input ): array {
$session_metadata = array_merge( $session_metadata, $input['metadata'] );
}

$session_id = $this->chat_db->create_session( $user_id, $agent_id, $session_metadata, $agent_type );
$session_id = $this->chat_db->create_session( $user_id, $agent_id, $session_metadata, $context );

if ( empty( $session_id ) ) {
return array(
Expand Down
26 changes: 13 additions & 13 deletions inc/Abilities/Chat/ListChatSessionsAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* List Chat Sessions Ability
*
* Lists chat sessions for a given user with pagination and agent type filtering.
* Lists chat sessions for a given user with pagination and context filtering.
*
* @package DataMachine\Abilities\Chat
* @since 0.31.0
Expand Down Expand Up @@ -35,7 +35,7 @@ private function registerAbility(): void {
'datamachine/list-chat-sessions',
array(
'label' => __( 'List Chat Sessions', 'data-machine' ),
'description' => __( 'List chat sessions for a user with pagination and agent type filtering.', 'data-machine' ),
'description' => __( 'List chat sessions for a user with pagination and context filtering.', 'data-machine' ),
'category' => 'datamachine',
'input_schema' => array(
'type' => 'object',
Expand All @@ -58,9 +58,9 @@ private function registerAbility(): void {
'default' => 0,
'description' => __( 'Pagination offset.', 'data-machine' ),
),
'agent_type' => array(
'context' => array(
'type' => 'string',
'description' => __( 'Agent type filter (chat, pipeline, system).', 'data-machine' ),
'description' => __( 'Context filter (chat, pipeline, system, standalone).', 'data-machine' ),
),
),
'required' => array( 'user_id' ),
Expand All @@ -76,7 +76,7 @@ private function registerAbility(): void {
'total' => array( 'type' => 'integer' ),
'limit' => array( 'type' => 'integer' ),
'offset' => array( 'type' => 'integer' ),
'agent_type' => array( 'type' => 'string' ),
'context' => array( 'type' => 'string' ),
'error' => array( 'type' => 'string' ),
),
),
Expand All @@ -103,7 +103,7 @@ private function registerAbility(): void {
/**
* Execute list-chat-sessions ability.
*
* @param array $input Input parameters with user_id, optional limit, offset, agent_type.
* @param array $input Input parameters with user_id, optional limit, offset, context.
* @return array Result with sessions list and total count.
*/
public function execute( array $input ): array {
Expand All @@ -123,21 +123,21 @@ public function execute( array $input ): array {
);
}

$limit = min( 100, max( 1, (int) ( $input['limit'] ?? 20 ) ) );
$offset = max( 0, (int) ( $input['offset'] ?? 0 ) );
$agent_type = ! empty( $input['agent_type'] ) ? sanitize_text_field( $input['agent_type'] ) : null;
$agent_id = isset( $input['agent_id'] ) && is_numeric( $input['agent_id'] ) ? (int) $input['agent_id'] : null;
$limit = min( 100, max( 1, (int) ( $input['limit'] ?? 20 ) ) );
$offset = max( 0, (int) ( $input['offset'] ?? 0 ) );
$context = ! empty( $input['context'] ) ? sanitize_text_field( $input['context'] ) : null;
$agent_id = isset( $input['agent_id'] ) && is_numeric( $input['agent_id'] ) ? (int) $input['agent_id'] : null;

$sessions = $this->chat_db->get_user_sessions( $user_id, $limit, $offset, $agent_type, $agent_id );
$total = $this->chat_db->get_user_session_count( $user_id, $agent_type, $agent_id );
$sessions = $this->chat_db->get_user_sessions( $user_id, $limit, $offset, $context, $agent_id );
$total = $this->chat_db->get_user_session_count( $user_id, $context, $agent_id );

return array(
'success' => true,
'sessions' => $sessions,
'total' => $total,
'limit' => $limit,
'offset' => $offset,
'agent_type' => $agent_type,
'context' => $context,
);
}
}
13 changes: 11 additions & 2 deletions inc/Abilities/InternalLinkingAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ public static function queueInternalLinking( array $input ): array {
$dry_run = ! empty( $input['dry_run'] );
$force = ! empty( $input['force'] );

$system_defaults = PluginSettings::getAgentModel( 'system' );
$user_id = get_current_user_id();
$agent_id = function_exists( 'datamachine_resolve_or_create_agent_id' ) && $user_id > 0 ? datamachine_resolve_or_create_agent_id( $user_id ) : 0;
$system_defaults = PluginSettings::resolveModelForAgentContext( $agent_id, 'system' );
$provider = $system_defaults['provider'];
$model = $system_defaults['model'];

Expand Down Expand Up @@ -449,7 +451,14 @@ public static function queueInternalLinking( array $input ): array {
}

$systemAgent = SystemAgent::getInstance();
$batch = $systemAgent->scheduleBatch( 'internal_linking', $item_params );
$batch = $systemAgent->scheduleBatch(
'internal_linking',
$item_params,
array(
'user_id' => $user_id,
'agent_id' => $agent_id,
)
);

if ( false === $batch ) {
return array(
Expand Down
21 changes: 18 additions & 3 deletions inc/Abilities/Media/AltTextAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ public static function generateAltText( array $input ): array {
$post_id = absint( $input['post_id'] ?? 0 );
$force = ! empty( $input['force'] );

$system_defaults = PluginSettings::getAgentModel( 'system' );
$user_id = get_current_user_id();
$agent_id = function_exists( 'datamachine_resolve_or_create_agent_id' ) && $user_id > 0 ? datamachine_resolve_or_create_agent_id( $user_id ) : 0;
$system_defaults = PluginSettings::resolveModelForAgentContext( $agent_id, 'system' );
$provider = $system_defaults['provider'];
$model = $system_defaults['model'];

Expand Down Expand Up @@ -242,7 +244,14 @@ public static function generateAltText( array $input ): array {
}

$systemAgent = SystemAgent::getInstance();
$batch = $systemAgent->scheduleBatch( 'alt_text_generation', $item_params );
$batch = $systemAgent->scheduleBatch(
'alt_text_generation',
$item_params,
array(
'user_id' => $user_id,
'agent_id' => $agent_id,
)
);

if ( false === $batch ) {
return array(
Expand Down Expand Up @@ -352,7 +361,9 @@ public function queueAttachmentAltText( int $attachment_id ): void {
return;
}

$system_defaults = PluginSettings::getAgentModel( 'system' );
$user_id = get_current_user_id();
$agent_id = function_exists( 'datamachine_resolve_or_create_agent_id' ) && $user_id > 0 ? datamachine_resolve_or_create_agent_id( $user_id ) : 0;
$system_defaults = PluginSettings::resolveModelForAgentContext( $agent_id, 'system' );
$provider = $system_defaults['provider'];
$model = $system_defaults['model'];

Expand All @@ -375,6 +386,10 @@ public function queueAttachmentAltText( int $attachment_id ): void {
'attachment_id' => $attachment_id,
'force' => false,
'source' => 'add_attachment',
),
array(
'user_id' => $user_id,
'agent_id' => $agent_id,
)
);
}
Expand Down
8 changes: 6 additions & 2 deletions inc/Abilities/Media/ImageGenerationAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ public static function generateImage( array $input ): array {
* @return string|null Refined prompt on success, null on failure.
*/
public static function refine_prompt( string $raw_prompt, string $post_context = '', array $config = array() ): ?string {
$system_defaults = PluginSettings::getAgentModel( 'system' );
$user_id = get_current_user_id();
$agent_id = function_exists( 'datamachine_resolve_or_create_agent_id' ) && $user_id > 0 ? datamachine_resolve_or_create_agent_id( $user_id ) : 0;
$system_defaults = PluginSettings::resolveModelForAgentContext( $agent_id, 'system' );
$provider = $system_defaults['provider'];
$model = $system_defaults['model'];

Expand Down Expand Up @@ -383,7 +385,9 @@ public static function is_refinement_enabled( array $config = array() ): bool {
}

// Must have a DM AI provider configured.
$system_defaults = PluginSettings::getAgentModel( 'system' );
$user_id = get_current_user_id();
$agent_id = function_exists( 'datamachine_resolve_or_create_agent_id' ) && $user_id > 0 ? datamachine_resolve_or_create_agent_id( $user_id ) : 0;
$system_defaults = PluginSettings::resolveModelForAgentContext( $agent_id, 'system' );

return ! empty( $system_defaults['provider'] ) && ! empty( $system_defaults['model'] );
}
Expand Down
2 changes: 1 addition & 1 deletion inc/Abilities/PipelineStepAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public function executeAddPipelineStep( array $input ): array {
);

if ( 'ai' === $step_type ) {
$pipeline_defaults = PluginSettings::getAgentModel( 'pipeline' );
$pipeline_defaults = PluginSettings::getContextModel( 'pipeline' );
$new_step['provider'] = $pipeline_defaults['provider'];
$new_step['model'] = $pipeline_defaults['model'];
}
Expand Down
13 changes: 11 additions & 2 deletions inc/Abilities/SEO/MetaDescriptionAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public static function generateMetaDescriptions( array $input ): array {
$limit = absint( $input['limit'] ?? 50 );
$force = ! empty( $input['force'] );

$system_defaults = PluginSettings::getAgentModel( 'system' );
$user_id = get_current_user_id();
$agent_id = function_exists( 'datamachine_resolve_or_create_agent_id' ) && $user_id > 0 ? datamachine_resolve_or_create_agent_id( $user_id ) : 0;
$system_defaults = PluginSettings::resolveModelForAgentContext( $agent_id, 'system' );
$provider = $system_defaults['provider'];
$model = $system_defaults['model'];

Expand Down Expand Up @@ -203,7 +205,14 @@ public static function generateMetaDescriptions( array $input ): array {
}

$systemAgent = SystemAgent::getInstance();
$batch = $systemAgent->scheduleBatch( 'meta_description_generation', $item_params );
$batch = $systemAgent->scheduleBatch(
'meta_description_generation',
$item_params,
array(
'user_id' => $user_id,
'agent_id' => $agent_id,
)
);

if ( false === $batch ) {
return array(
Expand Down
Loading
Loading