Files
ars-backend/app/Jobs/ToolRunJob.php
ROOG 59d4831f00 main: 增强工具调用与消息流程
- 支持 tool.call 和 tool.result 消息类型处理
- 引入 Tool 调度与执行逻辑,支持超时与结果截断
- 增加 ToolRegistry 和 ToolExecutor 管理工具定义与执行
- 更新上下文构建与消息映射逻辑,适配工具闭环处理
- 扩展配置与环境变量,支持 Tool 调用相关选项
- 增强单元测试覆盖工具调用与执行情景
- 更新文档和 OpenAPI,新增工具相关说明与模型定义
2025-12-22 12:36:59 +08:00

71 lines
2.3 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);
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;
}
}
}