From d76f982a03148ceaec0aac7604ef8c234388934c Mon Sep 17 00:00:00 2001 From: Joren de Graaf Date: Thu, 13 Aug 2026 15:42:26 +0200 Subject: [PATCH] working version of the braindump, it generates task suggestions --- assets/styles/app.scss | 5 + src/Controller/BraindumpController.php | 137 +++++++++++++++++++-- templates/braindump/_task_result.html.twig | 33 ++++- templates/braindump/index.html.twig | 6 +- 4 files changed, 162 insertions(+), 19 deletions(-) diff --git a/assets/styles/app.scss b/assets/styles/app.scss index 955b342..6737c57 100644 --- a/assets/styles/app.scss +++ b/assets/styles/app.scss @@ -352,4 +352,9 @@ header { .task-list__item--completed .task-list__title { opacity: .65; text-decoration: line-through; +} +.tasks-result { + background-color: #fff; + color: #000; + padding: 25px; } \ No newline at end of file diff --git a/src/Controller/BraindumpController.php b/src/Controller/BraindumpController.php index 9123d3f..b4976b3 100644 --- a/src/Controller/BraindumpController.php +++ b/src/Controller/BraindumpController.php @@ -21,27 +21,140 @@ final class BraindumpController extends AbstractController Request $request, HttpClientInterface $client ): Response { - if (!$this->isCsrfTokenValid( - 'braindump_dump', - $request->getPayload()->getString('_token') - )) { + // Give Ollama enough time to process a larger brain dump. + set_time_limit(120); + + // Check the CSRF token. + if ( + !$this->isCsrfTokenValid( + 'braindump_dump', + $request->getPayload()->getString('_token') + ) + ) { throw $this->createAccessDeniedException('Invalid CSRF token.'); } + // Get the user's brain dump. $brainDump = $request->getPayload()->getString('brain_dump'); - - $response = $client->request('POST', 'http://192.168.1.238:11434/api/generate', [ - 'json' => [ - 'model' => 'qwen3:4b', - 'prompt' => $brainDump, - 'stream' => false, + // dd('THIS IS THE NEW CONTROLLER', $brainDump); + // Tell Ollama exactly what structure we want back. + $schema = [ + 'type' => 'object', + 'properties' => [ + 'tasks' => [ + 'type' => 'array', + 'items' => [ + 'type' => 'object', + 'properties' => [ + 'title' => [ + 'type' => 'string', + ], + 'description' => [ + 'type' => 'string', + ], + 'estimatedDurationMinutes' => [ + 'type' => 'integer', + ], + 'priority' => [ + 'type' => 'string', + 'enum' => [ + 'Low', + 'Medium', + 'High', + ], + ], + 'category' => [ + 'type' => 'string', + ], + 'isRecurring' => [ + 'type' => 'boolean', + ], + ], + 'required' => [ + 'title', + 'description', + 'estimatedDurationMinutes', + 'priority', + 'category', + 'isRecurring', + ], + ], + ], ], - ]); + 'required' => [ + 'tasks', + ], + ]; + + // Ask Ollama to turn the brain dump into concrete tasks. + $response = $client->request( + 'POST', + 'http://192.168.1.238:11434/api/generate', + [ + 'timeout' => 120, + 'max_duration' => 120, + + 'json' => [ + 'model' => 'qwen3:4b', + + 'prompt' => << $schema, + + // We want one complete response rather than a stream. + 'stream' => false, + + // More deterministic output is useful for structured data. + 'options' => [ + 'temperature' => 0, + ], + ], + ] + ); $data = $response->toArray(); + // dd($data); +// dd($data['response']); + file_put_contents( + '/tmp/ollama-response.txt', + $data['response'] ?? 'NO RESPONSE' + ); + //$json = $data['response'] ?? '{"tasks":[]}'; + $json = $data['thinking'] ?? '{"tasks":[]}'; + $tasks = json_decode($json, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \RuntimeException( + 'Invalid JSON returned by Ollama: ' + . json_last_error_msg() + . "\n\nRaw response:\n" + . $json + ); + } + // Give the tasks to our HTMX partial. return $this->render('braindump/_task_result.html.twig', [ - 'response' => $data['response'] ?? 'No response received.', + 'tasks' => $tasks['tasks'] ?? [], ]); } } \ No newline at end of file diff --git a/templates/braindump/_task_result.html.twig b/templates/braindump/_task_result.html.twig index 45bb1b6..7f09005 100644 --- a/templates/braindump/_task_result.html.twig +++ b/templates/braindump/_task_result.html.twig @@ -1,5 +1,30 @@ -
-

Ollama says:

+{% if tasks is empty %} +

No tasks were suggested.

+{% else %} +

Suggested tasks

-

{{ response|nl2br }}

-
\ No newline at end of file + {% for task in tasks %} +
+

{{ task.title }}

+
    + {% if task.description %} +
  • {{ task.description }}

  • + {% endif %} + +
  • +
    Duration
    +
    {{ task.estimatedDurationMinutes }} minutes
    + +
    Priority
    +
    {{ task.priority }}
    + +
    Category
    +
    {{ task.category }}
    + +
    Recurring
    +
    {{ task.isRecurring ? 'Yes' : 'No' }}
    +
  • +
+ {% endfor %} + +{% endif %} \ No newline at end of file diff --git a/templates/braindump/index.html.twig b/templates/braindump/index.html.twig index fa35e7a..d018ad5 100644 --- a/templates/braindump/index.html.twig +++ b/templates/braindump/index.html.twig @@ -3,7 +3,7 @@ {% block title %}Brain Dump{% endblock %} {% block body %} - +

Brain Dump

-
+
{# HTMX will put the proposed tasks here #}
- +
{% endblock %} \ No newline at end of file