generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76c1804
commit ac4d195
Showing
9 changed files
with
348 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[2025-02-02 09:01:15] Agent not found: NonExistentAgent | ||
[2025-02-02 09:01:55] Agent not found: NonExistentAgent | ||
[2025-02-02 09:03:42] Agent not found: NonExistentAgent | ||
[2025-02-02 09:04:09] Agent not found: NonExistentAgent | ||
[2025-02-02 09:04:40] Agent not found: NonExistentAgent | ||
[2025-02-02 09:05:31] Agent not found: NonExistentAgent | ||
[2025-02-02 09:06:24] Agent not found: NonExistentAgent | ||
[2025-02-02 09:06:51] Agent not found: NonExistentAgent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace LarAgent\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
|
||
class AgentChatCommand extends Command | ||
{ | ||
protected $signature = 'agent:chat {agent : The name of the agent to chat with} {--history= : Chat history name}'; | ||
|
||
protected $description = 'Start an interactive chat session with an agent'; | ||
|
||
protected function logError($message) | ||
{ | ||
$logPath = 'logs/agent-chat-errors.log'; | ||
$timestamp = date('Y-m-d H:i:s'); | ||
$logMessage = "[{$timestamp}] {$message}\n"; | ||
|
||
if (!is_dir(dirname($logPath))) { | ||
mkdir(dirname($logPath), 0755, true); | ||
} | ||
|
||
file_put_contents($logPath, $logMessage, FILE_APPEND); | ||
} | ||
|
||
public function handle() | ||
{ | ||
$agentName = $this->argument('agent'); | ||
$historyName = $this->option('history') ?? Str::random(10); | ||
|
||
// Try both namespaces | ||
$agentClass = "\\App\\AiAgents\\{$agentName}"; | ||
if (!class_exists($agentClass)) { | ||
$agentClass = "\\App\\Agents\\{$agentName}"; | ||
if (!class_exists($agentClass)) { | ||
$this->error("Agent not found: {$agentName}"); | ||
$this->logError("Agent not found: {$agentName}"); | ||
return 1; | ||
} | ||
} | ||
|
||
$agent = $agentClass::for($historyName); | ||
|
||
$this->info("Starting chat with {$agentName}"); | ||
$this->line("Using history: {$historyName}"); | ||
$this->line("Type 'exit' to end the chat\n"); | ||
|
||
while (true) { | ||
$message = $this->ask('You'); | ||
|
||
if ($message === null || strtolower($message) === 'exit') { | ||
$this->info("Chat ended"); | ||
return 0; | ||
} | ||
|
||
try { | ||
$response = $agent->respond($message); | ||
$this->line("\n<comment>{$agentName}:</comment>"); | ||
$this->line($response."\n"); | ||
} catch (\Exception $e) { | ||
$this->error("Error: ".$e->getMessage()); | ||
$this->logError("Error in {$agentName} response: ".$e->getMessage()."\nStack trace:\n".$e->getTraceAsString()); | ||
return 1; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace LarAgent\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
|
||
class MakeAgentCommand extends Command | ||
{ | ||
protected $signature = 'make:agent {name : The name of the agent}'; | ||
|
||
protected $description = 'Create a new LarAgent agent class'; | ||
|
||
public function handle() | ||
{ | ||
$name = $this->argument('name'); | ||
|
||
$path = app_path('AiAgents/'.$name.'.php'); | ||
|
||
if (file_exists($path)) { | ||
$this->error('Agent already exists: '.$name); | ||
return 1; | ||
} | ||
|
||
$stub = file_get_contents(__DIR__.'/stubs/agent.stub'); | ||
|
||
$stub = str_replace('{{ class }}', $name, $stub); | ||
|
||
if (!is_dir(dirname($path))) { | ||
mkdir(dirname($path), 0755, true); | ||
} | ||
|
||
file_put_contents($path, $stub); | ||
|
||
$this->info('Agent created successfully: '.$name); | ||
$this->line('Location: '.$path); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\AiAgents; | ||
|
||
use LarAgent\Agent; | ||
|
||
class {{ class }} extends Agent | ||
{ | ||
protected $model = 'gpt-4'; | ||
|
||
protected $history = 'in_memory'; | ||
|
||
protected $provider = 'default'; | ||
|
||
protected $tools = []; | ||
|
||
public function instructions() | ||
{ | ||
return "Define your agent's instructions here."; | ||
} | ||
|
||
public function prompt($message) | ||
{ | ||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
use LarAgent\Commands\AgentChatCommand; | ||
use LarAgent\Agent; | ||
use LarAgent\Tests\Fakes\FakeLlmDriver; | ||
|
||
beforeEach(function () { | ||
// Create a mock agent class file | ||
if (!is_dir(app_path('AiAgents'))) { | ||
mkdir(app_path('AiAgents'), 0755, true); | ||
} | ||
|
||
$agentContent = <<<'PHP' | ||
<?php | ||
namespace App\AiAgents; | ||
use LarAgent\Agent; | ||
use LarAgent\Tests\Fakes\FakeLlmDriver; | ||
class TestAgent extends Agent | ||
{ | ||
protected $model = 'gpt-4-mini'; | ||
protected $history = 'in_memory'; | ||
protected $provider = 'default'; | ||
protected $tools = []; | ||
protected $driver = FakeLlmDriver::class; | ||
public function instructions() | ||
{ | ||
return "Test agent instructions"; | ||
} | ||
public function prompt($message) | ||
{ | ||
return $message; | ||
} | ||
protected function onInitialize() | ||
{ | ||
$this->llmDriver->addMockResponse('stop', [ | ||
'content' => 'Test response', | ||
]); | ||
} | ||
} | ||
PHP; | ||
|
||
file_put_contents(app_path('AiAgents/TestAgent.php'), $agentContent); | ||
|
||
// Make sure the autoloader can find our test agent | ||
require_once app_path('AiAgents/TestAgent.php'); | ||
}); | ||
|
||
afterEach(function () { | ||
// Clean up the test agent | ||
if (file_exists(app_path('AiAgents/TestAgent.php'))) { | ||
unlink(app_path('AiAgents/TestAgent.php')); | ||
} | ||
|
||
if (is_dir(app_path('AiAgents')) && count(scandir(app_path('AiAgents'))) <= 2) { | ||
rmdir(app_path('AiAgents')); | ||
} | ||
}); | ||
|
||
test('it fails when agent does not exist', function () { | ||
$this->artisan('agent:chat', ['agent' => 'NonExistentAgent']) | ||
->assertFailed() | ||
->expectsOutput('Agent not found: NonExistentAgent'); | ||
}); | ||
|
||
test('it can start chat with existing agent', function () { | ||
$this->artisan('agent:chat', ['agent' => 'TestAgent']) | ||
->expectsOutput('Starting chat with TestAgent') | ||
->expectsQuestion('You', 'exit') | ||
->expectsOutput('Chat ended') | ||
->assertExitCode(0); | ||
}); | ||
|
||
test('it uses provided history name', function () { | ||
$this->artisan('agent:chat', [ | ||
'agent' => 'TestAgent', | ||
'--history' => 'test_history' | ||
]) | ||
->expectsOutput('Using history: test_history') | ||
->expectsQuestion('You', 'exit') | ||
->expectsOutput('Chat ended') | ||
->assertExitCode(0); | ||
}); | ||
|
||
test('it can handle multiple messages', function () { | ||
$this->artisan('agent:chat', ['agent' => 'TestAgent']) | ||
->expectsOutput('Starting chat with TestAgent') | ||
->expectsQuestion('You', 'Hello') | ||
->expectsOutputToContain('Test response') | ||
->expectsQuestion('You', 'exit') | ||
->expectsOutput('Chat ended') | ||
->assertExitCode(0); | ||
}); |
Oops, something went wrong.