checkpoint: pre-audit baseline state

This commit is contained in:
Dwindi Ramadhana
2026-06-06 00:29:10 +07:00
parent 579aab1b2b
commit ae70e4aea9
38 changed files with 4061 additions and 3149 deletions

View File

@@ -177,6 +177,78 @@ class WP_Agentic_Writer_Context_Service {
return $manager->update_messages( $session_id, $messages );
}
/**
* Get structured session context JSON.
*
* @since 0.2.3
* @param string $session_id Session ID.
* @return array Session context.
*/
public function get_session_context( $session_id ) {
$manager = WP_Agentic_Writer_Conversation_Manager::get_instance();
$session = $manager->get_session( $session_id );
if ( ! $session || empty( $session['context'] ) || ! is_array( $session['context'] ) ) {
return array();
}
return $session['context'];
}
/**
* Merge a context patch into the stored session context.
*
* @since 0.2.3
* @param string $session_id Session ID.
* @param array $patch Context fields to merge.
* @return bool Success.
*/
public function update_session_context( $session_id, $patch ) {
if ( empty( $session_id ) || ! is_array( $patch ) ) {
return false;
}
$manager = WP_Agentic_Writer_Conversation_Manager::get_instance();
$context = $this->get_session_context( $session_id );
$context = $this->merge_context_recursive( $context, $patch );
$context['updated_at'] = current_time( 'c' );
return $manager->update_context( $session_id, $context );
}
/**
* Append an item to an array inside session context.
*
* @since 0.2.3
* @param string $session_id Session ID.
* @param string $key Context key.
* @param array $item Item to append.
* @param int $limit Maximum retained items.
* @return bool Success.
*/
public function append_session_context_item( $session_id, $key, $item, $limit = 25 ) {
if ( empty( $session_id ) || empty( $key ) || ! is_array( $item ) ) {
return false;
}
$manager = WP_Agentic_Writer_Conversation_Manager::get_instance();
$context = $this->get_session_context( $session_id );
if ( empty( $context[ $key ] ) || ! is_array( $context[ $key ] ) ) {
$context[ $key ] = array();
}
$item['created_at'] = $item['created_at'] ?? current_time( 'c' );
$context[ $key ][] = $item;
if ( $limit > 0 && count( $context[ $key ] ) > $limit ) {
$context[ $key ] = array_slice( $context[ $key ], -1 * $limit );
}
$context['updated_at'] = current_time( 'c' );
return $manager->update_context( $session_id, $context );
}
/**
* Save plan to post meta.
*
@@ -254,6 +326,7 @@ class WP_Agentic_Writer_Context_Service {
'include_images' => true,
'web_search' => false,
'default_mode' => 'writing',
'focus_keyword' => '',
'seo_focus_keyword' => '',
'seo_secondary_keywords' => '',
'seo_meta_description' => '',
@@ -336,6 +409,26 @@ class WP_Agentic_Writer_Context_Service {
return md5( $role . ':' . $content );
}
/**
* Merge context arrays while preserving nested JSON objects.
*
* @since 0.2.3
* @param array $base Existing context.
* @param array $patch Context patch.
* @return array Merged context.
*/
private function merge_context_recursive( $base, $patch ) {
foreach ( $patch as $key => $value ) {
if ( is_array( $value ) && isset( $base[ $key ] ) && is_array( $base[ $key ] ) && ! wp_is_numeric_array( $value ) ) {
$base[ $key ] = $this->merge_context_recursive( $base[ $key ], $value );
} else {
$base[ $key ] = $value;
}
}
return $base;
}
/**
* Clear context for a session and post.
*
@@ -422,4 +515,4 @@ class WP_Agentic_Writer_Context_Service {
return array_merge( array( $context_summary ), $messages );
}
}
}