|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LlmLaraHub\LlmDriver\Functions; |
| 4 | + |
| 5 | +use App\Models\Message; |
| 6 | +use App\Models\Task; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Support\Facades\Log; |
| 9 | +use LlmLaraHub\LlmDriver\Responses\FunctionResponse; |
| 10 | + |
| 11 | +class CreateTasksTool extends FunctionContract |
| 12 | +{ |
| 13 | + protected string $name = 'create_tasks_tool'; |
| 14 | + |
| 15 | + protected string $description = 'If the Campaign needs to have tasks created or the users prompt requires it you can use this tool to make multiple tasks'; |
| 16 | + |
| 17 | + public bool $showInUi = true; |
| 18 | + |
| 19 | + public array $toolTypes = [ |
| 20 | + ToolTypes::Source, |
| 21 | + ToolTypes::Output, |
| 22 | + ToolTypes::Chat, |
| 23 | + ToolTypes::ChatCompletion, |
| 24 | + ]; |
| 25 | + |
| 26 | + public function handle( |
| 27 | + Message $message): FunctionResponse |
| 28 | + { |
| 29 | + Log::info('TaskTool called'); |
| 30 | + |
| 31 | + $args = $message->args; |
| 32 | + foreach (data_get($args, 'tasks', []) as $taskArg) { |
| 33 | + $name = data_get($taskArg, 'name', null); |
| 34 | + $details = data_get($taskArg, 'details', null); |
| 35 | + $due_date = data_get($taskArg, 'due_date', null); |
| 36 | + $assistant = data_get($taskArg, 'assistant', false); |
| 37 | + $user_id = data_get($taskArg, 'user_id', null); |
| 38 | + |
| 39 | + $project = $message->chat->getChatable(); |
| 40 | + |
| 41 | + Task::updateOrCreate([ |
| 42 | + 'name' => $name, |
| 43 | + 'project_id' => $project->id, |
| 44 | + ], |
| 45 | + [ |
| 46 | + 'details' => $details, |
| 47 | + 'due_date' => $due_date, |
| 48 | + 'assistant' => $assistant, |
| 49 | + 'user_id' => ($user_id !== '' && User::whereId($user_id)->exists()) ? $user_id : null, |
| 50 | + ]); |
| 51 | + } |
| 52 | + |
| 53 | + return FunctionResponse::from([ |
| 54 | + 'content' => json_encode($args), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return PropertyDto[] |
| 60 | + */ |
| 61 | + protected function getProperties(): array |
| 62 | + { |
| 63 | + return [ |
| 64 | + new PropertyDto( |
| 65 | + name: 'tasks', |
| 66 | + description: 'Array of task objects', |
| 67 | + type: 'array', |
| 68 | + required: true, |
| 69 | + properties: [ |
| 70 | + new PropertyDto( |
| 71 | + name: 'items', |
| 72 | + description: 'Task object', |
| 73 | + type: 'object', |
| 74 | + required: true, |
| 75 | + properties: [ |
| 76 | + new PropertyDto( |
| 77 | + name: 'name', |
| 78 | + description: 'Name of the task', |
| 79 | + type: 'string', |
| 80 | + required: true |
| 81 | + ), |
| 82 | + new PropertyDto( |
| 83 | + name: 'details', |
| 84 | + description: 'Detailed info of the task', |
| 85 | + type: 'string', |
| 86 | + required: true |
| 87 | + ), |
| 88 | + new PropertyDto( |
| 89 | + name: 'due_date', |
| 90 | + description: 'Due date if any format "Y-m-d"', |
| 91 | + type: 'string', |
| 92 | + required: true |
| 93 | + ), |
| 94 | + new PropertyDto( |
| 95 | + name: 'assistant', |
| 96 | + description: 'Should the assistant be assigned this true or false', |
| 97 | + type: 'string', |
| 98 | + ), |
| 99 | + new PropertyDto( |
| 100 | + name: 'user_id', |
| 101 | + description: 'User id if assigned to a user', |
| 102 | + type: 'string', |
| 103 | + ), |
| 104 | + ] |
| 105 | + ), |
| 106 | + ] |
| 107 | + ), |
| 108 | + ]; |
| 109 | + } |
| 110 | +} |
0 commit comments