| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Agents: default agent definitions. |
| 4 |
* |
| 5 |
* Five ready-to-use agents seeded ONCE, and only on sites that have |
| 6 |
* no agents at all — an install that already built its own roster is |
| 7 |
* never touched (the seeded flag is set without creating anything). |
| 8 |
* Definitions are complete: role, ability allowlist, chat + send-to + |
| 9 |
* drag triggers, and full system prompts, so a fresh site can talk to |
| 10 |
* an agent the moment the feature flag turns on and a connector is |
| 11 |
* configured. Abilities that aren't registered on the site (the ai/* |
| 12 |
* family ships with the AI experiments plugin) are skipped by the |
| 13 |
* runner at tool-build time — allowlisting them here costs nothing |
| 14 |
* and lights them up when the provider plugin lands. |
| 15 |
* |
| 16 |
* @package OpenStation |
| 17 |
*/ |
| 18 |
|
| 19 |
defined( 'ABSPATH' ) || exit; |
| 20 |
|
| 21 |
/** |
| 22 |
* Option flag: defaults were seeded (or deliberately skipped). |
| 23 |
* |
| 24 |
* The VALUE keeps its pre-rebrand spelling on purpose: it is a |
| 25 |
* persisted or externally-visible identifier, so renaming it would |
| 26 |
* orphan data already written by live installs (or break a live |
| 27 |
* URL). The mismatch between this constant's name and its value is |
| 28 |
* deliberate — it is NOT a half-finished rename. |
| 29 |
*/ |
| 30 |
const OPENSTATION_AGENTS_DEFAULTS_SEEDED_OPTION = 'desktop_mode_agents_defaults_seeded'; |
| 31 |
|
| 32 |
require_once __DIR__ . '/default-definitions.php'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Seed the default agents. Runs once per site: the option flag is set |
| 36 |
* whether or not anything was created, and sites that already have |
| 37 |
* agents are left exactly as they are. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
function openstation_agents_seed_defaults() { |
| 42 |
if ( get_option( OPENSTATION_AGENTS_DEFAULTS_SEEDED_OPTION ) ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$existing = openstation_agent_get_agents(); |
| 47 |
if ( ! empty( $existing ) ) { |
| 48 |
update_option( OPENSTATION_AGENTS_DEFAULTS_SEEDED_OPTION, '1', false ); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
foreach ( openstation_agents_default_definitions() as $definition ) { |
| 53 |
$user = openstation_agent_create( |
| 54 |
array( |
| 55 |
'name' => $definition['name'], |
| 56 |
'role' => $definition['role'], |
| 57 |
'description' => $definition['description'], |
| 58 |
'instructions' => $definition['instructions'], |
| 59 |
'abilities' => $definition['abilities'], |
| 60 |
'vibes' => $definition['vibes'], |
| 61 |
'face' => $definition['face'], |
| 62 |
'faceSeed' => $definition['faceSeed'], |
| 63 |
) |
| 64 |
); |
| 65 |
if ( is_wp_error( $user ) ) { |
| 66 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 67 |
error_log( '[openstation] Default agent "' . $definition['name'] . '" failed to seed: ' . $user->get_error_message() ); |
| 68 |
continue; |
| 69 |
} |
| 70 |
openstation_agent_update( $user->ID, array( 'triggers' => $definition['triggers'] ) ); |
| 71 |
} |
| 72 |
|
| 73 |
update_option( OPENSTATION_AGENTS_DEFAULTS_SEEDED_OPTION, '1', false ); |
| 74 |
} |
| 75 |
/** |
| 76 |
* Hook wrapper — seed only on wp-admin requests by a user who could |
| 77 |
* create agents anyway. Keeps the seeder out of front-end requests, |
| 78 |
* cron, and the PHPUnit bootstrap (tests call the pure function). |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
function openstation_agents_maybe_seed_defaults() { |
| 83 |
if ( ! is_admin() || ! current_user_can( 'edit_users' ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
openstation_agents_seed_defaults(); |
| 87 |
} |
| 88 |
add_action( 'admin_init', 'openstation_agents_maybe_seed_defaults' ); |
| 89 |
|