| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Agents: "Agent chat" native window. |
| 4 |
* |
| 5 |
* Lazy-loaded native window the Agents section opens to talk to an |
| 6 |
* agent (the chat trigger). The window is a shell — the dedicated |
| 7 |
* `agent-run-window` bundle registers the render callback on |
| 8 |
* `window.openStationNativeWindows['desktop-mode-agent-run']`, |
| 9 |
* subscribes to the cross-bundle `desktop-mode/agents-run` shared |
| 10 |
* store, and paints the conversation for whichever agent the opener |
| 11 |
* selected. |
| 12 |
* |
| 13 |
* @package OpenStation |
| 14 |
*/ |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* Inline SVG bot icon — byte-identical to the agent avatar so the |
| 20 |
* motif is consistent everywhere agents appear. |
| 21 |
* |
| 22 |
* @return string Data URI. |
| 23 |
*/ |
| 24 |
function openstation_agent_run_window_icon() { |
| 25 |
return openstation_agent_avatar_url(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the bundle script + style handles. Lazy-loaded by the |
| 30 |
* native-window sync the first time the window opens, same as the |
| 31 |
* recycle-bin and posts-window modules. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
function openstation_agent_run_register_assets() { |
| 36 |
$version = OPENSTATION_VERSION; |
| 37 |
$suffix = openstation_asset_suffix(); |
| 38 |
|
| 39 |
$css_path = OPENSTATION_DIR . 'assets/css/agents.css'; |
| 40 |
wp_register_style( |
| 41 |
'desktop-mode-agent-run', |
| 42 |
OPENSTATION_URL . 'assets/css/agents.css', |
| 43 |
array( 'os-variables', 'dashicons' ), |
| 44 |
file_exists( $css_path ) ? (string) filemtime( $css_path ) : $version |
| 45 |
); |
| 46 |
|
| 47 |
$js_path = OPENSTATION_DIR . 'assets/js/agent-run-window' . $suffix . '.js'; |
| 48 |
wp_register_script( |
| 49 |
'desktop-mode-agent-run', |
| 50 |
OPENSTATION_URL . 'assets/js/agent-run-window' . $suffix . '.js', |
| 51 |
array( 'wp-i18n' ), |
| 52 |
file_exists( $js_path ) ? (string) filemtime( $js_path ) : $version, |
| 53 |
true |
| 54 |
); |
| 55 |
wp_set_script_translations( |
| 56 |
'desktop-mode-agent-run', |
| 57 |
'desktop-mode', |
| 58 |
OPENSTATION_DIR . 'languages' |
| 59 |
); |
| 60 |
} |
| 61 |
add_action( 'init', 'openstation_agent_run_register_assets', 5 ); |
| 62 |
|
| 63 |
/** |
| 64 |
* Static template rendered into the window body — the bundle mounts |
| 65 |
* its UI into `[data-os-agent-run-root]`. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
function openstation_agent_run_render_template() { |
| 70 |
?> |
| 71 |
<div class="desktop-mode-agent-run" data-os-agent-run-root> |
| 72 |
<div class="os-agent-run__loading"> |
| 73 |
<os-spinner></os-spinner> |
| 74 |
</div> |
| 75 |
</div> |
| 76 |
<?php |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Register the native window on `init` priority 25 — after the |
| 81 |
* registries boot. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
function openstation_agent_run_window_register() { |
| 86 |
if ( ! function_exists( 'openstation_register_window' ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
if ( ! openstation_agents_user_can_read() ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$registered = openstation_register_window( |
| 94 |
'desktop-mode-agent-run', |
| 95 |
array( |
| 96 |
'title' => __( 'Agent chat', 'desktop-mode' ), |
| 97 |
'icon' => openstation_agent_run_window_icon(), |
| 98 |
'template' => 'openstation_agent_run_render_template', |
| 99 |
'script' => 'desktop-mode-agent-run', |
| 100 |
'styles' => array( 'desktop-mode-agent-run' ), |
| 101 |
'width' => 760, |
| 102 |
'height' => 620, |
| 103 |
'min_width' => 540, |
| 104 |
'min_height' => 380, |
| 105 |
'placement' => 'none', |
| 106 |
// Chat invocations should always surface a visible window, |
| 107 |
// not race a focused window into the background. |
| 108 |
'autofocus' => true, |
| 109 |
'config' => array( |
| 110 |
'restRoot' => esc_url_raw( rest_url() ), |
| 111 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 112 |
'canManage' => openstation_agents_user_can_manage(), |
| 113 |
// The viewer — the chat paints their avatar next to |
| 114 |
// their own messages, WhatsApp-style. |
| 115 |
'currentUser' => array( |
| 116 |
'id' => (int) get_current_user_id(), |
| 117 |
'name' => (string) wp_get_current_user()->display_name, |
| 118 |
'avatarUrl' => (string) get_avatar_url( get_current_user_id(), array( 'size' => 96 ) ), |
| 119 |
), |
| 120 |
), |
| 121 |
) |
| 122 |
); |
| 123 |
if ( is_wp_error( $registered ) ) { |
| 124 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 125 |
error_log( '[openstation] Agent chat window registration failed: ' . $registered->get_error_message() ); |
| 126 |
} |
| 127 |
} |
| 128 |
add_action( 'init', 'openstation_agent_run_window_register', 25 ); |
| 129 |
|