- 注册 `LsTool` 和 `BashTool` 工具,支持目录操作和命令执行 - 增强工具调用逻辑,添加日志记录以提升调试能力 - 增加 `ToolRegistry` 和 `RunLoop` 的增量累积与排序优化 - 完善单元测试覆盖新工具的执行与行为验证
73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Services\CancelChecker;
|
|
use App\Services\OutputSink;
|
|
use App\Services\Tool\ToolCall;
|
|
use App\Services\Tool\ToolExecutor;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class ToolRunJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries;
|
|
|
|
public int $timeout;
|
|
|
|
public int $backoff;
|
|
|
|
/**
|
|
* @param array<string, mixed> $toolCall
|
|
*/
|
|
public function __construct(public string $sessionId, public array $toolCall)
|
|
{
|
|
$this->tries = (int) config('agent.tools.job.tries', 1);
|
|
$this->timeout = (int) config('agent.tools.job.timeout_seconds', 60);
|
|
$this->backoff = (int) config('agent.tools.job.backoff_seconds', 3);
|
|
}
|
|
|
|
public function handle(ToolExecutor $executor, OutputSink $sink, CancelChecker $cancelChecker): void
|
|
{
|
|
$call = ToolCall::fromArray($this->toolCall);
|
|
|
|
logger('ToolRunJob call:', $call->toArray());
|
|
|
|
if ($cancelChecker->isCanceled($this->sessionId, $call->parentRunId)) {
|
|
$sink->appendRunStatus($this->sessionId, $call->runId, 'CANCELED', [
|
|
'dedupe_key' => "run:{$call->runId}:status:CANCELED",
|
|
'parent_run_id' => $call->parentRunId,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$result = $executor->execute($call);
|
|
$sink->appendToolResult($this->sessionId, $result);
|
|
|
|
$status = $result->status === 'SUCCESS' ? 'DONE' : 'FAILED';
|
|
$sink->appendRunStatus($this->sessionId, $call->runId, $status, [
|
|
'parent_run_id' => $call->parentRunId,
|
|
'tool_call_id' => $call->toolCallId,
|
|
'dedupe_key' => "run:{$call->runId}:status:{$status}",
|
|
'error' => $result->error,
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
$sink->appendRunStatus($this->sessionId, $call->runId, 'FAILED', [
|
|
'parent_run_id' => $call->parentRunId,
|
|
'tool_call_id' => $call->toolCallId,
|
|
'dedupe_key' => "run:{$call->runId}:status:FAILED",
|
|
'error' => $exception->getMessage(),
|
|
]);
|
|
|
|
throw $exception;
|
|
}
|
|
}
|
|
}
|