init(); (new Agents\Summarize())->init(); } public function adminPageAdd() { add_submenu_page( ALPACA_BOT, 'Agents', 'Agents', 'manage_options', Options::appendPrefix('agents', '-'), [$this, 'adminPageRender'], 2 ); } // adminPageRender using accordion from pulling $this->getAgents() public function adminPageRender() { // HTML echo '
'; echo '

Agents

'; echo '

Agents are shortcodes that help Alpaca Bot perform tasks.

'; echo '
'; $agents = $this->getAgents(); foreach ($agents as $slug => $agent) { $icon = $agent['icon'] ?? 'person_apron'; echo ''; echo '
'; if (isset($agent['arguments'])) { echo '

Arguments

'; echo '
    '; foreach ($agent['arguments'] as $name => $arg) { echo '
  • ' . esc_html($name) . ' ' . esc_html($arg['type']) . ' ' . wp_kses($arg['description'], Options::getAllowedTags('p')) . '
  • '; } echo '
'; } if (isset($agent['examples'])) { echo '

Examples

'; foreach ($agent['examples'] as $example) { echo '
' . esc_html($example[0]) . '
'; echo '

' . wp_kses($example[1], Options::getAllowedTags('p')) . '

'; echo '
'; } } if (isset($agent['references'])) { echo '

References

'; echo ''; } echo '
'; } echo '
'; echo '
'; } public function getAgents() { $this->agents = array_merge($this->getCoreAgents(), $this->getCustomAgents()); return $this->agents; } public function getCoreAgents() { return apply_filters(Options::appendPrefix('core_agents'), []); } public function getCustomAgents() { return apply_filters(Options::appendPrefix('custom_agents'), []); } public function hookUserPrompt($prompt) { $out = do_shortcode($prompt); return $out; } public function router($atts, $content = '', $tag = '') { // Do not process during autosave (this may not be necessary) if (defined('DOING_AUTOSAVE') and DOING_AUTOSAVE) { return; } // Do not process during post save if (current_action() === 'render_block_core/shortcode' and did_action('save_post') >= 1) { return; } $cache = new Cache($atts, $content, $tag); $response = $cache->get(); if ($response) { return $response; } switch ($tag) { case 'alpacabot_agent': $response = $this->routerAgent($atts, $content, $tag); break; case 'alpacabot': $response = $this->routerAlpaca($atts, $content, $tag); break; default: return 'Error: Tag not found.'; } if (Options::validateValue($response)) { $cache->set($response); return $response; } else { return 'Error: No content returned.'; } return $response; } public function routerAgent($atts, $content = '', $tag = '') { $agents = $this->getAgents(); // Check if agent is the first argument if (isset($agents[$atts[0] ?? null])) { $agent = $agents[$atts[0]]; } elseif (isset($agents[$atts['agent'] ?? null])) { $agent = $agents[$atts['agent']]; } elseif (isset($agents[$atts['name'] ?? null])) { $agent = $agents[$atts['name']]; } else { return 'Error: Agent not found.'; } // set defaults foreach ($agent['arguments'] as $name => $arg) { if (isset($arg['default'])) { $atts[$name] = $atts[$name] ?? $arg['default']; } } // if valid callback $agent['callback'] if (is_callable($agent['callback'])) { $content = call_user_func($agent['callback'], $atts, $content); } return $content; } public function routerAlpaca($atts, $content = '', $tag = '') { $def = [ 'model' => Options::get('default_model'), 'prompt' => do_shortcode($content), ]; $atts = wp_parse_args($atts, $def); $ollama = new Ollama(); return $ollama->apiGenerate($atts); } }