定制 roots/acorn-ai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

roots/acorn-ai

Composer 安装命令:

composer require roots/acorn-ai

包简介

WordPress Abilities API integration and AI support for Acorn.

README 文档

README

Packagist Downloads Follow Roots Sponsor Roots

AI support for Acorn — wraps laravel/ai and adds first-class integration with the WordPress Abilities API.

Support us

Roots is an independent open source org, supported only by developers like you. Your sponsorship funds WP Packages and the entire Roots ecosystem, and keeps them independent. Support us by purchasing Radicle or sponsoring us on GitHub — sponsors get access to our private Discord.

Requirements

  • PHP 8.4+
  • WordPress 6.9+ (for Abilities API)
  • Acorn 5.0+

Installation

composer require roots/acorn-ai

Publish the config files:

# WordPress-specific config (abilities registration)
wp acorn vendor:publish --provider="Roots\AcornAi\AcornAiServiceProvider" --tag=ai-wordpress-config

# AI provider config (providers, API keys, defaults)
wp acorn vendor:publish --provider="Laravel\Ai\AiServiceProvider" --tag=ai-config

Add your provider API keys to .env:

OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...

WordPress AI Connectors

API keys configured through laravel/ai are automatically passed to WordPress AI provider plugins via environment variables. This means you don't need to enter keys on the WordPress Connectors settings page, and your keys are never stored in wp_options.

Supported providers: OpenAI, Anthropic, and Gemini (Google).

AI Providers & Agents

roots/acorn-ai includes laravel/ai and makes it available inside WordPress via Acorn's container. You get the full laravel/ai feature set: agents, tools, embeddings, image generation, audio, transcription, and reranking — all configured through config/ai.php (published from laravel/ai).

Creating an agent

wp acorn make:agent SupportAgent

Creating a tool

wp acorn make:tool SearchPosts

Refer to the laravel/ai documentation for full usage of agents, tools, structured output, and providers.

Abilities

The WordPress Abilities API (WordPress 6.9+) provides a standardized way to define capabilities that AI agents can invoke — via REST API (wp-abilities/v1/abilities/{name}/run) and the WordPress MCP Adapter.

roots/acorn-ai wraps ability registration so that your ability classes are resolved through Laravel's service container, giving you full dependency injection.

Creating an ability

wp acorn make:ability CreatePost

This generates app/Ai/Abilities/CreatePostAbility.php:

namespace App\Ai\Abilities;

use Roots\AcornAi\Abilities\Ability;

class CreatePostAbility extends Ability
{
    public function __construct(private PostRepository $posts) {}

    public function label(): string
    {
        return 'Create Post';
    }

    public function description(): string
    {
        return 'Creates a new WordPress post with the given title and content.';
    }

    public function execute(array $input): mixed
    {
        return $this->posts->create($input['title'], $input['content'] ?? '');
    }

    public function inputSchema(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'title' => ['type' => 'string', 'description' => 'The post title.'],
                'content' => ['type' => 'string', 'description' => 'The post body.'],
            ],
            'required' => ['title'],
        ];
    }
}

Registering abilities

Add your ability classes to config/ai-wordpress.php:

'abilities' => [
    \App\Ai\Abilities\CreatePostAbility::class,
],

Ability names

The default name is derived from the root namespace and class name:

\App\Ai\Abilities\CreatePostAbility  →  "app/create-post"

Override name() to use a custom name:

public function name(): string
{
    return 'app/create-post';
}

Permissions

Override permission() to control access. Return true, false, or a WP_Error:

public function permission(): bool|\WP_Error
{
    return current_user_can('edit_posts');
}

MCP Adapter

To expose an ability as an MCP tool when the WordPress MCP Adapter plugin is active, set mcp.public in meta():

public function meta(): array
{
    return [
        'mcp' => ['public' => true],
    ];
}

Listing abilities

wp acorn ability:list

Example: Post summarization via AI

This example shows a realistic ability that uses laravel/ai under the hood to summarize a WordPress post, exposes it over REST, and makes it available as an MCP tool for AI agents like Claude or Cursor.

Generate the class:

wp acorn make:ability SummarizePost

app/Ai/Abilities/SummarizePostAbility.php:

namespace App\Ai\Abilities;

use Illuminate\Support\Facades\Cache;
use Laravel\Ai\AnonymousAgent;
use Roots\AcornAi\Abilities\Ability;

class SummarizePostAbility extends Ability
{
    public function name(): string
    {
        return 'app/summarize-post';
    }

    public function label(): string
    {
        return 'Summarize Post';
    }

    public function description(): string
    {
        return 'Generates a concise summary of a WordPress post given its ID. '
            . 'Use this when you need a brief overview of a post without retrieving its full content.';
    }

    public function execute(array $input): mixed
    {
        $post = get_post($input['post_id']);

        if (! $post || $post->post_status !== 'publish') {
            return new \WP_Error('not_found', 'Post not found.');
        }

        return Cache::remember("ai-summary-{$post->ID}", now()->addDay(), function () use ($post) {
            $content = wp_strip_all_tags($post->post_content);

            return AnonymousAgent::make(
                instructions: 'You are a helpful assistant that summarizes blog posts in 2-3 sentences.',
                messages: [],
                tools: [],
            )->prompt("Summarize this post:\n\n{$content}")->text;
        });
    }

    public function permission(): bool|\WP_Error
    {
        return is_user_logged_in();
    }

    public function inputSchema(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'post_id' => [
                    'type' => 'integer',
                    'description' => 'The ID of the post to summarize.',
                ],
            ],
            'required' => ['post_id'],
        ];
    }

    public function meta(): array
    {
        return [
            'mcp' => ['public' => true],
        ];
    }
}

Register it in config/ai-wordpress.php:

'abilities' => [
    \App\Ai\Abilities\SummarizePostAbility::class,
],

What this gives you:

  • REST API — any authenticated client can call POST /wp-json/wp-abilities/v1/abilities/app/summarize-post/run with {"post_id": 42}
  • MCP tool — when the WordPress MCP Adapter is active, the ability is surfaced to connected AI agents automatically
  • Caching — summaries are cached for 24 hours so repeated calls don't burn API credits
  • Container injection — swap AnonymousAgent for a dedicated agent class or any injected service by adding constructor dependencies to the ability

Community

Keep track of development and community news.

roots/acorn-ai 适用场景与选型建议

roots/acorn-ai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.73k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2026 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「ai」 「abilities」 「acorn」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 roots/acorn-ai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 roots/acorn-ai 我们能提供哪些服务?
定制开发 / 二次开发

基于 roots/acorn-ai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-25