llm-agents/agent-symfony-console

简单的代理,可以根据用户输入处理 Symfony Console 命令

1.1.0 2024-09-07 08:48 UTC

This package is auto-updated.

Last update: 2024-09-07 08:49:03 UTC


README

PHP Latest Version on Packagist Total Downloads

欢迎来到 Symfony Console 代理,这是将自然语言转化为命令行魔法的最佳伙伴。这个神奇的代理理解你的人类胡言乱语并将其转化为控制台命令。就像拥有一个超级智能的 CLI 助手,就在你的指尖!

这是怎么回事?🤔

你是否希望只需用简单的英语告诉你的控制台要做什么?现在你可以做到了!这个代理接受你的随意请求,并确定要运行哪个 Symfony 命令。这对于刚加入项目、忘记确切命令语法或只是有点懒的开发者来说非常合适(我们都有过这样的经历)。

这里有一个简短的视频来展示它是如何工作的

Symfony Console Agent Demo

需求 📋

  • PHP 8.3 或更高版本(我们生活在未来,宝贝!)
  • Symfony Console 组件
  • 幽默感(可选,但强烈推荐)

安装

首先,确保你已经安装了 Composer。然后,运行以下命令将 Symfony Console 代理添加到你的项目中

composer require llm-agents/agent-symfony-console

Spiral 框架中的设置

  1. 打开你的 app/src/Application/Kernel.php 文件。
  2. 添加引导程序如下
    public function defineBootloaders(): array
    {
        return [
            // ... other bootloaders ...
            \LLM\Agents\Agent\SymfonyConsole\Integrations\Spiral\SymfonyConsoleBootloader::class,
        ];
    }

就是这样!你的 Spiral 应用现在可以开始使用代理了。

Laravel

对于 Laravel,你可以使用一个服务提供者

// bootstrap/providers.php
return [
    // ... other providers ...
    LLM\Agents\Agent\SymfonyConsole\Integrations\Laravel\SymfonyConsoleServiceProvider::class,
];

用法

要开始使用 Symfony Console 代理,你需要实现 LLM\Agents\Agent\SymfonyConsole\CommandManagerInterface 接口。该接口负责获取命令帮助、列出可用命令和执行命令。

以下是如何在 Laravel 应用中使用它的一个示例

<?php

declare(strict_types=1);

namespace LLM\Agents\Agent\SymfonyConsole\Integrations\Laravel;

use Illuminate\Contracts\Console\Kernel;
use LLM\Agents\Agent\SymfonyConsole\CommandManagerInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;

final readonly class ArtisanCommandManager implements CommandManagerInterface
{
    public function __construct(
        private Kernel $application,
        private array $enabledNamespaces = [
            'make:',
            'db:',
            'migrate',
            'route:list',
        ],
    ) {}

    public function getCommandHelp(string $command): string
    {
        $output = new BufferedOutput();
        $this->application->call($command, ['--help' => true], $output);

        return $output->fetch();
    }

    public function getCommands(): array
    {
        $commands = $this->application->all();

        $availableCommands = [];
        foreach ($this->enabledNamespaces as $namespace) {
            foreach ($commands as $name => $command) {
                if (\str_starts_with($name, $namespace)) {
                    $availableCommands[$name] = $command;
                }
            }
        }

        return $availableCommands;
    }

    public function call(\Stringable|string $command, array $parameters = [], ?OutputInterface $output = null): int
    {
        return $this->application->call($command, $parameters, $output);
    }
}

类图

想帮忙吗?🤝

我们喜欢贡献!如果你有让这个代理更酷的想法,这里是如何参与进来的

  1. 分支仓库
  2. 进行你的更改
  3. 创建一个新的 Pull Request

只需确保你的代码干净、有注释,并遵循 PSR-12 编码标准。

许可 📄

本项目采用 MIT 许可证 - 有关详细信息,请参阅 LICENSE 文件。

就这些了,各位!如果你有任何问题或遇到任何麻烦,请不要犹豫,打开一个问题。